How to get the number of pages of a .PDF uploaded by user?

sparkle picture sparkle · Apr 20, 2012 · Viewed 37.9k times · Source

I have a file input, and before "uploading" i need to calculate the number of pages of that .pdf in JAVASCRIPT (eg. JQuery...)

Answer

Sid Thakur picture Sid Thakur · May 30, 2014

In case you use pdf.js you may reference an example on github ('.../examples/node/getinfo.js') with following code that prints number of pages in a pdf file.

const pdfjsLib = require('pdfjs-dist');
...
pdfjsLib.getDocument(pdfPath).then(function (doc) {
    var numPages = doc.numPages;
    console.log('# Document Loaded');
    console.log('Number of Pages: ' + numPages);
}