我需要一种方法来计算PHP中PDF的页数.我做了一些谷歌搜索,我发现的唯一的东西要么使用shell/bash脚本,perl或其他语言,但我需要原生PHP的东西.有没有图书馆或如何做到这一点的例子?
ste*_*oen 25
如果使用Linux,这比使用identify获取页面计数要快得多(特别是对于大量页面):
exec('/usr/bin/pdfinfo '.$tmpfname.' | awk \'/Pages/ {print $2}\'', $output);
Run Code Online (Sandbox Code Playgroud)
你确实需要安装pdfinfo.
Tra*_*ale 14
您可以使用PHP的ImageMagick扩展.ImageMagick了解PDF,您可以使用该identify命令提取页数.PHP函数是Imagick :: identifyImage().
use*_*415 13
我知道这已经很老了......但如果它现在与我相关,它也可能与其他人相关.
我刚刚制定了这种获取页码的方法,因为这里列出的方法效率低,而且对于大型PDF来说速度极慢.
$im = new Imagick();
$im->pingImage('name_of_pdf_file.pdf');
echo $im->getNumberImages();
Run Code Online (Sandbox Code Playgroud)
似乎对我有用!
小智 10
我实际上采用了综合方法.由于我在我的服务器上禁用了exec,我想坚持使用基于PHP的解决方案,所以最终得到了这个:
码:
function getNumPagesPdf($filepath){
$fp = @fopen(preg_replace("/\[(.*?)\]/i", "",$filepath),"r");
$max=0;
while(!feof($fp)) {
$line = fgets($fp,255);
if (preg_match('/\/Count [0-9]+/', $line, $matches)){
preg_match('/[0-9]+/',$matches[0], $matches2);
if ($max<$matches2[0]) $max=$matches2[0];
}
}
fclose($fp);
if($max==0){
$im = new imagick($filepath);
$max=$im->getNumberImages();
}
return $max;
}
Run Code Online (Sandbox Code Playgroud)
如果由于没有Count标签而无法解决问题,那么它会使用imagick php扩展.我做双重方法的原因是因为后者很慢.