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

53,577

Solution 1

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

Solution 2

and a pure javascript solution:

var input = document.getElementById("files");
var reader = new FileReader();
reader.readAsBinaryString(input.files[0]);
reader.onloadend = function(){
    var count = reader.result.match(/\/Type[\s]*\/Page[^s]/g).length;
    console.log('Number of Pages:',count );
}

Solution 3

As has been stated in the other answers, something like pdf.js is be what you are looking for. I've taken a look at the API and it does include a numPages() function to return the total number of pages. It also seems to count pages for me when viewing the demo page from Mozilla.

It depends if you are able to use modern browsers and experimental technology for your solution. pdf.js is very impressive, but it is still experimental according to the github page .

If you are able to count the pages on the server after uploading, then you should look at pdftools or similar.

Something like pdftools --countpages is what you are looking for

Share:
53,577
sparkle
Author by

sparkle

Updated on January 18, 2022

Comments

  • sparkle
    sparkle over 2 years

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

    • elclanrs
      elclanrs about 12 years
      Check this out github.com/mozilla/pdf.js
    • halfer
      halfer about 12 years
      Also, you can limit the size of the file that can be uploaded to your site, if you're worried about excessive page counts.
  • mkl
    mkl almost 8 years
    That regular expression works for documents fulfilling a number of assumptions and in particular is likely to fail for documents with multiple revisions or intense object stream use.
  • Sajjad Shirazy
    Sajjad Shirazy almost 8 years
    i tested it on many pdf docs and it works. do you have any sample?
  • mkl
    mkl almost 8 years
    I could create any number of samples: As you surely are aware, the PDF format at byte level allows to add comments; thus, I could simply add any number of comments containing a "/Type /Page" to an existing document and so make the regular expression return a too high result. But you probably don't mean constructed examples but real-world ones. For that you might want to look at questions like this one etc.
  • Vikash Yadav
    Vikash Yadav about 5 years
    I am getting this message - Property 'match' does not exist on type 'string | ArrayBuffer'. Property 'match' does not exist on type 'ArrayBuffer'.ts(2339)
  • Matthijs
    Matthijs over 3 years
    One closing ) is missing as the last character.