代码之家  ›  专栏  ›  技术社区  ›  UnkwnTech

仅用PHP[关闭]计算PDF中的页数

  •  34
  • UnkwnTech  · 技术社区  · 15 年前

    11 回复  |  直到 15 年前
        1
  •  26
  •   stephangroen    12 年前

    如果使用Linux,这比使用Linux快得多 identify 要获取页数(尤其是在页数较高的情况下),请执行以下操作:

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

    您确实需要安装pdfinfo。

        2
  •  15
  •   user678415    12 年前

    我知道这很老了。。。但如果它现在与我相关,它也可能与其他人相关。

    我刚刚想出了这个获取页码的方法,因为这里列出的方法对于大型PDF来说效率很低,速度也非常慢。

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

    似乎对我来说很有用!

        3
  •  14
  •   Travis Beale    15 年前

    identify 命令提取页数。PHP函数是 Imagick::identifyImage() .

        4
  •  11
  •   sth ACP    14 年前

    实际上,我采用了一种综合方法。由于我在服务器上禁用了exec,我想继续使用基于PHP的解决方案,因此最终得出以下结论:

    代码:

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

        5
  •  9
  •   gen_Eric    13 年前

    您可以尝试fpdi(参见 here ),正如您在设置源文件时看到的那样,您将返回页码。

        6
  •  3
  •   Baboum    15 年前

    <?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标记中包含其他节点的总和,因此此脚本只查找max(即页数)。

        7
  •  2
  •   user669677 user669677    13 年前

    这个不使用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;
    
    }
    
        8
  •  2
  •   stev    12 年前
    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页面计数后,我只需在while中添加break,这样它就不会在这里无限循环。。。。

        9
  •  1
  •   kenorb    11 年前

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

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

    其中pdftops应默认安装。

    pdfinfo filename.pdf | grep Pages: | awk '{print $2}'
    
        10
  •  0
  •   Murilo    12 年前
    $pdftext = file_get_contents($caminho1);
    
     $num_pag = preg_match_all("/\/Page\W/", $pdftext,$dummy);
    
        11
  •  0
  •   Community kfsone    7 年前

    仅使用PHP可能会导致安装复杂的库、重新启动Apache等。许多纯PHP方法(如打开流和使用regex)都是可用的 不准确的 .

    包含的答案是我能想到的唯一快速可靠的方法。它使用单个可执行文件,但不必安装(无论是*nix还是Windows),并且一个简单的PHP脚本提取输出。最好的是,我还没有看到错误的页面计数!

    可以在这里找到,包括为什么其他方法“不起作用”

    Get the number of pages in a PDF document