Count pages in a PDF file using Imagemagick - PHP

I am using PHP 5 with Apache on my Windows Vista strong> PC. I already have Imagemagick installed and configured. I want to count the total number of pages in a pdf file using imagick .

I have one solution here , but I do not know how to open a pdf file in the form of text and counting pages.

does anyone give me a clear solution for counting pages using imagemagick like

identify -format %n testfile.pdf

From a Google search, I found some workarounds or examples;

  • imagick(identify -format %n testfile.pdf)
  • identify -density 12 -format "%p" testfile.pdf
  • identify -format %n testfile.pdf

I do not know how to use this material.

+5
php pdf imagemagick imagick
Sep 18 '11 at 15:59
source share
3 answers

I decided to use it;

exec("identify -format %n $file")

+2
Sep 24 2018-11-11T00:
source share

Instead of using "identify -format %n $file" (which can turn out to be extremely slow for complex or multi-page PDF files), you should use the right job tool , pdfinfo :

 exec("pdfinfo $file | grep Pages: | awk '{print $2}'") 

which is several times faster ...

+12
Aug 13 2018-12-12T00:
source share

From the specified page , here is a sample code to get the number of pages:

 <?php public function getNumPagesInPDF(array $arguments = array()) { @list($PDFPath) = $arguments; $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)); } ?> 
0
Sep 18 '11 at 16:05
source share



All Articles