簡體   English   中英

使用Imagemagick計算PDF文件中的頁面數-PHP

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

我在Windows Vista PC中將PHP 5與Apache一起使用。 我已經安裝並配置了Imagemagick 我想使用imagick pdf文件中的總頁數。

在這里找到一種解決方案,但不知道如何將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

我不知道如何利用這些東西。

與其使用"identify -format %n $file" (對於復雜的PDF或多頁PDF來說可能非常慢),您應該使用正確的工具 pdfinfo

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

快幾個數量級...

我用解決了

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

上述頁面中 ,這是獲取頁數的示例代碼:

<?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