Pdf.js: Worker loaded every time the document changes

Created on 3 Mar 2016  路  8Comments  路  Source: mozilla/pdf.js

I have a simple viewer based mostly on pdfjs-dist.
The viewer has a document list and it displays one document at a time.
However, when I change the document, the worker is requested and downloaded again.
Can't the worker just be downloaded once and use for subsequent document changes?

Most helpful comment

For use with multiple requests:

var worker = new PDFJS.PDFWorker('test1');
var loadingTask1 = PDFJS.getDocument({url: url1, worker: worker});
var loadingTask2 = PDFJS.getDocument({url: url2, worker: worker});
...
loadingTask1.destroy();
loadingTask2.destroy();
worker.destroy();

All 8 comments

The DocumentInitParameters https://github.com/mozilla/pdf.js/blob/master/src/display/api.js#L264 has worker property. Create PDFWorker object and re-use it across getDocument calls. Closing as answered.

@yurydelendik Is there a way to access the worker created?
If I create one it requests and downloads the worker again but not for subsequent document changes.

PDFWorker is an abstraction to create and hold reference to the worker (fake or Web Worker).

If I create one it requests and downloads the worker again but not for subsequent document changes.

I don't understand what does this mean. Perhaps the test source code will make it clear how it shall be implemented https://github.com/mozilla/pdf.js/blob/master/test/unit/api_spec.js#L260

For use with multiple requests:

var worker = new PDFJS.PDFWorker('test1');
var loadingTask1 = PDFJS.getDocument({url: url1, worker: worker});
var loadingTask2 = PDFJS.getDocument({url: url2, worker: worker});
...
loadingTask1.destroy();
loadingTask2.destroy();
worker.destroy();

Thanks for your help.
I think my issue is that I can't create the PDFWorker until after the first rendered document because I'm using PDFJS.workerSrc and I think it is not loaded until needed.

I think my issue is that I can't create the PDFWorker until after the first rendered document because I'm using PDFJS.workerSrc and I think it is not loaded until needed.

It is still confusing, to render a document you need PDFJS.workerSrc set. FYI getDocument call will create new PDFWorker object by itself if worker is not provided, so it's no different then create worker yourself and pass it to the getDocument.

Thanks! It was a promise issue in my code.

Can you post your code which solved this issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PeterNerlich picture PeterNerlich  路  3Comments

patelsumit5192 picture patelsumit5192  路  3Comments

xingxiaoyiyio picture xingxiaoyiyio  路  3Comments

smit-modi picture smit-modi  路  3Comments

jigskpatel picture jigskpatel  路  3Comments