简体   繁体   English

使用Imagemagick计算PDF文件中的页面数-PHP

[英]Count pages in PDF file using Imagemagick - PHP

I am using PHP 5 with Apache in my Windows Vista PC . 我在Windows Vista PC中将PHP 5与Apache一起使用。 I have Imagemagick already installed and configured. 我已经安装并配置了Imagemagick I want to count the total number of pages in a pdf file using imagick . 我想使用imagick pdf文件中的总页数。

I fount one solution here , but dont know how to open pdf file as text and count pages. 在这里找到一种解决方案,但不知道如何将pdf文件作为文本打开并计算页数。

somebody give me a clear solution to count pages using imagemagick like 有人给我一个清晰的解决方案,使用imagemagick来计数页面

identify -format %n testfile.pdf

From googling, I found some workarounds or examples; 通过谷歌搜索,我发现了一些变通方法或示例。

  1. imagick(identify -format %n testfile.pdf)
  2. identify -density 12 -format "%p" testfile.pdf
  3. identify -format %n testfile.pdf

I don't know how to make use of this stuff.. 我不知道如何利用这些东西。

Instead of using "identify -format %n $file" (which can turn out to be extremely slow for complex or for mult-page PDFs) you should rather use the right tool for the job , pdfinfo : 与其使用"identify -format %n $file" (对于复杂的PDF或多页PDF来说可能非常慢),您应该使用正确的工具 pdfinfo

exec("pdfinfo $file | grep Pages: | awk '{print $2}'")

which is faster by several magnitudes... 快几个数量级...

我用解决了

exec("identify -format %n $file")

From the mentioned page , here is a sample code to get the page count: 上述页面中 ,这是获取页数的示例代码:

<?php
public function getNumPagesInPDF(array $arguments = array())
{
@list($PDFPath) = $arguments;
$stream = @fopen($PDFPath, "r");
$PDFContent = @fread ($stream, filesize($PDFPath));
if(!$stream || !$PDFContent)
    return false;
$firstValue = 0;
$secondValue = 0;
if(preg_match("/\/N\s+([0-9]+)/", $PDFContent, $matches)) {
    $firstValue = $matches[1];
}
if(preg_match_all("/\/Count\s+([0-9]+)/s", $PDFContent, $matches))
{
    $secondValue = max($matches[1]);
}
return (($secondValue != 0) ? $secondValue : max($firstValue, $secondValue));
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM