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

4
我有一个PDF文件。我想获取它的高度和宽度(单位为毫米)。
因此,我执行了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点文件大小: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文件中有这些信息:

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

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 点,宽度 = 0,高度 = 0


(注:该内容为关于页面尺寸的技术描述)

为了更好地理解PDF宽度,请参考如何从命令行查询PDF页面大小? - LF00
2.83 = 72 / 25.4 - LF00
6个回答

6
一点正则表达式就可以得到正确的结果。
<?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。 - Seb Gy
因为您仍在执行 $data = split() 行。如果您直接在 $output 上运行正则表达式,那么这应该是您需要做的全部。如果您将其与其他答案提供的用于获取页面编号的正则表达式结合使用,则可以消除整个循环。 - AndrewR
你能再解释一下你的想法吗?我不是很理解,谢谢。 - Seb Gy
@AndrewR非常感谢您宝贵的回答....您能帮我从这个尺寸中获取px吗...? - Nadimul De Cj

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

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

不太好。$output$output = shell_exec("pdfinfo ".$pdflivrelink); 吗? - powtac
我这样做:preg_match('〜页面大小:([0-9 \。] +)x([0-9 \。] +)pts〜',shell_exec(“pdfinfo”.$ pdflivrelink),$matches);var_dump($ matches);相同的结果:数组(0){} - Seb Gy
我也得出了这个结论。我们该怎么做? - Seb Gy
它给了我:string(352) "标题:未知 创建者:Adobe InDesign CS5.5(7.5) 生产商:Adobe PDF Library 9.9 创建日期:2012年1月31日星期二17:05:25 修改日期:2012年2月10日星期五10:42:57 标记:是 页面数:34 加密:否 页面大小:581.108 x 793.7 pts 文件大小:31374145字节 优化:是 PDF版本:1.3"(这是关于一个新的PDF文件,不用担心) - Seb Gy
让我们在聊天中继续这个讨论。点击此处进入聊天室 - Seb Gy
显示剩余5条评论

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

1
@fitman..我已经尝试了你的方法,但是$result输出显示为空数组()。 - Nadimul De Cj
1
@NadimulDeCj 使用 $box="BleedBox" 来获取宽度和高度。 - naf4me
@MAH...谢谢…我得到了宽度和高度……但我还需要PDF的页码…. - Nadimul De Cj
请使用以下代码获取信息:如果(class_exists('Imagick')){ $image = new Imagick(); $image->pingImage($pdf_file); echo $image->getNumberImages(); } - naf4me

-1

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

-3

既然您已经了解了尺寸字符串的格式,您也可以像下面这样做。 (此函数将以数组形式返回宽度和高度。)

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接