javascript pdf.js 在 getDocument 上失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27724295/
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
pdf.js failing on getDocument
提问by btm1
- browser: Chrome
- environment: grails app localhost
- 浏览器:Chrome
- 环境:grails 应用本地主机
I'm running a grails app on local host (which i know there's an issue with pdf.js and local file system)and instead of using a file: url which i know would fail i'm passing in a typed javascript array and it's still failing. To be correct it's not telling me anything but "Warning: Setting up fake worker." and then it does nothing.
我正在本地主机上运行 grails 应用程序(我知道 pdf.js 和本地文件系统存在问题),而不是使用文件:我知道会失败的 url 我正在传入一个类型化的 javascript 数组,它是仍然失败。正确地说,它没有告诉我任何事情,而是“警告:设置假工人”。然后它什么都不做。
this.base64ToBinary = function(dataURI) {
var BASE64_MARKER = ';base64,';
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for(i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
};
PDFJS.disableWorker = true; // due to CORS
// I convert some base64 data to binary data here which comes back correctly
var data = utilities.base64ToBinary(result);
PDFJS.getDocument(data).then(function (pdf) {
//nothing console logs or reaches here
console.log(pdf);
}).catch(function(error){
//no error message is logged either
console.log("Error occurred", error);
});
I'm wondering if I just don't have it set up correctly? Can I use this library purely on the client side by just including pdf.js or do I need to include viewer.js too? and also i noticed compatibility file... the set up isn't very clear and this example works FIDDLEand mine doesn't and I'm not understanding the difference. Also if I use the url supplied in that example it also says the same thing.
我想知道是不是我没有正确设置它?我可以只在客户端使用这个库,只包含 pdf.js 还是我也需要包含 viewer.js?而且我还注意到兼容性文件......设置不是很清楚,这个例子可以在FIDDLE 中工作,而我的没有,我不明白其中的区别。此外,如果我使用该示例中提供的 url,它也会说同样的事情。
回答by btm1
I get to answer my own question:
我要回答我自己的问题:
the documentation isn't clear at all. If you don't define PDFJS.workerSrc to point to the correct pdf.worker.js file than in pdf.js it tries to figure out what the correct src path is to the file and load it.
文档根本不清楚。如果您没有定义 PDFJS.workerSrc 来指向正确的 pdf.worker.js 文件,而不是在 pdf.js 中,它会尝试找出文件的正确 src 路径并加载它。
Their method however is pretty sketchy for doing this:
然而,他们的方法非常粗略地执行此操作:
if (!PDFJS.workerSrc && typeof document !== 'undefined') {
// workerSrc is not set -- using last script url to define default location
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');
})();
}
They only grab the last script tag in the head and assume that that is the right src to load the file instead of searching all the script tags for the src that contains "pdf.js" and using that as the correct one.
他们只获取头部的最后一个脚本标签,并假设这是加载文件的正确 src,而不是搜索包含“pdf.js”的 src 的所有脚本标签并将其用作正确的标签。
Instead they should just make it clear and require that you do in fact point PDFJS.workerSrc = "(your path)/pdf.worker.js"
相反,他们应该说清楚并要求您实际上点 PDFJS.workerSrc = "(your path)/pdf.worker.js"
回答by Anne Claire
Here is the short answer : define PDFJS.workerSrc at the begining of your code.
这是简短的回答:在代码的开头定义 PDFJS.workerSrc。
PDFJS.workerSrc = "(your path)/pdf.worker.js"
PDFJS.workerSrc = "(your path)/pdf.worker.js"
see the exemple on the documentation : https://mozilla.github.io/pdf.js/examples/#interactive-examples
请参阅文档中的示例:https: //mozilla.github.io/pdf.js/examples/#interactive-examples