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


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

Best Solution

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);