Skip to content

Instantly share code, notes, and snippets.

@hjue
Created October 18, 2012 09:00
Show Gist options
  • Select an option

  • Save hjue/3910561 to your computer and use it in GitHub Desktop.

Select an option

Save hjue/3910561 to your computer and use it in GitHub Desktop.
getNumPagesInPDF
<?php
echo getNumPagesInPDF('iOS-zhangchao.pdf');
function getNumPagesInPDF($PDFPath)
{
$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]);
}
echo (($secondValue != 0) ? $secondValue : max($firstValue, $secondValue));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment