How To Load a PDF in an Android WebView
@kylewbanks on Mar 6, 2013.
Written by Hey, if you didn't already know, I'm currently working on an open world stealth exploration game called Farewell North in my spare time, which is available to wishlist on Steam!
Unfortunately, Android does not support viewing PDFs out of the box in a WebView. Luckily, Google has a nifty little tool that allows you to perform this very task quite easily using Google Docs. Basically we will embed our PDF in a Google Doc page on-the-fly and load that. Here's the code:
String myPdfUrl = "http://example.com/awesome.pdf"; String url = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl; Log.i(TAG, "Opening PDF: " + url); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(url);
Simply replace the myPdfUrl value with your own remote PDF, and you're done.
Let me know if this post was helpful on Twitter @kylewbanks or down below!