当前位置:网站首页>pdf.js 使用介绍
pdf.js 使用介绍
2022-07-19 01:50:00 【搬砖狗-小强】
项目背景
在网页中加载并显示PDF文件是最常见的业务需求。现在的浏览器大多数都自带pdf预览功能,但是每个浏览器的pdf加载器并不一样,工具栏也无法定制化,为了统一不同浏览器pdf预览的一致性,并增加一些自定义功能,我们使用pdf.js来实现pdf的预览。
pdf.js是一款非常优秀的pdf解析工具,其实我们火狐浏览器内核自带pdf预览解析器就是pdf.js这个解析器,我们把pdf在火狐浏览器打开按F12可以看到源码内容。
下面就是项目实现的最终效果,右侧工具栏只留下打印按钮,并在打印的时候触发项目需要的事件,防止复制和下载pdf。
pdf.js的使用
官网地址:http://mozilla.github.io/pdf.js,下载稳定版
项目结构
项目使用
把项目部署到nginx或者IIS,直接在浏览器中访问viewer.html即可预览我们的pdf,使用方法如下,file为我们要预览的pdf文件地址。
127.0.0.1/pdfjs-2.14.305-dist/web/viewer.html?file=/pdfjs-2.14.305-dist/web/compressed.tracemonkey-pldi-09.pdf
项目自定义
我们需要把右侧工具栏中很多不需要的按钮去掉,并且对按钮事件进行编辑,我们可以使用PDFViewerApplication对象进行调整。
window.onload = function () {
PDFViewerApplication.appConfig.toolbar.presentationModeButton.classList.add("hidden");
PDFViewerApplication.appConfig.toolbar.openFile.classList.add("hidden");
PDFViewerApplication.appConfig.toolbar.download.classList.add("hidden");
PDFViewerApplication.appConfig.toolbar.viewBookmark.classList.add("hidden");
}
function onbeforeprint() {
var url = GetQuery1("file");
var urlParams = parseUrlParams(url);
$.ajax({
type: "get",
url: "/PrintCount?sCardNos=" + urlParams.sCardNos,
success: function (data) { }
});
}
window.addEventListener("beforeprint", onbeforeprint);
项目部署
IIS中部署需要增加MIME类型.properties text/plain
其他
预览时禁止自动打印,在viewer.js方法_updateFromSandbox中把以下代码注释掉。
边栏推荐
- 小草满天飞
- make和Makefile总结二
- 【论文阅读】CoaT:Co-Scale Conv-Attentional Image Transformers
- 【 XXL-JOB】XXL-JOB学习
- CD 从抓轨到搭建流媒体服务器 —— 以《月临寐乡》为例
- Is the mobile account opening process of CITIC Securities safe? How to open a VIP account
- Live broadcast today | Apache pulsar meetup: vivo, Tencent cloud, bigo, Yunxing technology practice sharing
- Advanced numbers | [differential calculus of multivariate functions] concept chapter -- the relationship between continuity, partial differentiation and differentiability
- C language foundation day4 function notes
- 今日直播|Apache Pulsar Meetup:vivo、腾讯云、BIGO、云兴科技实践分享
猜你喜欢
Heavy! China open source map was officially launched, and we sincerely invite all open source communities to join us to create ~
C language foundation day4 function notes
微信小程序-获取用户位置(经纬度+所在城市)
点亮LED灯
认证培训|StreamNative Certification 培训第 2 期
今日直播|Apache Pulsar Meetup:vivo、腾讯云、BIGO、云兴科技实践分享
【论文阅读】CoaT:Co-Scale Conv-Attentional Image Transformers
NPE: An FPGA - based overlay Processor for Natural Language
"Taige experiment" SLA linkage static routing experiment
Dynamic memory management - C language
随机推荐
微信小程序-获取用户位置(经纬度+所在城市)
[HAOI2012] 音量调节(可达背包)
Mark and Lightbulbs(思维)
U++ 使用setTimer函数
ReplicaSet
Based on yarn1 Sharing of monorepo practice of X
【活动早知道】LiveVideoStack近期活动一览
Enter the enterprise series | streamnational x Zhong'an insurance
自己用U++写的植物大战僵尸豌豆射手部分逻辑记录
MySQL 事务
020-024多态回顾
PHP支付宝转账到支付宝账号
软链接和硬链接区别
c语言---程序环境与预处理
18_过滤器
【C语言刷LeetCode】1462. 课程表 IV(M)
C language foundation day4 function notes
Yum install mysql 常见问题
【文献阅读】NPE: An FPGA-based Overlay Processor for Natural Language
js 获取两个时间段的时间组成数组