How To Load a PDF in an Android WebView
Written by @kylewbanks on Mar 6, 2013.
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.
Hey! 👋 In 2024 I left my career as a software engineer to pursue indie game development. Here's what I've been working on.
Let me know if this post was helpful on Twitter @kylewbanks or down below!