使用Imagemagick-PHP计算PDF文件中的页面数
我在我的Windows Vista PC中使用带有Apache的PHP5。我已经安装并配置了使用Imagemagick-PHP计算PDF文件中的页面数,php,pdf,imagemagick,imagick,Php,Pdf,Imagemagick,Imagick,我在我的Windows Vista PC中使用带有Apache的PHP5。我已经安装并配置了Imagemagick。我想使用imagick计算pdf文件中的总页数 我找到了一个解决方案,但不知道如何以文本形式打开pdf文件并计算页面数 有人给我一个使用imagemagick计算页面的清晰解决方案,比如 标识-格式%n testfile.pdf 通过谷歌搜索,我找到了一些变通方法或示例 imagick(标识-格式%n testfile.pdf) identification-density 12-
Imagemagick
。我想使用imagick
计算pdf文件中的总页数
我找到了一个解决方案,但不知道如何以文本形式打开pdf文件并计算页面数
有人给我一个使用imagemagick计算页面的清晰解决方案,比如
标识-格式%n testfile.pdf
通过谷歌搜索,我找到了一些变通方法或示例
imagick(标识-格式%n testfile.pdf)
identification-density 12-format“%p”testfile.pdf
identify-格式%n testfile.pdf
<?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));
}
?>
我用
exec(“identify-format%n$file”)
而不是使用“identify-format%n$file”
(对于复杂的或多页的PDF来说,这可能会非常慢)。您应该使用适合该作业的工具,pdfinfo
:
exec("pdfinfo $file | grep Pages: | awk '{print $2}'")
速度快了好几个数量级……这对我不起作用(我设置$PDFPath=“test.pdf”;但不起作用。我该怎么办?这个线程可能会帮助你:这个解决方案不会在所有情况下都起作用。对我来说,这个脚本不适用于PDF 1.6,但适用于PDF 1.4和1.5版本。PDFlib库自2010年以来不再维护。但最糟糕的是,它是商业领域,而不是公共领域。对于多页PDF,尤其是特别是对于那些页面上有更复杂图形的页面…有用的指针,但我很难弄清楚如何在我的系统中获得pdfinfo。最后:Debian/Ubuntu:sudo aptitude安装poppler utils;OSX:brew安装poppler。