如何将PDF文档显示到Webview中?

时间:2018-06-09 17:09:30

标签: android html webview webclient

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

INPUT + BUTTON

NEW PDF PAGE

我在android上有这些权限

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文件)

我做了以下操作,按下网络上的按钮,开始下载已创建的PDF,而不打开以下页面:

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

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

 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();
        }});

还有其他一些我不知道的技术因为我已经研究了几天而且找不到任何东西..

1 个答案:

答案 0 :(得分:0)

您可以使用Google文档查看器在线阅读您的PDF格式:

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