最佳答案
我花了很多时间寻找一种快速简单,但大部分是 准确无误的方法来获得 PDF 文档中的页数。因为我在一家图形打印和复制公司工作,这家公司经常使用 PDF 文档,所以在处理文档之前,必须精确地知道文档中的页数。PDF 文档来自许多不同的客户端,因此它们不是由相同的应用程序生成的,也不使用相同的压缩方法。
下面是我找到的一些 不足或者简单的 < strong > NOT working 的答案:
Imagick 需要大量的安装,apache 需要重新启动,当我最终让它工作时,它花了惊人的长时间处理(每个文档2-3分钟) ,它总是返回每个文档中的 ABc0页面(到目前为止还没有看到一个 Imagick 的工作副本) ,所以我把它扔掉了。这就是 getNumberImages()
和 identifyImage()
方法。
FPDI 易于使用和安装(只需提取文件并调用 PHP 脚本即可) ,但是中的许多压缩技术都不受 FPDI 的支持。然后返回一个错误:
FPDF 错误: 该文档(test _ 1.pdf)可能使用了 FPDI 附带的免费解析器不支持的压缩技术。
这将在流中打开 PDF 文件,并搜索某种类型的字符串,包含页数或类似的内容。
$f = "test1.pdf";
$stream = fopen($f, "r");
$content = fread ($stream, filesize($f));
if(!$stream || !$content)
return 0;
$count = 0;
// Regular Expressions found by Googling (all linked to SO answers):
$regex = "/\/Count\s+(\d+)/";
$regex2 = "/\/Page\W*(\d+)/";
$regex3 = "/\/N\s+(\d+)/";
if(preg_match_all($regex, $content, $matches))
$count = max($matches);
return $count;
/\/Count\s+(\d+)/
(查找 /Count <number>
)不起作用,因为只有少数文档中包含参数 /Count
,所以大多数时候它不返回任何内容。来源。/Page<number>
)得不到页数,大多包含一些其他数据/\/N\s+(\d+)/
(查找 /N <number>
)也不起作用,因为文档可以包含多个 /N
值; 大多数(如果不是全部)包含页计数的 没有。来源。那么,什么样的工作才是可靠和准确的呢?