Number of pages in pdf file

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

+11
php pdf
Jul 08 '09 at 13:42
source share
3 answers

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.

+19
08 Oct '09 at 8:47
source share

PDF files store pages in a tree. "/ Pages" objects can have "/ Parent" and "/ Kids" entries followed by "/ Count". You cannot sum the "/ Count" entries, because Kid may be a different Pages node. The "/ Page" object is a sheet.

Open the pdf as a text file and count the number of times that "/ Page" appears in the file (rather than "/ Pages"). This should be correct in most cases.

+5
Jul 08 '09 at 14:25
source share
 exec('pdftops ' . $filename . ' - | grep showpage | wc -l', $output); 

See also related questions and answers:

Count the number of pages in PDF only in PHP

0
Oct 13 '12 at 15:03
source share



All Articles