问题描述
尝试在本地Apache服务器中使用PDF JS并在控制台中收到以下错误:
Trying to use PDF JS in a local Apache server and receiving the following error in console:
Uncaught Error: No PDFJS.workerSrc specified
这很奇怪,因为我要遵循示例在此处 http://mozilla.github上指定的所有内容.io/pdf.js/examples/.
This is very weird, because I'm following all that the examples specifies here http://mozilla.github.io/pdf.js/examples/.
我的主文件夹中有一个名为 file.pdf
的示例文件,我只是尝试显示它.我通过使用带有 file
参数的iframe来做到这一点:
I have, in my main folder, a sample file called file.pdf
and I'm just trying to display it. I did it by using a iframe with a file
parameter:
<iframe src="./web/viewer.html?file=http://localhost:99/PDF/arquivo.pdf" width="1800px" height="900px" />
现在我正在尝试使用JavaScript API来显示它.我正在尝试:
And now I'm trying to use the JavaScript API to display it. I'm trying to do:
<!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
,则会收到:
If I try to include pdf.worker.js
manually, I receive:
GET http://localhost:99/PDF/build/pdf.worker.worker.js 404 (Not Found)
因为它以编程方式包括 pdf.worker.js .
because it programmatically includes 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
我需要手动指定 pdf.worker.js 吗?拜托,我该怎么解决呢?
Do I need to manually specify pdf.worker.js?Please, what can I try to solve this?
非常感谢您!
(*)-我发现缺少很好的内容,也没有很好解释的PDF.JS文档.
(*) - I can see a lack of good content and a well explained documentation of PDF.JS.
推荐答案
我遇到了类似的错误,并通过在pdf.js的末尾显式指定pdf.worker.js来解决此问题.
I had a similar error and I fixed it by specifying the pdf.worker.js explicitly at the end of the pdf.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';
}
这篇关于未指定PDFJS.workerSrc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!