简体   繁体   中英

PHP Number of pages in a PDF file via ImageMagick

I am using the following code to get the number of pages in a PDF file.

$img = new imagick();
$img->setResolution(200,200);
$img->readImage("{$FileName}");
$NumberOfPages = $img->getNumberImages();
echo "$NumberOfPages";

The code works but is very slow (0.5 seconds per page. A 29 page PDF takes 15 seconds to deliver the result).

Am I missing something? There must be a faster way to:

1) Get the number of pages in a pdf

2) Convert a single page to an image

Please note that 2) is possible only after 1) is accomplished. No point in converting, say, page 39, when the PDF only has 16 pages.

Any help would be much appreciated.

David

I tried out your code but it did not work with the PDFs I have. I use Free PDF to create PDFs. It could be that the resulting PDFs are not linearized.

I found some code under question 1098156 and it seems to work ok with the PDFs I have:

function count_pages($pdfname) {
$pdftext = file_get_contents($pdfname);
$num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
return $num;
}

According to "Detect Pages in PDF Files". I recommend to not use imagemagick whenever possible for that simple task to detect pages of an PDF file. PDF has the "Linearized" layout feature -> http://labs.appligent.com/pdfblog/linearization/ to detect some basic PDF metadata from the beginning of the file. Here is a really simple code to get number of pages, i tested it with randomly 10 PDF files from the web, worked everytime and should be a way faster than the one with imagemagick.

This is just a quick code, not really well tested but should give you some ideas to do that job in PHP only. Maybe not all your PDF files have guaranted that Linearized layout.

$pdfcontent = file_get_contents("test.pdf", NULL, NULL, 0, 300);
preg_match("~Linearized.*?\/N ([0-9]+)~s", $pdfcontent, $pages);
if(isset($pages[1])){
    echo "Pages ".$pages[1];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM