tags:

views:

290

answers:

3

Does anyone know how I can count the number of pages in a pdf file using php? Thanks!

+1  A: 

Here is an overview of all what you want and more: http://www.php.net/manual/en/ref.pdf.php

Included a function where you can read the number of pages.

Kovu
Keep in mind that all of those functions are part of the PDFlib extension which is a commercial ($$$) product.
Mark Biek
Completly right, I forgot that, thank you
Kovu
We happen to have the commercial license for PDFLib, but I didn't see a function that would return the number of pages. There's a couple of suggestions in the comments on that page using php to read the contents and do a regular expression search and count, but I wonder if thats accurate all the time.
Julie
+4  A: 

PDFs store pages in a tree. "/Pages" objects can have a "/Parent" and "/Kids" entries, followed by a "/Count". You can't sum the "/Count" entries because a Kid might be another Pages node. The "/Page" object is the leaf.

Open the pdf as a text file and count the number of times "/Page" (not "/Pages") appears in the file. That should be correct most of the time.

R Ubben
Yep, this is the simplest method if you aren't going to use a third party library.
Rowan
Awesome. Very helpful, thanks!
Aseem Kishore
+2  A: 

Based on R Ubben's answer I found the following PHP code to give good results:

function count_pages($pdfname) {
  $pdftext = file_get_contents($pdfname);
  $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
  return $num;
}

\W matches any non-alphanumeric character and excludes things like /Pages, /PageMode etc.

fuenfundachtzig
Nice function. Thanks man.
dsplatonov