How To Load a PDF in an Android WebView
@kylewbanks on Mar 6, 2013.
Written by Hey! 👋 I recently left my job as a software engineer to become a full-time indie game developer. After four years of development you can find my first game, Farewell North, on Xbox, Nintendo Switch, and 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!