-1

I have tried a lot to load pdf file from local storage to inside the webview. I have shown the implementation below. please check and let me know if you can guide me

    webView.settings.javaScriptEnabled = true
        webView.settings.builtInZoomControls = true

        webView.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
            }
            override fun onPageFinished(view: WebView?, url: String?) {
                // PDF file has finished loading
                val test = url
//                Toast.makeText(this, url, Toast.LENGTH_SHORT).show()
            }

            override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
                // An error has occurred while loading the PDF file
                val test = error?.description
            }

        }

        // Load the PDF file into the WebView
        try {

            val inputStream = assets.open("test.pdf")
            val data = ByteArray(inputStream.available())
            inputStream.read(data)
            inputStream.close()

            val base64 :String  = android.util.Base64.encodeToString(
                data, android.util.Base64.DEFAULT)
            val mimeType = "application/pdf"
            val encoding = "utf-8"
            val html = "<iframe src='data:$mimeType;base64,$base64' " +
                    "width='100%' height='100%' " +
                    "style='border: none;'></iframe>"
            webView.loadData(html, mimeType, encoding)
        } catch (e: Exception) {
            e.printStackTrace()
            Log.e("TAG", e.toString())
        }

Manifest permission

  <uses-permission android:name="android.permission.INTERNET" />
user3513759
  • 67
  • 10
  • WHy do you think you can? It's a webview, not a pdf viewer. It doesn't display pdfs. There's some old hacks that work by uploading it to Google Drive and displaying it from there, but by itself a webview doesn't do pdf. – Gabe Sechan Mar 10 '23 at 08:15
  • @GabeSechan because i saw alot of same possible solutions – user3513759 Mar 10 '23 at 09:34
  • @zomega no. This solution for to load pdf using url or intent. but I want to load pdf from local storage to app – user3513759 Mar 10 '23 at 11:56
  • @user3513759 In the other question they say WebView cannot display PDFs. So I think this is an answer to your question. You either have to upload your PDF to Google Drive or use the PdfRenderer class. It's all described in the other question. – zomega Mar 10 '23 at 12:00

1 Answers1

0

I tried to open a PDF in WebView but it didn't work so I think it doesn't support it.

But you could use pdf.js. Here is a demo.

You could also use the PdfRenderer class but I think it only renders one page.

Another option is to convert your PDF to HTML. LibreOffice Draw can do this for example.

You could also upload your PDF file to Google Driver and then view this URL in the WebView.

zomega
  • 1,538
  • 8
  • 26