Denis Forigo
Denis Forigo

Reputation: 49

How do I access the metadata information from a PDF file using PHP?

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

Upvotes: 4

Views: 5537

Answers (3)

j0k
j0k

Reputation: 22756

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

Upvotes: 2

Nicholas Kouvatsos
Nicholas Kouvatsos

Reputation: 643

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.

Upvotes: 1

Nikolaos Dimopoulos
Nikolaos Dimopoulos

Reputation: 11485

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

Upvotes: 1

Related Questions