11

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

Julie
  • 209
  • 1
  • 4
  • 12

3 Answers3

19

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
  • 7,952
  • 13
  • 62
  • 87
  • I'd appreciate if you could elaborate on the meaning and the expected contents of the $dummy variable. Otherwise, a great answer! Thank you. – Dušan Rychnovský Apr 17 '13 at 12:25
  • Before php version 5.4.0 [`preg_match_all`](http://www.php.net/manual/en/function.preg-match-all.php) had to be called with three parameters and would store in the third an array with all the matches. As we're only interested in the number of matches we don't use this dummy parameter, and for php 5.4.0 and later versions it can be left out. – fuenfundachtzig Apr 17 '13 at 23:37
  • 2
    I'd like to point out that there are some cases where this does not work. Some third party pdf builders do not include "Page" in their byte structure. I have been using this code for months without issue until today when it was brought to my attention. In this case the product building the pdfs was made by the folks at http://www.antennahouse.com/ (no I don't work there, nor had I heard of them until today) – kyle Sep 22 '14 at 22:55
5

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
  • 2,225
  • 16
  • 8
0
exec('pdftops ' . $filename . ' - | grep showpage | wc -l', $output);

See as well the similar question and answers:

Count the number of pages in a PDF in only PHP

Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743