项目场景:
pdf.js(当前版本:4.0.269)似乎是不能直接用在非工程化的项目上。
问题描述
使用 pdf.js
自带的预览界面 viewer.html 预览:
window.open("../pdfjs/web/viewer.html?file=userBook.pdf");
提示以下错误:
pdf.mjs:1
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
viewer.mjs:1
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
viewer.html:1
Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received
原因分析:
官网似乎没有提到怎么打包出适合非工程化项目的包。有知道怎么弄的可以交流一下。
解决方案:
最后找了到这个项目PDFViewer。应该是pdf.js
源码修改得来的,版本应该比较旧。但我用于简单预览PDF实测没有问题。