Load PDF in Android WebView

We can load PDF in android easily using WebView. We will be using the simplest way for displaying PDF file in android. Using the Google Docs PDF viewer we could do this easily. Now lets get in to the implementation and source code.

Related Posts:

You could watch the video, it explained everything and also shows you how exactly you can Load PDF in Android WebView. Don’t forget to subscribe if you find out contents useful.

The first thing you have to do is implement WebView, its really easy to implement a WebView in android. We will be doing only the necessary part.

Designing the layout part is the first thing, we will be using a Linear Layout as parent layout and then will be using the WebView inside it. You should set an id for the WebView and this is important for initializing the WebView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WebActivity">

    <WebView
        android:id="@+id/web"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Go to the Java part and create an object for WebView then initialize it.

WebView webView = findViewById(R.id.web);

Now we have to set the WebView client.

webView.setWebViewClient(new WebViewClient());

If you like to have zoom and need to enable JavaScript for WebView just these two lines which does this.

webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptEnabled(true);

You can load the PDF file now, you have to use the Google docs PDF viewer for this. Just add “https://docs.google.com/gview?embedded=true&url=” + the URL of PDF file.

webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + "https://pdfurl.com/file.pdf");

Full source code to Load PDF in Android WebView

WebView webView = findViewById(R.id.web);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + "https://pdfurl.com/file.pdf");

That’s all you can load a PDF file in WebView these simple steps.

10 thoughts on “Load PDF in Android WebView”

  1. Hello Sir
    Please help me with the Source Code So that I should Just Directly Change the Pdf url

  2. When I try to access the pdf that is present in my google drive it failed to load , showing blank screen .How can I fix it ?

  3. Hii

    is it possible to load web view pdf from internal/sd card storage without using the google docs link directly load to get external storage

  4. This is working after i have added these lines in webview

    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setAllowFileAccessFromFileURLs(true);
    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);

Leave a Comment