webview will not load pdf files on link click

19,087

Solution 1

If you want to load pdf you can use Google docs to load it.

String googleDocs = "https://docs.google.com/viewer?url=";
String pdf_url = "http://kronox.org/documentacion/Hello.Android.new.pdf";  

webView.loadUrl(googleDocs + pdf_url);

NOTE: You need android.permission.INTERNET in Manifest file

Solution 2

Just create an Intent in your shouldOverrideUrlLoading method:

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if ( urlIsPDF(url)){
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "application/pdf");
            try{
                view.getContext().startActivity(intent);
            } catch (ActivityNotFoundException e) {
                //user does not have a pdf viewer installed
            }
        } else {
            webview.loadUrl(url);
        }
        return true;
    }

And then whenever a user clicks a PDF link in a page within your webview, the file will open in an external PDF app.

Solution 3

webView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".pdf")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "application/pdf");
            try {
                view.getContext().startActivity(intent);
            } catch (ActivityNotFoundException e) {
                //user does not have a pdf viewer installed
            }
        } else {
            webView.loadUrl(url);
        }

        return true;
    }
}

Solution 4

You have to override shouldOverrideUrlLoading method in WebClient. I use this approach with combination of intent and Google Docs as a backup:

/* You might want to move this string definition somewhere else */
final String googleDocs = "https://docs.google.com/viewer?url=";

WebView webView = new WebView(getContext());
//webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if (url.endsWith(".pdf")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "application/pdf");
            /* Check if there is any application capable to process PDF file. */
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            } else {
                /* If not, show PDF in Google Docs instead. */
                view.loadUrl(googleDocs + url);
            }
        } else {
            webView.loadUrl(url);
        }
        return true;
    }
});

You might need to change passing the context and accessing startActivity method, but other than that it should run as it is.

Also note that since API 24, there are 2 shouldOverrideUrlLoading methods you can override. As it is stated here from @CommonsWare, it is OK to override deprecated method.

Solution 5

This is the solution I use:

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".pdf")){
            String pdfUrl = googleDocs + url;
            view.loadUrl(pdfUrl);
        } else {
            view.loadUrl(url);
        }
        return true;
    }

with

private final String googleDocs = "https://docs.google.com/viewer?url=";
Share:
19,087
Chris
Author by

Chris

Updated on June 05, 2022

Comments

  • Chris
    Chris almost 2 years

    I have developed a web app that displays a list of pdf documents hosted on a web server. This app is embedded in a webview app for android however when I load the app on my phone, selection of a pdf link does nothing. What am I doing wrong? Thanks

    Heres the java code:

    package com.hellowebview;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    public class HellowebviewActivity extends Activity {
    /** Called when the activity is first created. */
    
    private WebView mWebView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://aipnz.webs.com");
        mWebView.setWebViewClient(new HelloWebViewClient());
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    
    }
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url)
        {
            webview.loadUrl(url);
            return true;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
        {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    }
    
  • Chris
    Chris about 12 years
    I would prefer to not use the googleDocs viewer, instead to load the PDF in an installed PDF viewer or a PDF render written into the activity code
  • Matt Baker
    Matt Baker over 8 years
    this is a great solution if you don't wan't to bump the user out of your app to view the PDF
  • Alex
    Alex almost 7 years
    Works for me!. For "urlIsPDF" I used: url.endsWith(".pdf")
  • Ashish John
    Ashish John over 6 years
    Just check packaging manager if intent is available before trying to open it
  • Md Saddamul Haque
    Md Saddamul Haque over 6 years
    this will definitely help all of you,i checked it on 28 Nov 2017,its working
  • Md Saddamul Haque
    Md Saddamul Haque over 6 years
    thanks-Works for me!. For "urlIsPDF" I used: url.endsWith(".pdf") – Alex
  • BharathRao
    BharathRao about 6 years
    nice solution...helped me a lot
  • Charles Tempo
    Charles Tempo almost 5 years
    Wonderful! Thank you for this nice solution!
  • Lucas Gras
    Lucas Gras over 4 years
    Great solution, seems that viewing pdf through webView is not possible with just the base url. Thanks.
  • mohammad alabid
    mohammad alabid about 4 years
    hello, how to do when the link of the pdf not contain .pdf extension like this foulabook.com/book/downloading/401212926
  • Hammad Khan
    Hammad Khan about 4 years
    If the link is not PDF that what do you want to do with it?
  • mohammad alabid
    mohammad alabid about 4 years
    the above link is link to direct pdf file
  • Hammad Khan
    Hammad Khan about 4 years
    You have to create custom handler for that depending in what format you are getting the data/file.
  • Kamlesh
    Kamlesh over 2 years
    This works only if pdf file size is small size otherwise this will not work. Not worked for me in case of 432 KB size pdf file. Thanks