Search code examples
javascriptloaderpdf.js

How to know the percent loaded of a PDF file on PDFjs


Is there any way to know the percentage of loading a PDF file in PDFJS?

I curently have the following code:

var filename = 'your_pdf_file.pdf';
PDFJS.getDocument(filename).then(function (pdf) {
    console.info('File loaded:' + filename)
}).catch(function(pdf){
    console.error('File not found:' + filename)
});

The problem is that I'm only able to know when the file was loaded.


Solution

  • Use progressCallback parameter in getDocument:

    var progressCallback = function (progress) {
        // progress object contains "total" and "loaded" properties
    }
    
    var filename = 'your_pdf_file.pdf';
    PDFJS.getDocument(filename, null, null, progressCallback).then(function (pdf) {
        console.info('File loaded:' + filename)
    }).catch(function(pdf){
        console.error('File not found:' + filename)
    });