最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - pdf.js failing on getDocument - Stack Overflow

programmeradmin5浏览0评论
  • browser: Chrome
  • environment: grails app localhost

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.

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 FIDDLE and 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.

  • browser: Chrome
  • environment: grails app localhost

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.

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 FIDDLE and 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.

Share Improve this question edited Dec 31, 2014 at 20:01 btm1 asked Dec 31, 2014 at 17:23 btm1btm1 3,8562 gold badges24 silver badges26 bronze badges 7
  • 1 What utilities.base64ToBinary does return? If it isn't Uint8Array then it will not work. Also, add second error callback to the then call, which will show reason of failure. – async5 Commented Dec 31, 2014 at 17:57
  • @async5 I've edited my question to include the function i'm using to create the Uint8Array it's def creating the array correctly. The same error is given when i use the URL supplied in the fiddle example too so something else is going on. Let me try the error function and see what the yields. – btm1 Commented Dec 31, 2014 at 19:46
  • @async5 what is the format for the error function? Is it like this? : PDFJS.getDocument(data).then(function(){}).error(function(data){}); That doesn't seem to work. – btm1 Commented Dec 31, 2014 at 19:52
  • PDFJS.getDocument(data).then(function() {}, function(reason){}) -- see Promise specification for details – async5 Commented Dec 31, 2014 at 19:57
  • Is there is a reason you specify PDFJS.disableWorker = true; ? Maybe for the same reason you have to set PDFJS.workerSrc ? – async5 Commented Dec 31, 2014 at 20:01
 |  Show 2 more comments

2 Answers 2

Reset to default 16

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.

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.

Instead they should just make it clear and require that you do in fact point PDFJS.workerSrc = "(your path)/pdf.worker.js"

Here is the short answer : define PDFJS.workerSrc at the begining of your code.

PDFJS.workerSrc = "(your path)/pdf.worker.js"

see the exemple on the documentation : https://mozilla.github.io/pdf.js/examples/#interactive-examples

发布评论

评论列表(0)

  1. 暂无评论