gpt4 book ai didi

javascript - 未指定 PDFJS.workerSrc

转载 作者:可可西里 更新时间:2023-11-01 01:21:42 24 4
gpt4 key购买 nike

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

Uncaught Error: No PDFJS.workerSrc specified

这很奇怪,因为我遵循示例在此处指定的所有内容 http://mozilla.github.io/pdf.js/examples/ .

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

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

现在我正尝试使用 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>

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

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

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

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

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

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

非常感谢!

(*) - 我可以看到缺少好的内容和解释清楚的 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';
}

关于javascript - 未指定 PDFJS.workerSrc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26101071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com