Javascript 未指定 PDFJS.workerSrc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26101071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:20:45  来源:igfitidea点击:

No PDFJS.workerSrc specified

javascriptpdf.js

提问by Marcelo Camargo

Trying to use PDF JS in a local Apache server and receiving the following error in console:

尝试在本地 Apache 服务器中使用 PDF JS 并在控制台中收到以下错误:

Uncaught Error: No PDFJS.workerSrc specified

This is very weird, because I'm following all that the examples specifies here http://mozilla.github.io/pdf.js/examples/.

这很奇怪,因为我正在关注示例在http://mozilla.github.io/pdf.js/examples/ 中指定的所有内容。

I have, in my main folder, a sample file called file.pdfand I'm just trying to display it. I did it by using a iframe with a fileparameter:

我的主文件夹中有一个示例文件file.pdf,我只是想显示它。我是通过使用带有file参数的 iframe 做到的:

<iframe src="./web/viewer.html?file=http://localhost:99/PDF/arquivo.pdf" width="1800px" height="900px" />

And now I'm trying to use the JavaScript API to display it. I'm trying to do:

现在我正在尝试使用 JavaScript API 来显示它。我正在尝试做:

<!DOCTYPE html>
<html>
    <head>
        <script src="./build/pdf.js" type="text/javascript"></script>       
        <script type="text/javascript">
            PDFJS.getDocument('arquivo.pdf').then(function(pdf) {
                // Here I use it
            })
        </script>
    </head>
    <body>
    </body>
</html>

If I try to include pdf.worker.jsmanually, I receive:

如果我尝试pdf.worker.js手动包含,我会收到:

GET http://localhost:99/PDF/build/pdf.worker.worker.js 404 (Not Found)

because it programmatically includes pdf.worker.js.

因为它以编程方式包含pdf.worker.js

With the sample code I posted here, I receive a log and an error:

使用我在这里发布的示例代码,我收到一个日志和一个错误:

Error: No PDFJS.workerSrc specified pdf.js:249
    at error (http://localhost:99/PDF/build/pdf.js:251:15)
    at Object.WorkerTransport (http://localhost:99/PDF/build/pdf.js:2305:9)
    at Object.getDocument (http://localhost:99/PDF/build/pdf.js:1805:15)
    at http://localhost:99/PDF/:6:10 pdf.js:251
Warning: Unsupported feature "unknown" pdf.js:234
Uncaught Error: No PDFJS.workerSrc specified

Do I need to manually specify pdf.worker.js? Please, what can I try to solve this?

我需要手动指定pdf.worker.js吗?请问,我可以尝试什么来解决这个问题?

Thank you so much!

非常感谢!

(*) - I can see a lack of good content and a well explained documentation of PDF.JS.

(*) - 我可以看到 PDF.JS 缺乏好的内容和解释清楚的文档。

回答by Will

I had a similar error and I fixed it by specifying the pdf.worker.js explicitly at the end of the pdf.js

我有一个类似的错误,我通过在 pdf.js 的末尾明确指定 pdf.worker.js 来修复它

if (!PDFJS.workerSrc && typeof document !== 'undefined') {
  // workerSrc is not set -- using last script url to define default location
  ****** I have no clue what the code below hope to accomplish ********
  ****** How can it locate the script container by assuming it ********
  ****** always would be at the end of <body> or <head> ????   ********
  PDFJS.workerSrc = (function () {
    'use strict';
    var scriptTagContainer = document.body ||
                             document.getElementsByTagName('head')[0];
    var pdfjsSrc = scriptTagContainer.lastChild.src;
    return pdfjsSrc && pdfjsSrc.replace(/\.js$/i, '.worker.js');
  })();


  ****** Here I just hardcode the location of the needed file *********
  ****** This is the part that makes it work.                 *********
  ****** Obviously, tailor this to the same path of pdf.js    *********
  PDFJS.workerSrc = '/static/js/pdf.worker.js';
}

回答by ProgrammerGuy

Include compatibility.js to fix the "Uncaught Error: No PDFJS.workerSrc specified" error on IE11.

包含 compatible.js 以修复 IE11 上的“未捕获错误:未指定 PDFJS.workerSrc”错误。

https://github.com/mozilla/pdf.js/blob/master/src/shared/compatibility.js

https://github.com/mozilla/pdf.js/blob/master/src/shared/compatibility.js

<script src="compatibility.js"></script>
<script src="pdf.js"></script>

compatibility.js implements any missing functionality required by PDFJS.

compatible.js 实现了 PDFJS 所需的任何缺失功能。

Note:It should be loaded beforePDFJS, not after.

注意:它应该PDFJS之前加载,而不是之后。

回答by R.K.Saini

Specify psd.worker.js file path in your page where you want to use the pdf.js file (viewer.html in case you are using viewer.html come with distribution bundle) like this. It work for me.

在您要使用 pdf.js 文件的页面中指定 psd.worker.js 文件路径(如果您使用的是带有分发包的 viewer.html,则为 viewer.html),就像这样。它对我有用。

<script>
    PDFJS.workerSrc ='path to psd.worker.js';

回答by Pankaj Upadhyay

I added below code in end of pdf.js and working fine

我在 pdf.js 的末尾添加了以下代码并且工作正常

if (!PDFJS.workerSrc && typeof document !== 'undefined') {
  PDFJS.workerSrc = (function () {
    'use strict';
    var scriptTagContainer = document.body ||
                             document.getElementsByTagName('head')[0];
    var pdfjsSrc = scriptTagContainer.lastChild.src;
    return pdfjsSrc && pdfjsSrc.replace(/\.js$/i, '.worker.js');
  })();
  PDFJS.workerSrc = 'pdf.worker.js';
}

回答by Arsh Kalsi

Go to pdf.js

转到 pdf.js

search function getWorkerSrc()

搜索函数 getWorkerSrc()

replace this lines

替换此行

pdfjsFilePath = "YOUR_PATH_TO_JS_FILE/pdf.worker.js";
if (pdfjsFilePath) {
  return pdfjsFilePath;
}