Electron加载网页中打开的PDF

正文

Electron2.x之后的版本都已经不支持PDF加载显示了,所以推荐网页中使用pdf.js来加载PDF

允许加载域名不一样的资源

Nginx中允许资源跨域访问

1
2
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;

viewer.js中注释掉下面的这行

1
throw new Error("file origin does not match viewer's");

禁用工具条菜单

viewer.jswebViewerInitialized()方法最后添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
appConfig.toolbar.presentationModeButton.setAttribute('hidden', 'true');
appConfig.toolbar.openFile.setAttribute('hidden', 'true');
appConfig.toolbar.print.setAttribute('hidden', 'true');
appConfig.toolbar.download.setAttribute('hidden', 'true');
appConfig.toolbar.viewBookmark.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.toolbar.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.toolbarButtonContainer.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.presentationModeButton.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.openFileButton.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.printButton.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.downloadButton.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.viewBookmarkButton.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.firstPageButton.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.lastPageButton.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.pageRotateCwButton.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.pageRotateCcwButton.setAttribute('hidden', 'true');
appConfig.secondaryToolbar.documentPropertiesButton.setAttribute('hidden', 'true');

访问方式

1
http://127.0.0.1:5500/web/viewer.html?file=https://www.psvmc.cn/123.pdf

修改后的资源下载地址

链接:https://pan.baidu.com/s/1cxhxFoumrxlEL1fk42x6IA
提取码:psvm

下载后直接放到项目的public目录下即可使用

项目中原来的地址修改为如下:

1
/pdf_show/web/viewer.html?file=https://www.psvmc.cn/123.pdf