3

Consider this PDF. I am trying to convert it to standard letter size (8.5 x 11) using the following command.

gs -dFIXEDMEDIA -dBATCH -dNOPAUSE -sPAPERSIZE=letter \
   -dPDFFitPage -q -sDEVICE=pdfwrite -sOutputFile=out.pdf \
    dean08mapreduce.pdf

Here is pdfinfo on the output.

Producer:       GPL Ghostscript 9.10
CreationDate:   Mon Nov 17 18:25:01 2014
ModDate:        Mon Nov 17 18:25:01 2014
Tagged:         no
Form:           none
Pages:          7
Encrypted:      no
Page size:      596.225 x 792 pts
Page rot:       0
File size:      126908 bytes
Optimized:      no
PDF version:    1.4

Here is pdfinfo on the original:

Producer:       Creo Normalizer JTP
CreationDate:   Fri Dec 21 09:09:07 2007
ModDate:        Fri Dec 21 14:46:23 2007
Tagged:         no
Form:           AcroForm
Pages:          7
Encrypted:      no
Page size:      603 x 801 pts
Page rot:       0
File size:      235239 bytes
Optimized:      yes
PDF version:    1.3

Notice that the output has the correct height but not the correct width. It is effectively 8.3 x 11, not 8.5 x 11.

I have tried the solutions here and here but neither achieves what I require.

How can I force the PDF to be exactly 8.5 x 11?

I do not mind if it involves adding a little whitespace on either side.

Community
  • 1
  • 1
merlin2011
  • 63,368
  • 37
  • 161
  • 279

1 Answers1

5

Double conversion isn't a good plan, you potentially introduce problems at every step, especially since PostScript does not support the graphics model of PDF (in particular it does not support transparency)

Your problem is that the original PDF file contains a CropBox, which is retained by the Ghostscript pdfwrite device. The output from pdfinfo is telling you the size of the PDF file, taking the CropBox into account. The MediaBox is in fact 612x792, ie exactly 8.5x11, which is what you wanted.

The reason the height is given differently is because the new MediaBox is inside the original CropBox, so it is the intersection of the two boxes being given.

If you don't want the CropBox to be preserved, you will have to construct a new CropBox Page or PAGES pdfmark and send it as PostScript. This isn't entirely non-trivial; in your case each page has a CropBox (its not a single default for the entire document), so you need to override the CropBox on each page. To do this you need to define a /EndPage procedure which sets the desired CropBox using a pdfmark, and send that before you process the PDF file.

This :

gs -sDEVICE=pdfwrite \
   -dDEVICEWIDTHPOINTS=612 -dDEVICEHEIGHTPOINTS=792 -dFIXEDMEDIA \
   -sOutputFile=out.pdf \
   -c "<</EndPage {0 eq {[/CropBox [0 0 612 792] /PAGE pdfmark true}{false}ifelse}>> setpagedevice" \
   -f dean08mapreduce.pdf

Worked for me.

Kurt Pfeifle
  • 78,224
  • 20
  • 220
  • 319
KenS
  • 27,930
  • 2
  • 31
  • 42