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
2009-07-08 13:45:50
Keep in mind that all of those functions are part of the PDFlib extension which is a commercial ($$$) product.
Mark Biek
2009-07-08 13:52:02
Completly right, I forgot that, thank you
Kovu
2009-07-08 13:52:59
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
2009-07-08 14:33:59
+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
2009-07-08 14:25:31
Yep, this is the simplest method if you aren't going to use a third party library.
Rowan
2009-07-09 02:37:32
+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
2009-10-08 08:47:10