javascript 如何使用 pdf.js 使 PDF 无法下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17547956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make PDF undownloadable using pdf.js
提问by sathyam1992
采纳答案by Talley Ouro
Here are the steps:
以下是步骤:
- Add jQuery library to shared folder.
- Include jQuery library to viewer.html file
Add this on the header section:
<script> $(function(){ $('#download').hide(); }); </script>
- 将 jQuery 库添加到共享文件夹。
- 将 jQuery 库包含到 viewer.html 文件中
在标题部分添加以下内容:
<script> $(function(){ $('#download').hide(); }); </script>
Done!
完毕!
回答by user247471
Just deleting the buttons breaks pdf.js. You need to add a "hidden" class to them (https://github.com/mozilla/pdf.js/issues/2611)
只是删除按钮会破坏 pdf.js。您需要为它们添加一个“隐藏”类(https://github.com/mozilla/pdf.js/issues/2611)
回答by vignesh
just add this in viewer.css
只需在viewer.css中添加这个
.download
{
display:none !important;
}
.print
{
display:none !important;
}
回答by Frambot
Modify the source. Line 85 of web/viewer.html.
修改源码。web/viewer.html 的第 85 行。
https://github.com/andreasgal/pdf.js/blob/master/web/viewer.html#L85
https://github.com/andreasgal/pdf.js/blob/master/web/viewer.html#L85
Just remove the button.
只需取下按钮即可。
<button id="download" title="Download" onclick="PDFView.download();" oncontextmenu="return false;">
<img src="images/download.svg" align="top" height="16"/>
Download
</button>
This won't completely stop experienced and eager users from downloading it. You can never stop that. But this is enough to raise the bar enough for the curious.
这不会完全阻止有经验和渴望的用户下载它。你永远无法阻止。但这足以提高好奇心的标准。
回答by Alok Kumar
The simplest method is to add hiddenclass to the specific button in the toolbar (download button in this case)
最简单的方法是给hidden工具栏中的特定按钮(本例中为下载按钮)添加类
PDF.JS has hidden class included by default in its CSS file. So just add a hiddenclass to the button which has the id downloadand secondaryDownload
PDF.JS 在其 CSS 文件中默认包含隐藏类。所以只需将一个hidden类添加到具有 iddownload和secondaryDownload
回答by 4oo4
Another way to do it actually looks to be using pdf.customise.js(a WordPress plugin that comes bundled with it does it this way). I did this to remove the openFile button.
另一种方法实际上看起来正在使用pdf.customise.js(与它捆绑在一起的 WordPress 插件就是这样做的)。我这样做是为了删除 openFile 按钮。
First, in viewer.html, add this:
首先,在 中viewer.html,添加以下内容:
<script src="pdf.customise.js"></script>
<script src="pdf.customise.js"></script>
Then, make your pdf.customise.jslike so:
然后,让你pdf.customise.js喜欢这样:
(function($) {
$(document).ready(function() {
var params = window.location.search.substring(1).split("&");
var disabledownload = false;
var disableprint = false;
var disabletext = false;
var disabledoc = false;
var disableopen = true;
for (var i = 0; i < params.length; i++) {
var value = params[i].split("=");
if (value && value.length == 2)
if (value[0] == "disabledownload" && value[1] == 1) disabledownload = 1;
else if (value[0] == "disableprint" && value[1] == 1) disableprint = 1;
else if (value[0] == "disabletext" && value[1] == 1) disabletext = 1;
else if (value[0] == "disabledoc" && value[1] ==
1) disabledoc = 1
}
var extracss = "";
if (disabledownload) extracss += " .download {display:none!important;}";
if (disableprint) extracss += " .print {display:none!important;}";
if (disabletext) extracss += " .textLayer {-webkit-touch-callout: none !important; -webkit-user-select: none !important; -khtml-user-select: none !important; -moz-user-select: none !important; -ms-user-select: none !important; user-select: none !important;} .selectTool { display: none !important;}";
if (disabledoc) extracss += " #documentProperties {display:none !important;}";
if (disableopen) extracss += " #openFile { display:none!important;}";
if (disableopen) extracss += " #secondaryOpenFile { display:none!important;}";
if (extracss) {
var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = extracss;
document.getElementsByTagName("head")[0].appendChild(style)
}
$(document).bind("pagerendered", function(e) {
if (disabledownload) $(".download").remove();
if (disableprint) $(".print").remove();
if (disableopen) $("#openFile").remove();
if (disableopen) $("#secondaryOpenFile").remove();
if (disabletext) {
$(".selectTool").remove();
$(".textLayer").remove();
if (PDFViewerApplication) PDFViewerApplication.pdfCursorTools.switchTool(1)
}
if (disabledoc) {
$(".documentProperties").prev(".horizontalToolbarSeparator").remove();
$(".documentProperties").remove()
}
})
})
})(jQuery);
I don't like that this uses jQuery instead of pure javascript (however it could be easily rewritten that way), but still works really well.
我不喜欢这使用 jQuery 而不是纯 javascript(但是它可以很容易地以这种方式重写),但仍然工作得很好。

