1. 下載下傳pdf.js
pdf.js官方下載下傳位址

2. 解壓
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檔案
想要加載别的檔案,應該怎麼做呢?在
web/viewer.js
檔案中找到
webViewerInitialized
方法:
如果位址欄參數中有file字段,就使用file字段的位址;如果沒有,就用
defaultUrl
4.1 預覽pdfjs伺服器内部的檔案
把檔案放到 web 目錄下:
通路位址:可以通路成功
http://localhost:8888/web/viewer.html?file=test.pdf
4.2 預覽其他伺服器中的檔案
在 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