只在PHP中计算PDF中的页数

我需要一种方法来计算PHP中的PDF页数。 我已经做了一些谷歌search,我发现的唯一的东西要么使用shell / bash脚本,Perl或其他语言,但我需要本地PHP的东西。 有没有图书馆或如何做到这一点的例子?

您可以使用PHP的ImageMagick扩展。 ImageMagick了解PDF,您可以使用identify命令来提取页面的数量。 PHP函数是Imagick :: identifyImage() 。

如果使用Linux,这比使用identify来获取页面数量要快得多(特别是页数很高):

 exec('/usr/bin/pdfinfo '.$tmpfname.' | awk \'/Pages/ {print $2}\'', $output); 

你确实需要安装pdfinfo。

我知道这已经很老了,但是如果现在和我有关,那么也可能与其他人有关。

我只是想出了这种获取页码的方法,因为这里列出的方法对于大型PDF来说效率低下且速度非常慢。

 $im = new Imagick(); $im->pingImage('name_of_pdf_file.pdf'); echo $im->getNumberImages(); 

似乎对我很好!

我其实是采取了综合的方法。 由于我在我的服务器上禁用了exec,所以我想坚持使用基于PHP的解决scheme,所以最终这样做:

码:

 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; } 

如果因为没有计数标签而无法计算出来,那么就使用imagick php扩展。 我采取双重做法的原因是因为后者很慢。

您可以尝试fpdi(请参阅此处 ),正如您在设置源文件时所看到的那样,您可以看到页码。

尝试这个 :

 <?php if (!$fp = @fopen($_REQUEST['file'],"r")) { echo 'failed opening file '.$_REQUEST['file']; } else { $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); echo 'There '.($max<2?'is ':'are ').$max.' page'.($max<2?'':'s').' in '. $_REQUEST['file'].'.'; } ?> 

Count标签显示不同节点的页数。 父节点在其Count标签中具有其他值的总和,所以这个脚本只是查找最大值(即页面数)。

 function getNumPagesPdf($filepath) { $fp = @fopen(preg_replace("/\[(.*?)\]/i", "", $filepath), "r"); $max = 0; if (!$fp) { return "Could not open file: $filepath"; } else { 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 = trim($matches2[0]); break; } } } @fclose($fp); } return $max; } 

这正是我想要的:

我只是制定了这种获得PDF页码的方法…得到PDF页数后,我只是添加中断,以便它不会在无限循环在这里….

这一个不使用imagick:

 function getNumPagesInPDF($file) { //http://www.hotscripts.com/forums/php/23533-how-now-get-number-pages-one-document-pdf.html if(!file_exists($file))return null; if (!$fp = @fopen($file,"r"))return null; $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); return (int)$max; } 
 $pdftext = file_get_contents($caminho1); $num_pag = preg_match_all("/\/Page\W/", $pdftext,$dummy); 

只使用PHP可以导致安装复杂的库,重新启动Apache等许多纯PHP方式(如开放stream和使用正则expression式)是不准确的

包括的答案是我能想到的唯一快速可靠的方法。 它使用一个单一的可执行文件,但不必安装(* nix或Windows),一个简单的PHP脚本提取输出。 最好的事情是,我还没有看到一个错误的pagecount呢!

在这里可以find,包括为什么其他方法“不工作”

获取PDF文档中的页数

在* nix环境中,您可以使用:

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

默认情况下应安装pdftops。

或者像Xethron所build议的那样:

 pdfinfo filename.pdf | grep Pages: | awk '{print $2}'