View PDF in Webview

Asked

Viewed 2,623 times

3

I am developing an application for a Clinical Analysis Laboratory. It has an online system where you can view the results.

On the site, it has a list of all customer exams and when I click on the link to open the exam, it loads a PDF in a new tab, this by Desktop. By Mobile, when I click on it, it seems to open a blank tab in Webview, but does not load any PDF, without downloading the PDF.

My Code:

wv = (WebView) findViewById(R.id.webViewResultados);

    WebSettings ws = wv.getSettings();
    ws.setJavaScriptEnabled(true);
    ws.setSupportZoom(false);
    ws.setAllowFileAccess(true);

    wv.loadUrl("http://187.17.196.42:8181/ConcentWeb/servlet/hlab8000");
    wv.setWebViewClient(new WebViewClient());
  • I use this library Android Pdfviewer to view the pdf files. See if it is useful for you.

2 answers

3

Option 1

Use the Viewer from Google Drive. See how it should look:

WebView ws = (WebView) findViewById(R.id.webview);
ws.getSettings().setJavaScriptEnabled(true); 
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
ws.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

Obs.: It is important to observe the usage limit in relation to Google Drive.

Option 2

Use the pdf.js, open-source project mozila’s, which is perhaps the most viable option as it has no use limit. Just download and copy the project to your assets. Take a read on Viewer options to see more options beyond the basics. See below how it would look:

String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";

webview = (WebView) findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + pdf  + "#zoom=page-width");
  • The problem as you said is the usage limit, because the page shows all the customer exams, so if he wants to consult several is difficult

1

  • Diego, I tested that option, but in my case it didn’t work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.