1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > vue2.6使用pdf.js预览pdf文件-亲测有效

vue2.6使用pdf.js预览pdf文件-亲测有效

时间:2023-08-10 20:12:48

相关推荐

vue2.6使用pdf.js预览pdf文件-亲测有效

最近有个需求是页面上预览pdf,但是用了 iframe和embed都不行,会直接下载,于是决定用pdf.js来做。步骤如下

1.下载pdf.js 文件

地址:/mozilla/pdf.js

2.将下载的build和web文件放在一个文件夹中,我取的名是pdf,然后将这个文件夹放到vue工程的public文件下

3.页面中的使用

//页面可使用iframe或embed标签<embed :src="pdfSrc" type="application/pdf" width=1000 height=800 ></embed>loadPDF(url) {// url即为pdf地址this.pdfSrc = '/pdf/web/viewer.html?file=' + url;},

此时,可能会报 file origin does not match viewer’s" 的错误,是跨域的原因

只需要将web文件中的viewer.js文件中的这段代码注释了就可以啦

// if (origin !== viewerOrigin && protocol !== "blob:") {// throw new Error("file origin does not match viewer's");// }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。