PDF.js报错workerSrc修复
在项目中使用PDF.js展示PDF报错
pdf.js:8715 Uncaught (in promise) Error: No "GlobalWorkerOptions.workerSrc" specified. at getWorkerSrc (pdf.js:8715) at PDFWorker_initialize [as _initialize] (pdf.js:8798) at new PDFWorker (pdf.js:8776) at Object.getDocument (pdf.js:7991) at s.loadFile (PDF.vue:88) at mysubinfos.vue:295
解决方法
1、// 必须要指定workerSrc属性.
pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
2、 修改 api.js 中的 getWorkerSrc() 函数
//默认实现
function getWorkerSrc() {
if (GlobalWorkerOptions.workerSrc) {
return GlobalWorkerOptions.workerSrc;
}
if (typeof workerSrc !== 'undefined') {
return workerSrc;
}
if (typeof PDFJSDev !== 'undefined' &&
PDFJSDev.test('PRODUCTION && !(MOZCENTRAL || FIREFOX)') &&
pdfjsFilePath) {
return pdfjsFilePath.replace(/(\.(?:min\.)?js)(\?.*)?$/i, '.worker$1$2');
}
throw new Error('No "GlobalWorkerOptions.workerSrc" specified.');
}
//修改后
function getWorkerSrc() {
return pdfjsFilePath.replace('pdf.js', 'pdf.worker.js');}