Skip to content Skip to sidebar Skip to footer

Opening A Pdf File Inside A Webview

Around 2 days I tried to open PDF files in my custom WebvView. Here's my WebView code: import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Dia

Solution 1:

There is no native support to open PDF files in Android, but you have some workarounds that can more or less do the job for you. Alternatives:

1 - You could open your system default app to read the pdf file by an intent from your app;

2 - You could use a third party library to open the pdf inside your app;

3 - You could invoke Google Docs website functionality to open your pdf file inside your webview. To do so, use the following code sample:

StringpdfUrl="http://yourwebsite.com/yourfile.pdf";
Stringurl="http://docs.google.com/gview?embedded=true&url=" + pdfUrl;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);

Solution 2:

If you want to display the PDF inside the web view use:

   url = "https://docs.google.com/viewer?url=https://linkToYourPdf.pdf"

   webView.loadUrl(url);

If your PDF contains more then one page, don't forget to enable the vertical scroll

   webview.setVerticalScrollBarEnabled(true);

Solution 3:

Try the following for the webview option if you choose that

WebViewwebview= (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true); 
Stringpdf="online pdf link";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

But Remember if you use google docs for viewing the pdf or any other doc, google docs may throw you the error stating

You've reached the bandwidth limit for viewing or downloading files that aren't in Google Docs format..... So doesn't seem reliable

So, be cautious with google doc viewing

please refer to my whole answer if you have a pdf reading through asset folder

Post a Comment for "Opening A Pdf File Inside A Webview"