Android Webview cannot render the pdf sometimes and shows blank/white page instead

后端 未结 5 2014
感情败类
感情败类 2021-01-11 13:51
  • Open the pdf in the webview using google docs
  • Open the same pdf or different pdf again and again.
  • Sometimes it will show the blank/white page in the
5条回答
  •  离开以前
    2021-01-11 14:23

    After testing second PDF URL file, WebView seems like that can not load large PDF file.

    Reason:

    WebView display HTML. The fact that this works at all is by a hack- google will convert simple PDFs into HTML. It doesn't seem like they support anything that big. Even if they did, I would expect loading a large page PDF converted to HTML would be so large I highly doubt you'd be able to load it without going OOM. Use an appropriate PDF library, make a real PDF rendering view, and make sure not to render more of the PDF at a time than you need (or else you'll go OOM anyway). In other words, don't rely on hacky solutions you never should have relied on in the first place.

    Solution:

    You should try alternatives like PDF.js running locally in your device, instead of a service like Google Docs preview.(Or download PDF first to local file path)

    Put it in your assets folder and tweak the example:

    wv.loadUrl("file:///android_asset/web/viewer.html");
    

    Also, you can have Out Of Memory situations. An alternative to try is a native viewer like AndroidPdfViewer.

提交回复
热议问题