PHP获取Pdf文件中的高度和宽度属性

PHP获取Pdf文件中的高度和宽度属性,php,pdf,get,height,width,Php,Pdf,Get,Height,Width,我有一个PDF文件。 我想得到它的高度和宽度,单位是毫米 所以我做了一个exec(pdfinfo…); 我有一个结果: 创建者:Adobe InDesign CS5(7.0.3)制作人:Acrobat Diller 9.4.2(Macintosh)CreationDate:Mon Jan 30 15:48:43 2012修改日期:Fri Feb 10:35:05 2012标记:无页面:34加密:无页面大小:552.744 x 708.643 pts文件大小:80724791字节优化:是PDF版本

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

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

创建者:Adobe InDesign CS5(7.0.3)制作人:Acrobat Diller 9.4.2(Macintosh)CreationDate:Mon Jan 30 15:48:43 2012修改日期:Fri Feb 10:35:05 2012标记:无页面: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(由lowagie.com制作)创建日期:Mon Feb 27 13:18:23 2012修改日期:Mon Feb 27 16:26:12 2012标记:无页面:36加密:无页面大小:425.2 x 538.582 pts文件大小:5097597字节优化:是PDF版本:1.6

425.2x538.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";
$output=shell_exec(“pdfinfo”。$pdflivrelink);
$data=分解(“\n”,$output)//将其放入数组中
对于($c=0;$c高度=$height”;
结果是:

页面大小:425.2x538.582ptswidth=0高度=0

用一个:


一点正则表达式将得到正确的结果

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

因为您知道大小字符串的格式,所以也可以像下面这样执行。(此函数返回数组中的宽度和高度。)


为什么不使用普通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"));

使用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)];
}

谢谢你的帮助!我的宽度=0高度=0,因为您仍在执行
$data=split()
行。如果您直接在$output上使用正则表达式,那么您应该只需要这样做。如果你把这个和另一个答案的正则表达式结合起来得到page num,你可以去掉整个循环。你能解释一下你的想法吗?我不太明白,thanks@AndrewR非常感谢您宝贵的回答。。。。你能帮我从这个维度得到px吗…?谢谢你的帮助!我的数组(0){}不好<代码>$output
$output=shell_exec(“pdfinfo”。$pdflivrelink)?是的,当我执行$output=shell_exec(“pdfinfo”。$pdflivrelink);我没有结果,但当我做$output=“文本…”时,它的结果是:数组(3){[0]=>string(32)“页面大小:425.2x538.582 pts”[1]=>string(5)“425.2”[2]=>string(7)“538.582”}我这样做:preg_匹配('~页面大小:([0-9\.]+)x([0-9\.]+)pts~),shell_exec(“pdfinfo”。$pdflivrelink),$matches);var_dump($matches);相同的结果:数组(0){}尝试“页数:”模式。@fitman。。我已经尝试过你的方法,但它在$result中显示null array()output@NadimulDeCj使用$box=“BleedBox”获取宽度和高度。@MAH。。。非常感谢。我得到了宽度和高度。。。但我还需要pdf的页码…请使用此代码获取(class_exists('Imagick'){$image=new Imagick();$image->pingImage($pdf_文件);echo$image->getNumberImages();}要更好地理解pdf宽度,请参阅
2.83=72/25.4
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;
}
<?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"));
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)];
}