How to access metadata from a PDF file using PHP?

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

+4
source share
3 answers

ImageMagick seems to understand PDF files and Imagick::identifyImage() returns an array with a lot of information.

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" } 
+2
source

Perhaps you should take a look at the Zend Framework, in particular the Zend_Pdf component.

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

NTN

+1
source

If you just want 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 with glasses. Then you can do some simple math to convert it to whatever units you are looking for.

+1
source

Source: https://habr.com/ru/post/1433846/


All Articles