我花了很多时间寻找一种快速简便但却非常准确的方法来获取PDF文档中的页数.由于我在一家使用PDF工作的图形打印和复制公司工作,因此在处理文档之前必须准确了解文档中的页数.PDF文档来自许多不同的客户端,因此它们不是使用相同的应用程序生成的和/或不使用相同的压缩方法.
以下是我发现的一些不足或根本不工作的答案:
Imagick需要大量的安装,apache需要重新启动,当我最终使用它时,处理时间非常长(每个文档2-3分钟)并且它总是1在每个文档中返回页面(没有看到工作副本到目前为止,Imagick,所以我扔掉了.那是getNumberImages()和identifyImage()方法.
FPDI易于使用和安装(只提取文件并调用PHP脚本),但 FPDI不支持许多压缩技术.然后它返回一个错误:
FPDF错误:此文档(test_1.pdf)可能使用FPDI附带的免费解析器不支持的压缩技术.
这将在流中打开PDF文件,并搜索某种类型的字符串,其中包含pagecount或类似的内容.
$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;
Run Code Online (Sandbox Code Playgroud)
/\/Count\s+(\d+)/(查找/Count <number>)不起作用,因为只有少数文档/Count内部有参数,因此大多数时候它不会返回任何内容.资源./\/Page\W*(\d+)/(寻找/Page<number>)没有得到页数,大多包含一些其他数据.资源./\/N\s+(\d+)/(查找/N <number>)也不起作用,因为文档可以包含多个值/N; 大多数(如果不是全部)不包含pagecount.资源.那么,什么工作可靠和准确?
Ric*_*Wit 86
它可以下载到Linux和Windows.您下载包含几个与PDF相关的小程序的压缩文件.在某处提取它.
其中一个文件是pdfinfo(或Windows的pdfinfo.exe).通过在PDF文档上运行它返回的数据示例:
Title: test1.pdf
Author: John Smith
Creator: PScript5.dll Version 5.2.2
Producer: Acrobat Distiller 9.2.0 (Windows)
CreationDate: 01/09/13 19:46:57
ModDate: 01/09/13 19:46:57
Tagged: yes
Form: none
Pages: 13 <-- This is what we need
Encrypted: no
Page size: 2384 x 3370 pts (A0)
File size: 17569259 bytes
Optimized: yes
PDF version: 1.6
Run Code Online (Sandbox Code Playgroud)
我还没有看到一个PDF文档,它返回了一个虚假的页面(尚未).它也非常快,即使有200多MB的大文档,响应时间也只需几秒钟或更短.
有一种从输出中提取页面计数的简单方法,这里是PHP:
// Make a function for convenience
function getPDFPages($document)
{
$cmd = "/path/to/pdfinfo"; // Linux
$cmd = "C:\\path\\to\\pdfinfo.exe"; // Windows
// Parse entire output
// Surround with double quotes if file name has spaces
exec("$cmd \"$document\"", $output);
// Iterate through lines
$pagecount = 0;
foreach($output as $op)
{
// Extract the number
if(preg_match("/Pages:\s*(\d+)/i", $op, $matches) === 1)
{
$pagecount = intval($matches[1]);
break;
}
}
return $pagecount;
}
// Use the function
echo getPDFPages("test 1.pdf"); // Output: 13
Run Code Online (Sandbox Code Playgroud)
当然,这个命令行工具可以用于其他语言,可以解析外部程序的输出,但我在PHP中使用它.
我知道它不是纯PHP,但外部程序在PDF处理方面更好(如问题所示).
我希望这可以帮助人们,因为我花了很多时间试图找到解决方案,我已经看到很多关于PDF页面的问题,其中我找不到我想要的答案.这就是我提出这个问题并自己回答的原因.
Kul*_*ngi 18
最简单的是使用ImageMagick
这是一个示例代码
$image = new Imagick();
$image->pingImage('myPdfFile.pdf');
echo $image->getNumberImages();
Run Code Online (Sandbox Code Playgroud)
否则你也可以使用PDF像MPDF或TCPDF为的库PHP
你可以qpdf像下面这样使用。如果文件 file_name.pdf 有 100 页,
$ qpdf --show-npages file_name.pdf
100
Run Code Online (Sandbox Code Playgroud)
这是一个使用 PHP 获取 PDF 页数的简单示例。
<?php
function count_pdf_pages($pdfname) {
$pdftext = file_get_contents($pdfname);
$num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
return $num;
}
$pdfname = 'example.pdf'; // Put your PDF path
$pages = count_pdf_pages($pdfname);
echo $pages;
?>
Run Code Online (Sandbox Code Playgroud)