How to query pdf page size from the command line?

The 'pts' unit used by pdfinfo denotes a PostScript point. A PostScript point is defined in terms of an inch and a resolution of 72 dots per inch:

In the late 1980s to the 1990s, the traditional point was supplanted by the desktop publishing point (also called the PostScript point), which was defined as 72 points to the inch (1 point = 1⁄72 inches = 25.4⁄72 mm = 0.352¯7 mm [ ≙ 0.3528 mm] ).

The manual to gv contains a list of common paper formats specified in PostScript points.


Not the easiest way, but given imagemagick and units you could also use

$ identify -verbose some.pdf | grep "Print size" 
Print size: 8.26389x11.6944

to find the page size in inches (this may yield several results if the PDF uses different dimensions) and then convert the numbers like this:

$ units -t '8.26389 inch' 'mm'
  209.90281

Meaning that 8.26 inches are 209.9 mm (I used an A4 PDF for this).


Came across the same problem and came to the following solution. I didn't get into the documentation of how pdf files are constructed I just compared two empty pdf files with different page sizes.

It looks like pdfs have all kinds of attributes embedded between "<<" and ">>". I found that the page size info is there in plain text and can be found with a simple regex search.

This may or may not be true to all pdfs but it worked on all I could find from different sources.

The relevant part can look like any of these for a size A4 page:

/MediaBox [0 0 595 842]
/MediaBox[0 0 595 842]
/MediaBox[ 0 0 595.32 841.92]

It means [0 0 width height] so here is my super lame but working solution to extract this:

cat test.pdf | egrep -ao "/MediaBox ?\[ ?[0-9]+ [0-9]+ [0-9]+(\.[0-9]+)? [0-9]+(\.[0-9]+)?\]" | head -1

Just change test.pdf to your file.

Tags:

Pdf