【问题标题】:Count pages in PDF file using Imagemagick - PHP使用 Imagemagick 计算 PDF 文件中的页数 - PHP
【发布时间】:2011-11-19 17:30:36
【问题描述】:

我在我的 Windows Vista PC 中使用 PHP 5 和 Apache。我已经安装和配置了Imagemagick。我想使用imagick 计算 pdf 文件中的总页数。

我找到了一种解决方案 here,但不知道如何将 pdf 文件作为文本打开并计算页数。

有人给我一个清晰的解决方案来计算使用 imagemagick 之类的页面数

identify -format %n testfile.pdf

通过谷歌搜索,我找到了一些解决方法或示例;

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

我不知道如何使用这些东西..

【问题讨论】:

    标签: php pdf imagemagick imagick


    【解决方案1】:

    与其使用"identify -format %n $file"(对于复杂或多页的PDF,这可能会变得非常慢),不如使用适合工作的工具pdfinfo

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

    快了几个数量级...

    【讨论】:

    • 有用的指针,但我在弄清楚如何在我的系统中获取 pdfinfo 时遇到了一些困难。最后:Debian/Ubuntu:sudo aptitude install poppler-utils; OSX:brew install poppler。
    【解决方案2】:

    我用它解决了;

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

    【讨论】:

    • 这对于多页 PDF 来说会非常缓慢,尤其是对于页面上具有更复杂图形的 PDF...
    【解决方案3】:

    来自mentioned page,这里是获取页数的示例代码:

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

    【讨论】:

    • 这对我不起作用.. :( 我设置了 $PDFPath = "test.pdf"; ,但不起作用。我该怎么办?
    • 这个帖子可能会对你有所帮助:stackoverflow.com/questions/1143841/…
    • 此解决方案并非在所有情况下都有效。对我来说,这个脚本不适用于 PDF 1.6,但适用于 1.4 和 1.5 版本
    • 自 2010 年以来不再维护 PDFlib 库。但更糟糕的是,它的商业领域而非公共领域。
    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 2023-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多