33

For scripting I need to get the page dimensions of a PDF file (in mm).

pdfinfo just prints it in 'pts', e.g.:

Page size:      624 x 312 pts

What should I use?

Or what unit is 'pts' anyway - in case I want to convert them ...

  • 2
    Point on Wikipedia – Mat May 27 '12 at 18:16
  • Which page did you want the size of? The legal size outer cover? The leaflet size "this page is intentionally blank"? The letter size double pages? – Ignacio Vazquez-Abrams May 28 '12 at 9:53
28

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.

  • 1
    on common paper formats: pdfinfo sometimes gives me the paper format (like Page size: 595.28 x 841.89 pts (A4)) — I wonder if it does that for a list of page sizes it knows about? – njsg May 27 '12 at 20:45
  • 2
    A point is actually 0.352777777... mm, so 0.3528 mm is a closer approximation. – cjm May 28 '12 at 5:34
14

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).

  • 1
    When I identify a PDF on OS X I don't get any output. – David Moles May 31 '16 at 20:58
  • very late, but identify requires ImageMagick – defuzed Jan 22 at 14:15
6

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.

  • 1
    note that the values returned by this are in "points" not mm, pixels or inches – defuzed Jan 22 at 14:09
0

I used maxchlepzigs answer to calculate the mm directly:

$ pdfinfo test.pdf | grep "Page size" | grep -Eo '[-+]?[0-9]*\.?[0-9]+' | awk -v x=0.3528 '{print $1*x}'

this also works with Alex Knaufs answer but identify takes much longer than pdfinfo and requires imagemagick, the upside though is that you can use this for multiple files (ie by cd'ing into a directory and using *.pdf):

$ identify -verbose some.pdf | grep "Print size" | grep -Eo '[-+]?[0-9]*\.?[0-9]+' | awk -v x=25.4 '{print $1*x}'

The second grep command gets the two point/inch values. I'm fairly sure you can skip the grep regex and do it directly with awk but i couldn't figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.