Home > other >  How to show PDF from base64 String
How to show PDF from base64 String

Time:11-16

I am receiving an encoded base64 string for a PDF file and I would to show it inside a fragment with a Floating Action button that can be used for saving or printing the pdf.

I have tried enter image description here

CodePudding user response:

I was able to solve my issue by using this library

Add following to your build.gradle

implementation 'com.github.mhiew:android-pdf-viewer:3.2.0-beta.1'

Add the following to your layout

<com.github.barteksc.pdfviewer.PDFView
        android:id="@ id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/app_bar" />

Then simply decode the base64 string and pass it to the pdfView as follow

private fun renderPdf(base64EncodedString: String) = try {
    val decodedString = Base64.decode(base64EncodedString, Base64.DEFAULT)
    binding.pdfView.fromBytes(decodedString).load()
} catch (e: Exception) {
    // handle error
}
  • Related