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


PHP Get height and width in Pdf file proprieties

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

所以我做了一个高管(pdfinfo…);我有这样的结果:

创建者:Adobe InDesign CS5(7.0.3)制作者:Acrobat Distiller 9.4.2(Macintosh)创建日期:1月30日星期一15:48:43 2012修改日期:2月10日星期五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

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

it结果:

页面大小:425.2 x 538.582 ptswidth=0 height=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";
?>

更新(要求了解更多详细信息):完成下面的工作示例。我已更新Regex以匹配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";
?>

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

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

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

Imagick库可用于获取文件的维度

 $image = new Imagick($file);
 $geo=$image->getImageGeometry();
 $width=$geo['width'];
 $height=$geo['height'];

如果没有安装imagick库,Ubuntu用户可以使用以下命令进行安装:

 sudo apt-get install php-imagick
 php -m | grep imagick
 sudo service apache2 restart

由于您知道大小字符串的格式,您也可以按照以下方式进行操作。(此函数返回数组中的宽度和高度。)

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