PHP Get Height and Width in PDF File Proprieties

PHP Get height and width in Pdf file proprieties

A little regex will get you the correct results.

<?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";
?>

Update ( asked for more details ) :
Complete working example below. I've updated Regex to match real output from 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";

?>

Get height and width of a PDF file using PHP

You can access the first page of any multipage file format that ImageMagick can read by appending [0] to the filename.

This means, that you can ask identify to print out width and height for the first page of a PDF with this command, which you should have no problem to translate into PHP syntax

 identify  -format "width: %W  --  height: %H\n"  some.pdf[0]

This will print the values for the first pages MediaBox in the following format:

  width: 345  --  height: 777

The unit of these values is PostScript points (where 72 pt == 1 inch). Of course you are free to modify the command to suite your needs, like giving out only 2 number values, or using the WxH format:

 identify  -format "%W %H\n"  some.pdf[0]
identify -format "%Wx%H\n" some.pdf[0]

However, be aware of the following facts:

  1. PDF also supports the optional TrimBox, CropBox, ArtBox and BleedBox settings.
  2. The most important of these is the TrimBox, because:
  3. Should the TrimBox be different from the MediaBox (it needs to be the same or smaller and isn't allowed to be bigger!) then PDF viewers and printer drivers are asked to only render the part of the page that's inside that box.
  4. identify will return the MediaBox values only, it does not have support for the other Boxes.
  5. Likewise, convert will use the (potentially bigger) MediaBox size of the PDF page to render the image (and thus its result will seemingly look different from what you see in a PDF viewer).
  6. Luckily, PDFs with TrimBox values which are very much different from the MediaBox values are not very common.
  7. If you need to get access to the value settings for all the Boxes, you should use a different command utility to extract the relevant info: pdfinfo -box -f 1 -l 1 some.pdf | grep -E '(Box:|rot:|size:)'. (Use the Poppler version of pdfinfo if possible...)

How to get width and height PDF page by Imagick?

How about this approach?
By Imagick I can easily get image from pdf file

$RESOLUTION = 300;
$myurl = 'filename.pdf['.$pagenumber.'];'
$image = new Imagick($myurl);
$image->setResolution( $RESOLUTION , $RESOLUTION );
$image->setImageFormat( "png" );
$image->writeImage('newfilename.png');

Now I have image from page of PDF file. I know resolution (number pixels per inch) and I can get width and height of image in pixels. So don't need to have deep knowledge in math to calculate width and height of page of PDF in inch:

$pdfPageWidth = $imageWidth / $RESOLUTION;
$pdfPageHeight = $imageHeght/ $RESOLUTION;

How do I get the width and height of a doc generated with FPDF

note: read Ross' McLellan's answer below

As far as I remember you can't do it with vanilla FPDF. You can either extend it to have a method that would return this value for you, or just store the width as a public property of fpdf object.

Get properties of a pdf file by using PHP

First check your pdfinfo's path, if you don't have it: XPDF... so then change your code:

$pdffile = "C:\\Users\\suresh\\Downloads\\Invoice_52683.pdf";
$pdfinfo_path = "C:\\path\\to\\pdfinfo.exe";
$pdfinfo = shell_exec($pdfinfo_path." ".$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);
echo $width = $heightandwidth[1];
echo $height = $heightandwidth[2];

If you use Linux: $pdfinfo_path = "/path/to/pdfinfo";

How to get DPI, width and length of an image in PDF in PHP

DPI is irrelevant in PDFs themselves. Your concern is with an image embedded within it, and to parse those out easily you will probably want to rebuild with a library that handles the file IO for you, such as http://www.pdflib.com



Related Topics



Leave a reply



Submit