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

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条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗