Javascript 如何使用PDF.js使PDF不可下载
我发现这个项目非常有用。但是,我不知道如何删除“下载”选项 修改源代码。web/viewer.html的第85行 只需卸下按钮Javascript 如何使用PDF.js使PDF不可下载,javascript,pdf,pdf.js,Javascript,Pdf,Pdf.js,我发现这个项目非常有用。但是,我不知道如何删除“下载”选项 修改源代码。web/viewer.html的第85行 只需卸下按钮 <button id="download" title="Download" onclick="PDFView.download();" oncontextmenu="return false;"> <img src="images/download.svg" align="top" height="16"/> Downlo
<button id="download" title="Download" onclick="PDFView.download();" oncontextmenu="return false;">
<img src="images/download.svg" align="top" height="16"/>
Download
</button>
下载
这不会完全阻止有经验和渴望的用户下载它。你永远不能停止。但这足以提高好奇者的门槛。删除按钮会破坏pdf.js。 您需要向它们添加一个“隐藏”类()以下是步骤:
<script>
$(function(){
$('#download').hide();
});
</script>
$(函数(){
$(“#下载”).hide();
});
完成了 只需将其添加到viewer.css中即可
.download
{
display:none !important;
}
.print
{
display:none !important;
}
最简单的方法是将
hidden
类添加到工具栏中的特定按钮(本例中为下载按钮)
PDF.JS的CSS文件中默认包含隐藏类。因此,只需在按钮中添加一个
隐藏的
类,该类的id为下载
和第二次下载
,另一种方法实际上是使用pdf.customize.js
(附带的WordPress插件就是这样做的)。我这样做是为了删除openFile按钮
首先,在viewer.html
中添加以下内容:
然后,让您的pdf.customize.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);
(函数($){
$(文档).ready(函数(){
var params=window.location.search.substring(1.split(“&”);
var disabledownload=false;
var disableprint=false;
var disabletext=false;
var disabledoc=false;
var disableopen=true;
对于(变量i=0;i
我不喜欢它使用jQuery而不是纯javascript(尽管可以很容易地用这种方式重写),但它仍然工作得很好。将此添加到viewer.css: 要隐藏下载图标,请执行以下操作:
.toolbarButton.download {
display: none;
}
要隐藏打印机图标
.toolbarButton.download {
display: none;
}
对双方来说
.toolbarButton.download, .toolbarButton.print {
display: none;
}
无论用户在浏览器中看到什么,都可以下载。你能给我们代码吗?我不想从网站上下载内容只是为了帮你看看。是旧版本的@mishik吗?我们可以下载src的内容吗?谢谢@Joe Frambach,成功了!我的目的是阻止至少一个普通的用户,因为他们不适合我。当我删除此下载按钮时,网页不会加载PDF。@VivekSancheti是否删除整个下载按钮元素?该元素跨越第85-86-87-88行。有没有办法阻止任何用户下载该文件?@mak89k我们需要将其加密到我们自己的扩展名中,就像亚马逊对kindle电子书所做的那样,这样即使他们下载,如果没有正确的解密序列(这是我的想法!)也无法访问。非常简单的修复!如果我们要删除这些按钮,则需要删除事件侦听器。幸运的是,我能找到的所有地方都说“只需将隐藏类添加到要隐藏的按钮”,但在更新版本的pdf.js中[在许多按钮上,如open]似乎不起作用,并且没有关于它的问题报告。如果他们只是在JS中检查现有元素,然后再尝试将代码附加到它,那就太好了。。允许html自由调整而不令人头痛。我如何下载这样呈现的文件,带有隐藏打印按钮?有什么建议吗p$(“#下载”).style.display='none'太棒了!非常感谢这个简单的片段。我如何下载像这样呈现的文件,以及隐藏的打印按钮?有什么建议吗p相同:$(“#print”).style.display='none'我们可以禁用按钮,任何人都可以使用Ctrl+SIf下载PDF如果只是隐藏按钮,这应该是答案。。我不明白为什么大多数开发人员都不是