0
点赞
收藏
分享

微信扫一扫

在线预览pdf文件,使用pdf.js

千行 2022-10-10 阅读 120


1. 下载pdf.js

​​pdf.js官方下载地址​​

在线预览pdf文件,使用pdf.js_nginx

2. 解压

在线预览pdf文件,使用pdf.js_nginx_02

3. 运行启动

项目在本地环境(url以file://开头)无法运行,需要服务器环境。
使用nginx,修改配置文件 nginx安装目录​​​/conf/nginx.conf​

server {
listen 8888;

location / {
root D:/studyspace/pdfjs-2.5.207-dist;
}

location /pdf/ {
proxy_pass http://127.0.0.1:8080/;
}
}

启动 nginx,访问 ​​http://localhost:8888/web/viewer.html​

4. 预览指定的pdf文件

在线预览pdf文件,使用pdf.js_服务器_03


想要加载别的文件,应该怎么做呢?在 ​​web/viewer.js​​​ 文件中找到 ​​webViewerInitialized​​ 方法:

在线预览pdf文件,使用pdf.js_html_04


如果地址栏参数中有file字段,就使用file字段的地址;如果没有,就用 ​​defaultUrl​

4.1 预览pdfjs服务器内部的文件

把文件放到 web 目录下:

在线预览pdf文件,使用pdf.js_服务器_05


访问地址:可以访问成功

http://localhost:8888/web/viewer.html?file=test.pdf

4.2 预览其他服务器中的文件

在更多的实际场景中,要预览的文件不会在pdfjs服务器的内部,一般都在另外一个业务服务器中!

在 nginx 配置中中添加对 /pdf/ 的代理

server {
listen 8888;

location / {
root D:/studyspace/pdfjs-2.5.207-dist;
}

location /pdf/ {
proxy_pass http://127.0.0.1:8080/;
}
}

测试预览:

  • 假如8080服务下有个11111.pdf,则实际打开下面的地址,就可以直接预览pdf文件了!

http://localhost:8888/web/viewer.html?file=/pdf/11111.pdf


举报

相关推荐

0 条评论