【问题标题】:PHP Get height and width in Pdf file proprietiesPHP在Pdf文件属性中获取高度和宽度
【发布时间】:2012-03-26 05:07:09
【问题描述】:

我有一个 PDF 文件。 我会得到它的高度和宽度,以毫米为单位。

所以我做了一个 exec(pdfinfo ... ); 我有这个结果:

创建者:Adobe InDesign CS5 (7.0.3) 制作者:Acrobat Distiller 9.4.2 (Macintosh) 创建日期:2012 年 1 月 30 日星期一 15:48:43 修改日期:2012 年 2 月 10 日星期五 10:35:05 标记:无页数: 34 加密:否页面大小:552.744 x 708.643 pts 文件大小:80724791 字节优化:是 PDF 版本:1.3

我有一个脚本女巫提取我的信息:

<?php 
$output = shell_exec("pdfinfo ".$pdflivrelink);
$data = explode("\n", $output); //puts it into an array
for($c=0; $c < count($data); $c++) {
        if(stristr($data[$c],"Pages") == true) {
        $pagesnumber = trim(substr($data[$c],6));
        }
        if(stristr($data[$c],"Page size") == true) {
            $pagesize_H = height_pdf(trim(substr($data[$c],9)));
        }
        if(stristr($data[$c],"Page size") == true) {
            $pagesize_L = width_pdf(trim(substr($data[$c],9)));
        }

}
function height_pdf($size){
$hauteur = round(substr($size,7,7)/2.83);
return $hauteur;
}
function width_pdf($size){
$largeur = round(substr($size,17,7)/2.83);
return $largeur;
} ?>

没关系,因为我有三个数字点三个数字 (552.744 x 708.643)。 但是,我不知道为什么,有些 PDF 文件有这个信息:

创建者:pdftk 1.41 - www.pdftk.com 制作者:iText 2.1.5(by lowagie.com) 创建日期:2012 年 2 月 27 日星期一 13:18:23 修改日期:2012 年 2 月 27 日星期一 16:26:12 标记:否页数:36 加密:否 页面大小:425.2 x 538.582 pts 文件大小:5097597 字节 优化:是 PDF 版本:1.6

425.2 x 538.582:所以我的脚本不起作用!

你能帮帮我吗?非常感谢!


我对此进行了测试:

    $output = shell_exec("pdfinfo ".$pdflivrelink);
    $data = explode("\n", $output); //puts it into an array
    for($c=0; $c < count($data); $c++) {
            if(stristr($data[$c],"Pages") == true) {
            $pagesnumber = trim(substr($data[$c],6));

            }
            if(stristr($data[$c],"Page size") == true) {
                echo $data[$c];
    preg_match('/Page size: ([0-9]*\.?[0-9]?) x ([0-9]*\.?[0-9]?)/', $data[$c], $matchess);
    $width = round($matchess[1]/2.83);
    $height = round($matchess[2]/2.83);

            }
}
echo "width = $width<br>height = $height";

结果:

页面大小:425.2 x 538.582 ptswidth = 0 height = 0

【问题讨论】:

标签: php pdf get height width


【解决方案1】:

一点正则表达式就能得到正确的结果。

<?php
$str = 'Creator: pdftk 1.41 - www.pdftk.com Producer: iText 2.1.5 (by lowagie.com) CreationDate: Mon Feb 27 13:18:23 2012 ModDate: Mon Feb 27 16:26:12 2012 Tagged: no Pages: 36 Encrypted: no Page size: 425.2 x 538.582 pts File size: 5097597 bytes Optimized: yes PDF version: 1.6';

preg_match('/Page size: ([0-9]*\.?[0-9]?) x ([0-9]*\.?[0-9]?)/', $str, $matches);
$width = round($matches[1]/2.83);
$height = round($matches[2]/2.83);

echo "width = $width<br>height = $height";
?>

更新(要求提供更多详细信息): 完整的工作示例如下。我更新了正则表达式以匹配来自pdfinfo的真实输出

<?php

$output = shell_exec("pdfinfo ".$pdflivrelink);

// find page count
preg_match('/Pages:\s+([0-9]+)/', $output, $pagecountmatches);
$pagecount = $pagecountmatches[1];

// find page sizes
preg_match('/Page size:\s+([0-9]{0,5}\.?[0-9]{0,3}) x ([0-9]{0,5}\.?[0-9]{0,3})/', $output, $pagesizematches);
$width = round($pagesizematches[1]/2.83);
$height = round($pagesizematches[2]/2.83);

echo "pagecount = $pagecount <br>width = $width<br>height = $height";

?>

【讨论】:

  • 感谢您的帮助!我有宽度 = 0 高度 = 0
  • 因为你还在做$data = split()这一行。如果你直接在 $output 上咆哮他的正则表达式,那应该就是你需要做的。如果您将其与其他答案的正则表达式结合起来以获取页面编号,则可以摆脱整个循环。
  • 你能详细解释一下你的想法吗?不太明白,谢谢
  • @AndrewR 非常感谢您的宝贵回答...。您能帮我从这个维度获取 px 吗...?
【解决方案2】:

使用preg_match()

// Debugging:
$output = shell_exec("pdfinfo ".$pdflivrelink);
var_dump($output);

// Dimension:
preg_match('~ Page size: ([0-9\.]+) x ([0-9\.]+) pts ~', $output, $matches);
var_dump($matches);


// No of pages:
preg_match('~ Pages ([0-9]+) ~', $output, $matches);
var_dump($matches);

【讨论】:

  • 感谢您的帮助!我有数组(0){}
  • 不好。 $output$output = shell_exec("pdfinfo ".$pdflivrelink); ?
  • 是的,当我这样做时 $output = shell_exec("pdfinfo ".$pdflivrelink);我没有结果,但是当我执行 $output ="the text..." 时,结果是:array(3) { [0]=> string(32) " Page size: 425.2 x 538.582 pts " [1]= > 字符串(5) "425.2" [2]=> 字符串(7) "538.582" }
  • 我这样做:preg_match('~ 页面大小: ([0-9\.]+) x ([0-9\.]+) pts ~', shell_exec("pdfinfo". $pdflivrelink), $matches); var_dump($matches);结果相同:array(0) { }
  • 尝试“页数:”模式。
【解决方案3】:

为什么不使用纯 PHP 来获取 pdf 尺寸?

<?php
function get_pdf_dimensions($path, $box="MediaBox") {
    //$box can be set to BleedBox, CropBox or MediaBox 

    $stream = new SplFileObject($path); 

    $result = false;

    while (!$stream->eof()) {
        if (preg_match("/".$box."\[[0-9]{1,}.[0-9]{1,} [0-9]{1,}.[0-9]{1,} ([0-9]{1,}.[0-9]{1,}) ([0-9]{1,}.[0-9]{1,})\]/", $stream->fgets(), $matches)) {
            $result["width"] = $matches[1];
            $result["height"] = $matches[2]; 
            break;
        }
    }

    $stream = null;

    return $result;
}

var_dump(get_pdf_dimensions("file.pdf"));

【讨论】:

  • @fitman .. 我试过你的方法,但它在 $result 输出中显示 null array()
  • @NadimulDeCj 使用 $box="BleedBox" 获取宽度和高度。
  • @MAH ...谢谢...我得到了宽度和高度...但我还需要pdf的页码...。
  • 请使用此代码获取 if(class_exists('Imagick')){ $image = new Imagick(); $image->pingImage($pdf_file);回声 $image->getNumberImages(); }
【解决方案4】:

使用 Fpdi,注意使用 getTemplateSize 它是...

const INCHESTOMM = 25.4;

public static function getPDFdimensions($strFilename): array
{
    $pdf1 = new FPDI('P', 'in');
    $pdf1->setSourceFile($strFilename);
    $tplIdx1 = $pdf1->importPage(1);
    $size = $pdf1->getTemplateSize($tplIdx1);
    $w = $size["width"];
    $h = $size["height"];
    return [round($w * self::INCHESTOMM), round($h * self::INCHESTOMM)];
}

【讨论】:

    【解决方案5】:

    既然你知道大小字符串的格式,你也可以像下面那样做。 (此函数以数组的形式返回宽度和高度。)

    function size_pdf($size){
        $result = array();
        $tmp = exlode('x', $size);
        $result['height'] = round(trim($tmp[0])/2.83);
        $result['width'] = round(trim($tmp[1])/2.83);
    
        return $result;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 2014-07-03
      • 2019-05-26
      • 2011-11-26
      • 1970-01-01
      • 2021-11-19
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多