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

标签 php pdf get height width

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

所以我执行了 exec(pdfinfo ... ); 我得到这个结果:

Creator: Adobe InDesign CS5 (7.0.3) Producer: Acrobat Distiller 9.4.2 (Macintosh) CreationDate: Mon Jan 30 15:48:43 2012 ModDate: Fri Feb 10 10:35:05 2012 Tagged: no Pages: 34 Encrypted: no Page size: 552.744 x 708.643 pts File size: 80724791 bytes Optimized: yes PDF version: 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";

结果:

Page size: 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";
?>

更新(要求提供更多详细信息): 下面是完整的工作示例。我已更新正则表达式以匹配 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";

?>

关于PHP 获取 Pdf 文件属性中的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9622357/

相关文章:

循环内的 PHPExcel 条件格式

php - 在 PHP 中,为什么 json_encode 比序列化慢?

javascript - jsPDF - 如何生成 A4 大小的所有页面的 PDF

HTTP 客户端错误请求 400

Java REST 客户端 : print get request (json)?

ios - 我如何从 viewdidload 函数中的 GET 请求函数快速排列

php - 我应该在我的网站上使用 "LOGINS" session 吗?

php - Eloquent 多个 whereHas ('pivot_table_X' ) AND whereHas ('pivot_table_X' )

php - 通过 htaccess 将扩展名插入到没有扩展名的文件中

php - 一些图像显示,一些图像不显示(Broken X)