如何使用pdf.js使PDF文件不可下载

14

我找到了pdf.js项目,这个项目非常有用。但是,我不知道如何删除“下载”选项。


11
用户在浏览器中看到的内容都可以下载。 - mishik
你能给我们提供代码吗?我不想只是为了帮你检查内容而去下载网站上的东西。 - Matheno
1
https://github.com/andreasgal/pdf.js 是 https://github.com/mozilla/pdf.js 的旧版本。 - async5
1
@mishik,我们可以下载src的内容吗? <video class="video-responsive bg-dark" autoplay="" poster="/s/videos/5f6b72f30cf2d09aa32cb6d9/cover" src="blob:https://www.durgasoftonline.com/6302d93a-de88-4436-90a5-a5ae75cb17e3"></video> - Ajay Takur
9个回答

18

只需在viewer.css中添加此内容。

.download
{
    display:none !important;    
}

.print
{
    display:none !important;
}

1
如果只是隐藏按钮,这应该就是答案。我不明白为什么大多数开发人员现在要使用js或jquery解决CSS问题!对此回答点赞! - Oliver M Grech

13

太棒了,非常容易修复! - Louwki
如果我们要移除这些按钮,也需要移除事件监听器。 - Vasistan
1
不幸的是,我在任何地方都找不到“只需将隐藏类添加到您想要隐藏的按钮”这样的说法,但似乎在较新版本的pdf.js中,这种方法并不适用于许多按钮(例如打开按钮),而且没有相关问题报告。如果他们在JS中对现有元素进行检查,然后再尝试将代码附加到它上面,那就太好了...这样可以自由调整HTML而不会头疼。 - NinjaKC
如何下载像这样渲染的文件,其中还有隐藏的打印按钮?有什么建议吗? :p - Mahesha999

8
以下是步骤:
  1. Add jQuery library to shared folder.
  2. Include jQuery library to viewer.html file
  3. Add this on the header section:

    <script>
    $(function(){
        $('#download').hide();
    });
    </script>
    

完成!


$("#download").style.display = 'none'$("#下载").style.display = 'none' - Perry
太好了!非常感谢这个简单的代码片段。 - ozeray
1
我该如何下载像这样渲染的文件,其中包含隐藏的打印按钮?有什么建议吗? :p - Mahesha999
1
相同的代码:$("#print").style.display = 'none' - karrtojal
5
我们可以禁用该按钮,任何人都可以使用 Ctrl+S 下载 PDF。 - Hemanathagiribabu

5

修改源代码。在 web/viewer.html 文件的第 85 行。

https://github.com/andreasgal/pdf.js/blob/master/web/viewer.html#L85

只需删除按钮即可。

  <button id="download" title="Download" onclick="PDFView.download();" oncontextmenu="return false;">
    <img src="images/download.svg" align="top" height="16"/>
    Download
  </button>

这并不能完全阻止有经验和渴望的用户去下载它。你永远无法完全阻止这种情况。但是,这足以提高好奇者的门槛。


2
它对我不起作用。当我删除这个下载按钮时,网页无法加载PDF。 - user1500341
1
有没有一种方法可以阻止任何用户下载文件? - Anil Muppalla
@mak89k 我们需要像亚马逊将 Kindle 电子书加密为自己的扩展名一样对其进行加密,这样即使他们下载也无法在没有正确解密序列的情况下访问(这只是我的想法!) - sathyam1992
@mak89k 如果我们有一个高效的算法来完成任务,那么这个想法是否仍然有效呢?抛开性能问题。 - sathyam1992
1
这个解决方案不起作用secondary_toolbar.js:220 Uncaught (in promise) TypeError: Cannot read property 'addEventListener' of null at SecondaryToolbar._bindClickListeners (secondary_toolbar.js:220) at new SecondaryToolbar (secondary_toolbar.js:58) at Object._initializeViewerComponents (app.js:483) at Object.initialize (app.js:246) _bindClickListeners @ secondary_toolbar.js:220 SecondaryToolbar @ secondary_toolbar.js:58 _initializeViewerComponents @ app.js:483 initialize @ app.js:246 Promise.then (async) run @ app.js:539 webViewerLoad @ viewer.js:232 - Rocstar
显示剩余5条评论

2
另一种方法似乎可以使用 pdf.customise.js 实现(WordPress插件捆绑了这种方式)。我用这种方式删除了openFile按钮。
首先,在viewer.html中添加以下内容: <script src="pdf.customise.js"></script> 然后,按以下方式制作你的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);

我不喜欢它使用jQuery而不是纯JavaScript(但这可以很容易地重新编写),但仍然运行得非常好。"最初的回答"

完美解决方案,谢谢4oo4。我甚至添加了禁用书签按钮的逻辑。 - Rohutech

0

最简单的方法是在工具栏中的特定按钮(在此情况下为下载按钮)上添加hidden类。

PDF.JS在其CSS文件中默认包含了hidden类。因此,只需将hidden类添加到具有ID downloadsecondaryDownload的按钮即可。


-1
在viewer.js中找到download(),并将return false;放在最顶部。如果您还想禁用打印功能,同样可以在beforePrint()、afterPrint()和performPrint()之前添加这个代码。
然后,您可以使用CSS等其他方式来隐藏按钮。

Viewer.js

async download() {
    return false;
}
beforePrint() {
    return false;
}
afterPrint() {
    return false;
}
performPrint() {
    return false;
}

-1

@KJ,你用什么浏览器?无论如何,有时开发人员需要保护其PDF源文件,例如禁用直接访问(像IDM这样的工具可以嗅探“.pdf”),或者永远不要透露其原始文件名和.pdf扩展名,或者首先将PDF编码为base64。更多关于PDF.js主页的文档,请参考,即使没有绝对安全,但加固代码是开发人员自己工作的一部分,如果我说错了请指正 :) - iggnimutal
虽然这个链接可能回答了问题,但最好在此处包含答案的必要部分,并提供链接以供参考。如果链接页面更改,仅链接的答案可能会失效。-【来自审查】 - camille

-1

将以下内容添加到viewer.css文件中:

隐藏下载图标:

.toolbarButton.download {
    display: none;
}

隐藏打印机图标...

.toolbarButton.download {
    display: none;
}

For both...

.toolbarButton.download, .toolbarButton.print {
    display: none;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接