【发布时间】:2018-11-19 11:04:56
【问题描述】:
我想在 android 上的新活动中显示 PDF,我正在使用 webview,并且我有一个纯 PHP 网页,当我在 INPUT 中输入代码并按下按钮时会打开一个包含 PDF 文件的新页面创建并根据输入的代码
我在 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 不能在同一个 webview 中显示 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();
}});
还有一些我不知道的其他技术,因为我研究了几天,但找不到任何东西..
【问题讨论】:
-
WebView没有显示 PDF 的内置功能。您需要查看various PDF rendering options。
标签: android html webview webclient