Posted By

traeregan on 11/10/09


page pdf count

Versions (?)

Who likes this?

8 people have marked this snippet as a favorite


Count The Number of Pages in a PDF

 / Published in: PHP

  1. METHOD 1
  2. ImageMagick: identify -density 12 -format "%p" image.pdf
  4. METHOD 2
  5. FPDI (when setting the source file you get back the page count):
  8. METHOD 3
  9. Open the pdf as a text file and count the number of times "/Page" occurs.
  10. function count_pages($pdfname) {
  11. $pdftext = file_get_contents($pdfname);
  12. $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
  13. return $num;
  14. }

Report this snippet  


RSS Icon Subscribe to comments
Posted By: fugue on November 10, 2009

If you're using Linux, you can also try pdfinfo:

pdfinfo myfile.pdf | awk '/Pages/ {print $2}'

Using pdfinfo gives you access to a lot of information on the document.

Posted By: traeregan on November 15, 2009

Thank you, fugue, that ended up being what I went with. Much appreciated!

Posted By: adrianbj on February 20, 2010

Does anyone know how to duplicate "" using PHP's imagick?

Posted By: adrianbj on February 20, 2010

Nevermind - it is: getNumberImages()

Posted By: adrianbj on February 21, 2010

Oh, and in case someone using imagemagick at the command is confused by the output of identify -density 12 -format "%p" image.pdf, try identify -format "%n" image.pdf

Posted By: adrianbj on February 21, 2010

Finally, sorry for the weird post with that url - I meant to write "identify -density 12 -format "%p" image.pdf" - I guess I pasted the wrong contents from my clipboard

Posted By: JonyGreen on October 9, 2015

I'm not a developer, i always use this free online pdf to image converter

You need to login to post a comment.