douxiong3234 2014-12-28 17:22
浏览 216
已采纳

PHP通过ImageMagick的PDF文件中的页数

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

  • 写回答

2条回答 默认 最新

  • dsb12300 2014-12-31 08:52
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?