pdfparser icon indicating copy to clipboard operation
pdfparser copied to clipboard

How to get the PDF width and height ?

Open Cirrusware opened this issue 5 years ago • 7 comments

Hello,

How to get the PDF width and height ?

Thanks Vincent

Cirrusware avatar May 22 '21 14:05 Cirrusware

Hello all,

i need the same information. Is there already news about this.

Thank you and greetings Himra

Himra avatar Nov 10 '21 12:11 Himra

i need the same information. Is there already news about this.

Not to my knowledge, sorry.

k00ni avatar Nov 10 '21 12:11 k00ni

Hi, I hope this is still of some use. Actually I have a look, because of issue #472.

You can use MediaBox info for this. If you look The PDF Specification, on page 77 the MediaBox is defined as:

MediaBox: (Required;** inheritable) A rectangle (see 7.9.5 "Rectangles"), expressed in default user space units, that shall define the boundaries of the physical medium on which the page shall be displayed or printed (see 14.11.2, "Page Boundaries").

There is a lot of info in the specification and I am not an expert on this.

For getting that info:

$parser = new Paser();
$pdf = $parser->parseFile('file.pdf');
$pages = $pdf->getPages();
$page = $pages[0];
$details = $page->getDetails();
$mediaBox = $details['MediaBox'];

Media box should be something like:

Array
(
    [0] => 0      
    [1] => 0      
    [2] => 595.2  
    [3] => 841.919
)

Hope this can help!

izabala avatar Nov 18 '21 23:11 izabala

Hi! Im trying to get pdf page height and width using the code in your example, but I only get the following properties: array(4) { ["Type"]=> string(4) "Page" ["Parent"]=> array(2) { ["Type"]=> string(5) "Pages" ["Count"]=> string(1) "1" } ["Resources"]=> array(0) { } ["Contents"]=> array(2) { ["Filter"]=> string(11) "FlateDecode" ["Length"]=> string(4) "1829" } }

But, if I print the whole pdf object, I can search "MediaBox" and I see two values that appear to be Height and Width. I copied the result in a pastebin for the sake of the readers :) https://pastebin.com/kE83rvs6

Any advice on how to get this working? Thanks a lot in advance!

pablo4225 avatar Sep 10 '22 17:09 pablo4225

Same question. Any idea how to get width and height from fpdf generated file? Code above doesn`t work for me either.

khvoroffski avatar Feb 14 '23 14:02 khvoroffski

The same of previous suggestion but with more details:

$pages = $pdf->getPages();
$mediaBox = [];
foreach ($pages as $page) {
	$details = $page->getDetails();
	$mediaBox[] = [
		'width' => $details['MediaBox'][2],
		'height' => $details['MediaBox'][3]
	];
}

Output:

Array(
	[0] => Array
	(
		[width] => 595.28
		[height] => 841.89
	)
	[1] => Array
	(
		[width] => 360
		[height] => 360
	)
	[2] => Array
	(
		[width] => 841.89
		[height] => 595.28
	)
)

vitormattos avatar Apr 10 '23 22:04 vitormattos

Only to register here the information, I identified an edge case parsing a PDF generated with FPDF. The MediaBox don't exists at page scope and was necessary to get from document scope:

I added this information here in the documentation here: https://github.com/smalot/pdfparser/pull/700

vitormattos avatar Apr 05 '24 18:04 vitormattos