如何在无法保存、打印或共享的情况下在 Web 浏览器中显示 pdf

How to display a pdf in a web browser without any possibility of saving, printing or sharing

我需要在 android 应用程序中显示一个 pdf 文件,这样用户将无法保存或共享它,Google 也不会保留它的副本。

我考虑过使用 iframe,但这允许用户在浏览器中打开 pdf 文件(并从那里打印或共享它)。

这是我想出的:

pdfView.setWebChromeClient(new WebChromeClient());
WebSettings settings = pdfView.getSettings();
settings.setPluginState(WebSettings.PluginState.ON);
pdfView.setWebViewClient(new WebViewClient());
pdfView.setBackgroundColor(0x00000000);
settings.setAllowFileAccess(false);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(false);
settings.setSaveFormData(false);
settings.setJavaScriptEnabled(true);

String html = "<iframe src=\"http://docs.google.com/gview?url=" + 
    path + "&embedded=true&wmode=opaque\"" +
    "style=\"width:600px; height:900px;\" scrolling=\"no\"" + 
    "frameborder=\"0\" allowfullscreen></iframe>";

pdfView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);

解决方案不必基于在网络浏览器中嵌入和显示。 我需要支持 API 16+,所以不能使用 PdfRenderer。

它帮助我在 iframe 标签中添加了 sandbox="allow-same-origin allow-scripts"。