Opening a tab with a PDF attachment in a Webview

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?


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");

您需要附加http://docs.google.com/gview?embedded=true&url=以在Webview中打开PDF文件

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);
链接地址: http://www.djcxy.com/p/46772.html

上一篇: 如何让jQuery限制上传文件类型?

下一篇: 在Web视图中打开带有PDF附件的选项卡