Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find PDF page count with Node (on Windows)

I did a lot of research (I guess not enough?) and am trying to find an easy to use library to find the page count of a PDF using Node.js. The library would need to be usable on a Windows OS.

Anyone know how best to approach this? Worst case situation, I was thinking about doing something with PhantomJS and the PDF.js library.

Thanks for any help!!

like image 730
Justin Avatar asked May 08 '15 19:05

Justin


2 Answers

Since it's built on pdf.js, pdf2json it should work in windows.

I managed to find the page count of a test document like so:

var PDFParser = require('pdf2json');
var pdfParser = new PDFParser();

pdfParser.on('pdfParser_dataReady', function(data) {
    var doc = data.PDFJS && data.PDFJS.pdfDocument && data.PDFJS.pdfDocument.numPages;
    console.log('Number of pages:', doc);
});
// pdfParser.on('pdfParser_dataError', _.bind(_onPFBinDataError, self));

pdfParser.loadPDF('test.pdf');
like image 128
Andrew Lavers Avatar answered Sep 21 '22 12:09

Andrew Lavers


In its current version you get the total pages via pdf2json's Pages array:

stream.pipe(new PDFParser())
  .on('pdfParser_dataReady', (data) => {
    const pageCount = data && data.formImage && data.formImage.Pages && data.formImage.Pages.length ? data.formImage.Pages.length : 0;
    console.log(pageCount);
  })
like image 28
Julius B Avatar answered Sep 19 '22 12:09

Julius B