0

We are trying to allow our users to download / display PDF files in a web application (WebView)

The HTML is a simple

<iframe src="path/to/pdf" />

In a standard android browser everything works - when clicking the link, the browser is allowing either display the PDF, or download... (content-type is application/pdf)

When using the webview we do not get the same results - a blank page is showing, (maybe a new tab is not opening correctly?!)

Note - We cannot use Google Viewer - the PDF is in a user restricted area (a session is required) - and so we cannot provide a public link to the PDF, which is required for google viewer to work.

How can we make the webview work just like the regular browser?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • what you tried so far post your code along with question. – akhilesh0707 Feb 27 '18 at 10:03
  • the code is a simple webview opening an html page, which contains an iframe with src set to the PDF location... works in a regular mobile browser, not working in the webview –  Feb 27 '18 at 10:05
  • did you tired this link https://stackoverflow.com/questions/14578530/how-to-open-display-documents-pdf-doc-without-external-app – akhilesh0707 Feb 27 '18 at 10:10
  • This is using google docs viewer, we cannot use this. why isn't the webview interacting with the PDF like the regular android browser? –  Feb 27 '18 at 10:11
  • check another answer someone is suggesting custom lib. – akhilesh0707 Feb 27 '18 at 10:13
  • The custom library suggested isn't good enough, check the comments. - besides, why use a custom lib when the regular browser is interacting with the PDF just fine? what is the difference? - thanks anyway –  Feb 27 '18 at 10:15

2 Answers2

0

HTML is simple but it contains many css, javascript/jquery code dependencies. Which browser renders accordingly and javascript can run on browsers. As browsers are preconfigured to run javascript and render html css.


Android webview is not preconfigured to run javascript, css. It can render html only. So it looks weird in webview as it renders without css and does not run javascript


How to configure webview to run css and javascript, allow go forward in webview and go backword like browser?

Here are full properties listed. Android developers

Then your webview can let users download the file present there..


WebView mWebView = findViewById(R.id.myWebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new MyWebChromeClient((Activity)mContext));
mWebView.addJavascriptInterface(new testClass(), "jsinterface");
mWebView.loadUrl("UrlToLoad");
0

You need to append http://docs.google.com/gview?embedded=true&url= to open PDF file in Webview

String docInitialUrl = "http://docs.google.com/gview?embedded=true&url="+docUrl;
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.setInitialScale(100);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl(docInitialUrl);
Nits24
  • 11
  • 1