Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the PDF width and height ? #427

Closed
Cirrusware opened this issue May 22, 2021 · 7 comments · Fixed by #593
Closed

How to get the PDF width and height ? #427

Cirrusware opened this issue May 22, 2021 · 7 comments · Fixed by #593

Comments

@Cirrusware
Copy link

Hello,

How to get the PDF width and height ?

Thanks
Vincent

@k00ni k00ni added the question label May 24, 2021
@Himra
Copy link

Himra commented Nov 10, 2021

Hello all,

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

Thank you and greetings
Himra

@k00ni
Copy link
Collaborator

k00ni commented Nov 10, 2021

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

Not to my knowledge, sorry.

@k00ni k00ni changed the title PDF Format Width and Height How to get the PDF width and height ? Nov 10, 2021
@izabala
Copy link
Contributor

izabala commented Nov 18, 2021

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!

@pablo4225
Copy link

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!

@khvoroffski
Copy link

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

@vitormattos
Copy link
Contributor

vitormattos commented Apr 10, 2023

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 added a commit to vitormattos/pdfparser that referenced this issue Apr 10, 2023
vitormattos added a commit to vitormattos/pdfparser that referenced this issue Apr 15, 2023
k00ni added a commit that referenced this issue Apr 17, 2023
* Add instructions to get page width and height

Based on #427 (comment)

* Update doc/Usage.md

* Replaced tabs with whitespaces

---------

Co-authored-by: Konrad Abicht <hi@inspirito.de>
@vitormattos
Copy link
Contributor

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: #700

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants