pdf.js禁止下载,打印(包括快捷键ctrl+s,ctrl+p)
pdf.js本身功能是很完整的,但我们有需求要禁止下载和打印,所以找了网上的一些办法,综合一下。
步骤
- 页面中先隐藏下载和打印的按钮,找到 id=“print” 和 id=“download”,在样式中添加 style=“dispaly:none”
<!-- 打印 -->
<button id="print" class="toolbarButton hiddenMediumView" style="dispaly:none" title="Print" tabindex="32"
data-l10n-id="print">
<span data-l10n-id="print_label">Print</span>
</button>
<!-- 下载 -->
<button id="download" class="toolbarButton hiddenMediumView" style="dispaly:none" title="Save" tabindex="33"
data-l10n-id="save">
<span data-l10n-id="save_label">Save</span>
</button>
- 隐藏代码中实现下载和打印的方法,防止用户使用快捷键打印下载
bindEvents() {
const {
eventBus,
_boundEvents
} = this;
_boundEvents.beforePrint = this.beforePrint.bind(this);
_boundEvents.afterPrint = this.afterPrint.bind(this);
eventBus._on("resize", webViewerResize);
eventBus._on("hashchange", webViewerHashchange);
// eventBus._on("beforeprint", _boundEvents.beforePrint);
// eventBus._on("afterprint", _boundEvents.afterPrint);
eventBus._on("pagerender", webViewerPageRender);
eventBus._on("pagerendered", webViewerPageRendered);
eventBus._on("updateviewarea", webViewerUpdateViewarea);
eventBus._on("pagechanging", webViewerPageChanging);
eventBus._on("scalechanging", webViewerScaleChanging);
eventBus._on("rotationchanging", webViewerRotationChanging);
eventBus._on("sidebarviewchanged", webViewerSidebarViewChanged);
eventBus._on("pagemode", webViewerPageMode);
eventBus._on("namedaction", webViewerNamedAction);
eventBus._on("presentationmodechanged", webViewerPresentationModeChanged);
eventBus._on("presentationmode", webViewerPresentationMode);
eventBus._on("switchannotationeditormode", webViewerSwitchAnnotationEditorMode);
eventBus._on("switchannotationeditorparams", webViewerSwitchAnnotationEditorParams);
// eventBus._on("print", webViewerPrint);
// eventBus._on("download", webViewerDownload);
eventBus._on("firstpage", webViewerFirstPage);
eventBus._on("lastpage", webViewerLastPage);
.......
参考地址:https://blog.csdn.net/liuying93/article/details/83410405