前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Electron加载网页中打开的PDF

Electron加载网页中打开的PDF

作者头像
码客说
发布2021-07-14 11:00:28
2.2K0
发布2021-07-14 11:00:28
举报
文章被收录于专栏:码客

正文

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

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

Nginx中允许资源跨域访问

代码语言:javascript
复制
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;

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

代码语言:javascript
复制
throw new Error("file origin does not match viewer's");

禁用工具条菜单

viewer.jswebViewerInitialized()方法最后添加

代码语言:javascript
复制
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');

访问方式

代码语言:javascript
复制
http://127.0.0.1:5500/web/viewer.html?file=https://www.psvmc.cn/123.pdf

修改后的资源下载地址

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

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

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

代码语言:javascript
复制
/pdf_show/web/viewer.html?file=https://www.psvmc.cn/123.pdf
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-07-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 正文
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档