I have a file input, and before "uploading" i need to calculate the number of pages of that .pdf in JAVASCRIPT (eg. JQuery...)
-
1Check this out https://github.com/mozilla/pdf.js – elclanrs Apr 20 '12 at 21:20
-
Also, you can limit the size of the file that can be uploaded to your site, if you're worried about excessive page counts. – halfer Apr 22 '12 at 22:38
6 Answers
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);
})

- 2,665
- 3
- 24
- 46

- 798
- 1
- 7
- 10
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 );
}

- 2,657
- 26
- 24
-
4That 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. – mkl Aug 30 '16 at 08:54
-
i tested it on many pdf docs and it works. do you have any sample? – Sajjad Shirazi Aug 30 '16 at 09:13
-
4I 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](http://stackoverflow.com/q/30236358/1729265) etc. – mkl Aug 30 '16 at 15:44
-
2I am getting this message - Property 'match' does not exist on type 'string | ArrayBuffer'. Property 'match' does not exist on type 'ArrayBuffer'.ts(2339) – Vikash Yadav Jun 05 '19 at 13:23
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

- 1,955
- 15
- 26
You could also use pdf-lib
.
You will need to read the file from the input field and then make use of pdf-lib
to get the number of pages. The code would be like this:
import { PDFDocument } from 'pdf-lib';
...
const readFile = (file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
reader.readAsArrayBuffer(file);
});
}
const getNumPages =async (file) => {
const arrayBuffer = await readFile(file);
const pdf = await PDFDocument.load(arrayBuffer);
return pdf.getPages();
}
And then just get the number of pages of the attached file with:
const numPages = await getNumPages(input.files[0]);
being input
the variable which stores the reference to the DOM element of the file input.

- 315
- 3
- 11

- 2,674
- 24
- 17
In typescript class using Pdf-lib I use the following.
// getPAGE COUNT:
async getPageCount(formUrl: any): Promise<number>{
const LogPdfFields = [] as any[];
const formPdfBytes = await fetch(formUrl).then((res) => res.arrayBuffer());
const pdfDoc = await PDFDocument.load(formPdfBytes);
const pageCount = pdfDoc.getPageCount();
return pageCount;
}
Call as a promise

- 19
- 1
I think the API has changed a little since Tracker1 posted an answer. I tried Tracker1's code and saw this error:
Uncaught TypeError: pdfjsLib.getDocument(...).then is not a function
A small change fixes it:
const pdfjsLib = require('pdfjs-dist');
...
pdfjsLib.getDocument(pdfPath).promise.then(function (doc) {
var numPages = doc.numPages;
console.log('# Document Loaded');
console.log('Number of Pages: ' + numPages);
}

- 180
- 1
- 12