Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of pages in a PDF in only PHP [closed]

Tags:

php

pdf

I need a way to count the number of pages of a PDF in PHP. I've done a bit of Googling and the only things I've found either utilize shell/bash scripts, perl, or other languages, but I need something in native PHP. Are there any libraries or examples of how to do this?

like image 226
UnkwnTech Avatar asked Jul 17 '09 15:07

UnkwnTech


People also ask

How do I count the number of pages in a PDF using PHP?

php function count_pdf_pages($pdfname) { $pdftext = file_get_contents($pdfname); $num = preg_match_all("/\/Page\W/", $pdftext, $dummy); return $num; } $pdfname = 'example. pdf'; // Put your PDF path $pages = count_pdf_pages($pdfname); echo $pages; ?>

How do I count pages without opening a PDF?

In Adobe Acrobat Pro, go to file > create PDF > merge files into a single PDF. Then add files and select the files you want. Click combine, and see how many pages are in the final PDF.


2 Answers

If using Linux, this is much faster than using identify to get the page count (especially with a high number of pages):

exec('/usr/bin/pdfinfo '.$tmpfname.' | awk \'/Pages/ {print $2}\'', $output); 

You do need pdfinfo installed.

like image 185
stephangroen Avatar answered Oct 08 '22 16:10

stephangroen


I know this is pretty old... but if it's relevant to me now, it can be relevant to others too.

I just worked out this method of getting page numbers, as the methods listed here are inefficient and extremely slow for large PDFs.

$im = new Imagick(); $im->pingImage('name_of_pdf_file.pdf'); echo $im->getNumberImages(); 

Seems to be working great for me!

like image 33
user678415 Avatar answered Oct 08 '22 14:10

user678415