PHP – Gets the layout mode of the PDF from PHP/Linux (landscape or portrait)

Gets the layout mode of the PDF from PHP/Linux (landscape or portrait)… here is a solution to the problem.

Gets the layout mode of the PDF from PHP/Linux (landscape or portrait)

Given a PDF, how can I get the layout mode (or relative width/height) of the PDF using PHP libraries or linux command-line tools?

Use http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf it can set this variable on new PDFs, but it applies to existing PDFs from Adobe.

Consider converting PDF to PS, or using GS in some other way – like converting it to an image first, then getting its width and height. Is this the best way to do it?

Solution

The solution I used was to print the first page as an image using ghostscript and then get the image dimensions

$cmd = 'gs -dSAFER -dBATCH -dNOPAUSE -dFirstPage=1 -dLastPage=1 -sDEVICE=png16m -r400 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile="'.$complete_file_path.' /p%d.png" "'.$complete_file_path.' /'.$this->pdffilename.'"';
        $result = $this->proc( $cmd );
        list($width, $height, $type, $attr) = getimagesize($complete_file_path.' /'.$pngfilename);

Related Problems and Solutions