pdf文件中的页数

时间:2009-07-08 13:42:08

标签: php pdf

有谁知道如何使用php计算pdf文件中的页数?谢谢!

3 个答案:

答案 0 :(得分:19)

根据R Ubben的回答,我发现以下PHP代码可以提供良好的结果:

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

\W匹配任何非字母数字字符,并排除/Pages/PageMode等内容。

答案 1 :(得分:5)

PDF将页面存储在树中。 “/ Pages”对象可以有“/ Parent”和“/ Kids”条目,后跟“/ Count”。您不能对“/ Count”条目求和,因为Kid可能是另一个Pages节点。 “/ Page”对象是叶子。

将pdf作为文本文件打开,并计算文件中出现“/ Page”(而不是“/ Pages”)的次数。大多数时候这应该是正确的。

答案 2 :(得分:0)

exec('pdftops ' . $filename . ' - | grep showpage | wc -l', $output);

请参阅类似的问题和答案:

Count the number of pages in a PDF in only PHP

相关问题