一、首先需要把pdfjs文件夾放在后端服務器,前端需要做的事情非常簡單
pdfjs在這里獲取:鏈接:https://pan.baidu.com/s/1ZmRkcUCvjGSJjVD6GdSWcQ 提取碼:ikdy
或者去網站下載:http://mozilla.github.io/pdf.js/getting_started/#download
二、在html中寫
<iframe :src="pdfSrc" frameborder="0" width="100%" height="100%"></iframe>
data:
data(){ return { pdfSrc: '', // pdf文件在服務器的路徑,例如:http://192.168.1.2:8080/file/pdf/32c1881d-0371-6e0d-aa5c-7bdf3f223300.pdf serverIp: '' // 服務器ip加端口,例如:http://192.168.1.2:8080/(這個是本地服務) } },
三、把服務器地址拼上pdfjs中viewer.html的路徑和pdf地址,賦給pdfSrc
this.pdfSrc = this.serverIp+'pdfjs/web/viewer.html?file='+this.$route.query.url
服務器地址和pdf生成地址都需要后端通過接口返回給你
如果出現跨域報錯:file origin does not match viewer’s,可以在viewer.js中搜索 file origin does not match viewer 把這幾行代碼注釋掉:
// if (fileOrigin !== viewerOrigin) { // throw new Error('file origin does not match viewer\'s'); // }