跳到主要内容位置

pdf预览器: 1 篇

查看所有标签(分类)

vue3引入pdfjs-dist5.x预览器viewer报错问题解决及pdfjs-dist5.x使用教程

1 前言#

最近在做 pdf 在预览的功能,pdfjs-dist 插件使用了一下感觉还可以,具体就不用介绍了,能看到这篇文档的小伙伴想必都是研究过 pdfjs-dist(官方文档:https://github.com/mozilla/pdf.js)的。 我是在引入 pdfjs-dist 5.2.133 遇到的报错,下面开始 show time:

2 报错复现#

报错内容:

app.js:2498 Uncaught (in promise) Error: file origin does not match viewer's at validateFileURL (app.js:2498:10) at Object.run (app.js:960:28)

app.js:1507 加载 PDF 时发生错误。

PDF.js v5.2.133 (build: 4f7761353) Message: file origin does not match viewer's

image-20250519170550373

3 报错解决#

首先确认咱穿过来的这个 url 地址是放在浏览器里面是可以直接下载 pdf(接口没啥问题)的,这里 url 被转码了,不过没关系,用我们转码之前的 pdf 的 url 测试就可以了

我转码之前的 pdf url 是:http://127.0.0.1:7501/api/v1/user/getPdfFile?file=8c424181bb74b34199b0f02bc3b2b012.pdf

image-20250519171219097

重点:

更改 pdfjs-dist 的源码,因为里面有跨域相关的校验,咱前端不要这个

将下面这段咔咔注释掉,就 OK 了!

image-20250519171506892

正常展示效果如下:

image-20250519171823133

有的博客说,要在这里加一句file = decodeURIComponent(file) ,不过我试了加不加都不影响,估计是版本问题吧

image-20250519172045311

4 pdfjs-dist5.x 完整引用代码#

关键代码如下:

image-20250519172550077

pdf 预览器离线包下载:https://mozilla.github.io/pdf.js/getting_started/#download

关键代码块:

let currentFileUrl = ref('') currentFileUrl.value = encodeURIComponent( `http://127.0.0.1:7501/api/v1/user/getPdfFile?file=8c424181bb74b34199b0f02bc3b2b012.pdf` )
<iframe style="height: 100%; width: 100%" :src="`./lib/pdfjs-5.2.133-legacy-dist/web/viewer.html?file=${currentFileUrl}`">
</iframe>

5 参考#