I need a way to count the number of pages of a PDF in PHP. I've done a bit of Googling and the only things I've found either utilize shell/bash scripts, perl, or other languages, but I need something in native PHP. Are there any libraries or examples of how to do this?
php function count_pdf_pages($pdfname) { $pdftext = file_get_contents($pdfname); $num = preg_match_all("/\/Page\W/", $pdftext, $dummy); return $num; } $pdfname = 'example. pdf'; // Put your PDF path $pages = count_pdf_pages($pdfname); echo $pages; ?>
In Adobe Acrobat Pro, go to file > create PDF > merge files into a single PDF. Then add files and select the files you want. Click combine, and see how many pages are in the final PDF.
If using Linux, this is much faster than using identify
to get the page count (especially with a high number of pages):
exec('/usr/bin/pdfinfo '.$tmpfname.' | awk \'/Pages/ {print $2}\'', $output);
You do need pdfinfo installed.
I know this is pretty old... but if it's relevant to me now, it can be relevant to others too.
I just worked out this method of getting page numbers, as the methods listed here are inefficient and extremely slow for large PDFs.
$im = new Imagick(); $im->pingImage('name_of_pdf_file.pdf'); echo $im->getNumberImages();
Seems to be working great for me!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With