我想在android上的新活动中显示PDF,我正在使用webview,我有一个纯PHP网页,当我在输入中输入代码并按下按钮时,将打开一个新页面,其中包含创建的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不能在同一个网页视图中显示PDF )
我执行了以下操作,在web上按下按钮并开始下载已经创建的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();
}});
还有其他一些我不知道的技巧,因为我研究了几天,但什么也找不到。
发布于 2018-06-10 01:21:41
您可以使用Google Docs Viewer在线阅读您的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);
https://stackoverflow.com/questions/50776684
复制相似问题