Search code examples
androidhtmlwebviewhref

Cannot call Pdf from Html in my WebView


I have a Terms and Conditions page which uses a WebView to display the Terms which are in Html. It is loaded like this -

    WebView webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.loadUrl("file:///android_asset/TermsAndConditions.html");

Also in my assets folder is the MaintermsandConditions.pdf which is called from a link in the Html like this

<a href="file:///android_asset/MainTermsandConditions.pdf">Main Terms</a>

The only problem is that even though the Pdf is in my assets folder, nothing happens when I click the link. I've tried adding target="_blank" but it still won't work.

What am I doing wrong


Solution

  • If you whant dispaly PDF in WebView for free you have to options:

    1) Using Google .You have to host your file and load special link to your app:

    url = "https://docs.google.com/gview?embedded=true&url=" + urlOfYourPdf;
    

    2) Use PDF.js. You have to import this lib and add to assets folder. In html you have to link to this library. Then you can set in body your pdf file from assets.

    You can try : https://github.com/googlesamples/android-PdfRendererBasic, but this is only for API 21 and above.