4

How do I access the metadata (XMP) information from a PDF file using PHP? I need the height and the width of a file.

Denis Forigo
  • 49
  • 1
  • 3
  • Take a look [at this answer](http://stackoverflow.com/questions/9622357/php-get-height-and-width-in-pdf-file-proprieties) the question and answer should give you the info you need! – David Sep 12 '12 at 16:13

3 Answers3

2

It seems that ImageMagick understands PDF's and Imagick::identifyImage() return an array with lots of informations.

This snippet:

$img = new Imagick('test.pdf');
var_dump($img->identifyImage());

Generate this render:

array(9) {
  ["imageName"]=>
  string(9) "/test.pdf"
  ["format"]=>
  string(30) "PDF (Portable Document Format)"
  ["geometry"]=>
  array(2) {
    ["width"]=>
    int(596)
    ["height"]=>
    int(843)
  }
  ["resolution"]=>
  array(2) {
    ["x"]=>
    float(72)
    ["y"]=>
    float(72)
  }
  ["units"]=>
  string(9) "Undefined"
  ["type"]=>
  string(14) "TrueColorMatte"
  ["colorSpace"]=>
  string(3) "RGB"
  ["compression"]=>
  string(9) "Undefined"
  ["fileSize"]=>
  string(7) "37.6KBB"
}
Community
  • 1
  • 1
j0k
  • 22,600
  • 28
  • 79
  • 90
1

You might want to have a look at Zend Framework, in particular their Zend_Pdf component.

From their manual page:

$pdf = Zend_Pdf::load($pdfPath);

echo $pdf->properties['Title'] . "\n";
echo $pdf->properties['Author'] . "\n";

$pdf->properties['Title'] = 'New Title.';
$pdf->save($pdfPath);

HTH

Nikolaos Dimopoulos
  • 11,495
  • 6
  • 39
  • 67
1

If you just want the width and height, use

<?php 

$pdffile = "filename.pdf";
$pdfinfo = shell_exec("pdfinfo ".$pdffile); 

// find height and width
preg_match('/Page size:\s+([0-9]{0,5}\.?[0-9]{0,3}) x ([0-9]{0,5}\.?[0-9]{0,3})/', $pdfinfo,$heightandwidth); 
$width = $heightandwidth[1]; 
$height = $heightandwidth[2]; 

?> 

This will give you height and width in pts. You can then do some simple math to convert to whatever units you're looking for.