2012-03-08 92 views
2

我有一個PDF文件。 我會以毫米爲單位獲得它的高度和寬度。PHP獲取Pdf文件屬性中的高度和寬度

所以我做了一個exec(pdfinfo ...); 我有這樣的結果:

創建者:Adobe公司的InDesign CS5(7.0.3)監製:的Acrobat Distiller中9.4.2(Macintosh)的CreationDate:星期一1月30日15時48分43秒2012創建ModDate:週五10年2月10日: 35:05 2012標籤:無頁數: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; 
} ?> 

沒關係,因爲我有三個數字點三個n (552.744 x 708.643)。 但是,我不知道爲什麼,有些PDF文件有這樣的信息:

者:PDFTK 1.41 - www.pdftk.com監製:iText的2.1.5(由lowagie.com)CreationDate:週一02月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文件大小:5097597 bytes優化:是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高度= 0

回答

4

一個小正則表達式會給你正確的結果。

<?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高度= 0 – 2012-03-08 18:32:01

+0

因爲你仍然在做'$ data = split()'行。如果你直接在$輸出上咆哮他的regex,那應該就是你所需要做的。如果將其與其他答案的正則表達式組合以獲取頁面編號,則可以擺脫整個循環。 – AndrewR 2012-03-09 15:59:21

+0

你能解釋一下你的想法嗎?我不都明白,謝謝 – 2012-03-09 21:18:35

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

感謝您的幫助!我有數組(0){} – 2012-03-08 18:32:34

+0

不好。 '$ output'是'$ output = shell_exec(「pdfinfo」。$ pdflivrelink);'? – powtac 2012-03-08 18:34:45

+0

是的,當我做$ output = shell_exec(「pdfinfo」。$ pdflivrelink);我沒有結果,但是當我做$ output =「文本...」時,結果如下:array(3){[0] => string(32)「Page size:425.2 x 538.582 pts」[1] = > string(5)「425.2」[2] => string(7)「538.582」} – 2012-03-08 18:38:41

-1

匹配你既然知道大小字符串的格式,你也可以像下面。 (此函數返回數組中的寬度和高度。)

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

你可以在我的代碼中插入你的函數嗎? – 2012-03-09 10:02:57

2

爲什麼不使用普通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 ..我已經嘗試過你的方法,但它顯示空數組($)結果輸出 – 2016-02-11 04:52:52

+1

@NadimulDeCj使用$ box =「BleedBox」來獲取寬度和高度。 – naf4me 2016-02-11 08:14:36

+0

@MAH ...謝謝...我有寬度和高度...但我也需要pdf的頁碼.... – 2016-02-11 08:17:07