Various ways to load PDF in Android webview

Android webview can not load PDF file by default. Luckily, there are various ways to how to load PDF file in Android webview, like use google service, pdf.js etc.

Android webview can not load PDF file by default. :(

Luckily, there are various ways to how to load PDF file in Android webview.

Open Intent with startActivity

If your requirement does not need use embedded webview to show PDF, you can just use startActivity() to let system choose proper app to open PDF:

val intent = Intent(Intent.ACTION_VIEW) = Uri.parse(url)

Use google docs service to load pdf in html page

The trick is load PDF file in an html page, then use webview to load html.

Say you have an PDF with URL, you can load in html like this:

String pdfViewerURL = "";
WebView webview = (WebView) findViewById(;

String pdfURL = "";
String url = pdfViewerURL + pdfURL

This does not require any third party library, but it depends on Google service. There are reports say Google pdf viewer may return error:

Use pdfjs

pdf.js is a general-purpose, web standards-based platform for parsing and rendering PDFs.

Use pdf.js need extra work, there is open source project PdfWebview have a great sample.

Create resource files under src/main/assets/pdfviewer.html

<html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=4.0,user-scalable=yes"/>
  <style type="text/css">
   canvas {
     width: 100%;
     height: 100%;
     border: 1px solid black;
  <script type="text/javascript" src="pdf.js"></script>
  <script type="text/javascript" src="pdf.worker.js"></script>
  <script type="text/javascript" src="pdf.sandbox.js"></script>
  <script type="text/javascript" src="pdfviewer.js"></script>

Create resource files under src/main/assets/pdfviewer.js

var url =;

function createPage() {
    var div = document.createElement("canvas");
    return div;

function renderPage(num) {
    pdfDoc.getPage(num).then(function (page) {
        var viewport = page.getViewport({ scale: 2.0 });
        var canvas = createPage();
        var ctx = canvas.getContext('2d');

        canvas.height = viewport.height;
        canvas.width = viewport.width;

            canvasContext: ctx,
            viewport: viewport
        }).promise.then(() => {});

pdfjsLib.getDocument(url).promise.then(function (pdf) {
    pdfDoc = pdf;
    for (var i = 1; i <= pdfDoc.numPages; i++) {

Download pdf.js to assets

Then download pdf.js, pdf.worker.js and pdf.sandbox.js from pdf.js to src/main/assets/.

Sample code to load pdf with pdf.js

Here is an example code to load pdf in webview use pdf.js:

webView = findViewById(
val settings: WebSettings? = webView?.settings
settings?.javaScriptEnabled = true
settings?.allowFileAccess = true

settings?.builtInZoomControls = true
settings?.displayZoomControls = false


That’s it.


OmniLock - Block / Hide App on iOS

Block distractive apps from appearing on the Home Screen and App Library, enhance your focus and reduce screen time.

DNS Firewall for iOS and Mac OS

Encrypted your DNS to protect your privacy and firewall to block phishing, malicious domains, block ads in all browsers and apps