Skip to main content

Count PDF pages with php

function getNumPagesInPDF($PDFPath = NULL)
 {
 $stream = fopen($PDFPath, "r");
 $PDFContent = fread ($stream, filesize($PDFPath));
 if(!$stream || !$PDFContent)
 return false;

$firstValue = 0;
 $secondValue = 0;
 if(preg_match("/\/N\s+([0-9]+)/", $PDFContent, $matches)) {
 $firstValue = $matches[1];
 }

if(preg_match_all("/\/Count\s+([0-9]+)/s", $PDFContent, $matches))
 {
 $secondValue = max($matches[1]);
 }

return (($secondValue != 0) ? $secondValue : max($firstValue, $secondValue));
 }

Sorry, I don't know where I found that code - but it's really useful.