首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Webview中显示pdf文档?

如何在Webview中显示pdf文档?
EN

Stack Overflow用户
提问于 2018-06-10 01:09:30
回答 1查看 723关注 0票数 0

我想在android上的新活动中显示PDF,我正在使用webview,我有一个纯PHP网页,当我在输入中输入代码并按下按钮时,将打开一个新页面,其中包含创建的PDF文件,并根据输入的代码

INPUT + BUTTON

NEW PDF PAGE

我在android上有这些权限

代码语言:javascript
复制
webSetting.setJavaScriptEnabled(true);
    webSetting.setDisplayZoomControls(true);
    htmlWebView.getSettings().setJavaScriptEnabled(true);
    htmlWebView.getSettings().setDomStorageEnabled(true);
    htmlWebView.getSettings().setDatabaseEnabled(true);
    htmlWebView.getSettings().setMinimumFontSize(1);
    htmlWebView.getSettings().setMinimumLogicalFontSize(1);

我知道Android不能在同一个网页视图中显示PDF )

我执行了以下操作,在web上按下按钮并开始下载已经创建的PDF,而不是使用以下命令打开页面:

代码语言:javascript
复制
header("Content-Type: application/octet-stream");
$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
 echo fread($fp, 65536);
 } 
fclose($fp); 

但它仍然不起作用,我使用以下代码下载其他文件而不打开它们,直接下载。

代码语言:javascript
复制
 htmlWebView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.setMimeType(mimeType);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            request.setDescription("Downloading PDF...");
            request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalFilesDir(MainActivity.this, Environment.DIRECTORY_DOWNLOADS,".pdf");
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Toast.makeText(getApplicationContext(), "Downloading PDF...",
                    Toast.LENGTH_LONG).show();
        }});

还有其他一些我不知道的技巧,因为我研究了几天,但什么也找不到。

EN

回答 1

Stack Overflow用户

发布于 2018-06-10 01:21:41

您可以使用Google Docs Viewer在线阅读您的pdf:

代码语言:javascript
复制
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true); 
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50776684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档