[英]How can I display a pdf document into a Webview?
我想在 webview 上显示 pdf 内容。 这是我的代码:
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf");
我得到一个空白屏幕。 我也设置了互联网权限。
您可以使用 Google PDF 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("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
如果您使用仅查看 url,则用户不会登录到那里的 google 帐户。
https://docs.google.com/viewer?url=http://my.domain.com/yourPdfUrlHere.pdf
就用户体验而言,使用 google docs 打开 pdf 是一个坏主意。 它真的很慢而且反应迟钝。
从 api 21 开始,我们有了PdfRenderer ,它可以帮助将 pdf 转换为位图。 我从未使用过它,但似乎很容易。
其他解决方案是下载 PDF 并通过 Intent 将其传递给专用的 PDF 应用程序,该应用程序将显示它。 快速和良好的用户体验,特别是如果此功能不是您的应用程序的核心。
使用此代码下载并打开 PDF
public class PdfOpenHelper {
public static void openPdfFromUrl(final String pdfUrl, final Activity activity){
Observable.fromCallable(new Callable<File>() {
@Override
public File call() throws Exception {
try{
URL url = new URL(pdfUrl);
URLConnection connection = url.openConnection();
connection.connect();
// download the file
InputStream input = new BufferedInputStream(connection.getInputStream());
File dir = new File(activity.getFilesDir(), "/shared_pdf");
dir.mkdir();
File file = new File(dir, "temp.pdf");
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return file;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<File>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(File file) {
String authority = activity.getApplicationContext().getPackageName() + ".fileprovider";
Uri uriToFile = FileProvider.getUriForFile(activity, authority, file);
Intent shareIntent = new Intent(Intent.ACTION_VIEW);
shareIntent.setDataAndType(uriToFile, "application/pdf");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (shareIntent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(shareIntent);
}
}
});
}
}
为了使 Intent 工作,您需要创建一个FileProvider来授予接收应用程序打开文件的权限。
以下是您的实现方式: 在您的清单中:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
最后在资源文件夹中创建一个file_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="shared_pdf" path="shared_pdf"/>
</paths>
希望这会有所帮助 =)
使用此代码
private void pdfOpen(String fileUrl){
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
//---you need this to prevent the webview from
// launching another browser when a url
// redirection occurs---
webView.setWebViewClient(new Callback());
webView.loadUrl(
"http://docs.google.com/gview?embedded=true&url=" + fileUrl);
}
private class Callback extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(
WebView view, String url) {
return (false);
}
}
这里加载progressDialog。 需要给 WebClient 否则它会强制在浏览器中打开:
final ProgressDialog pDialog = new ProgressDialog(context);
pDialog.setTitle(context.getString(R.string.app_name));
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
WebView webView = (WebView) rootView.findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pDialog.show();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pDialog.dismiss();
}
});
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webView.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
实际上所有的解决方案都非常复杂,我找到了一个非常简单的解决方案(我不确定它是否适用于所有 sdk 版本)。 它将在预览窗口中打开 pdf 文档,用户可以在其中查看和保存/共享文档:
webView.setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
val i = Intent(Intent.ACTION_QUICK_VIEW)
i.data = Uri.parse(url)
if (i.resolveActivity(getPackageManager()) != null) {
startActivity(i)
} else {
val i2 = Intent(Intent.ACTION_VIEW)
i2.data = Uri.parse(url)
startActivity(i2)
}
})
(科特林)
这是在您收到评论中提到的错误之前谷歌允许的实际使用限制,如果用户将在应用程序中打开一生一次的pdf,那么我觉得它是完全安全的。 尽管建议使用 Android 5.0 / Lollipop 中的 Android 内置框架遵循本机方法,但它称为PDFRenderer 。
String webviewurl = "http://test.com/testing.pdf";
webView.getSettings().setJavaScriptEnabled(true);
if(webviewurl.contains(".pdf")){
webviewurl = "http://docs.google.com/gview?embedded=true&url=" + webviewurl; }
webview.loadUrl(webviewurl);
您可以使用
webView.getSettings().setJavaScriptEnabled(true);
val url = "http://your.domain.com/your_pdf_urlHere.pdf"
webView.loadUrl("https://docs.google.com/gview?embedded=true&url=$url")
如果你想在 webview 中打开嵌套的 pdf 链接
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading( view: WebView, request: WebResourceRequest): Boolean {
if (request.url.toString().endsWith(".pdf")) {
val url = "https://docs.google.com/gview?embedded=true&url=${request.url}"
view.loadUrl(url)
}
return false
}
}
这不会要求登录谷歌帐户
从这里下载源代码( 在 webview android 中打开 pdf )
活动_main.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="@+id/webview"></WebView>
</RelativeLayout>
主活动.java
package com.pdfwebview;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webview;
ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
listener();
}
private void init() {
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setTitle("PDF");
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
webview.loadUrl("https://drive.google.com/file/d/0B534aayZ5j7Yc3RhcnRlcl9maWxl/view");
}
private void listener() {
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pDialog.show();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pDialog.dismiss();
}
});
}
}
我使用https://developer.adobe.com/document-services/docs/overview/pdf-embed-api/howtos/修复了它
<div id="adobe-dc-view"></div>
<script>
$(document).on("click", "#your-id", function() {
var adobeDCView = new AdobeDC.View({ clientId: your - id, divId: "adobe-dc-view" });
adobeDCView.previewFile({
content: { location: { url: pdf - url } },
metaData: { fileName: "name.pdf" }
}, { embedMode: "FULL_WINDOW", defaultViewMode: "FIT_PAGE", showAnnotationTools: true, showDownloadPDF: true });
})
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.