That is, how to change the margins of PDF files while keeping the physical page size the same. This article is about solutions that work on Linux, though some might also work on Windows and Mac.

There are multiple solution alternatives, but only the first is reported to work out well. Let's start:

Alternative 1: Using ghostscript (works best!)

Since Thomas enabled this to work (see comments), this is the single best-working alternative in this list. It has no obvious drawbacks. It is a quite elegant solution, including freely adjustable margins and vector fonts and images, and avoiding the GUI hassles. Also, the instruction should also work with Postscript (PS) files. The idea comes from this hread.

gs
  -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
  -dCompatibilityLevel="1.3" -dPDFSETTINGS="/printer" \
  -dSubsetFonts=true -dEmbedAllFonts=true \
  -sPAPERSIZE=a4 -sOutputFile="out.pdf" \
  -c "<</BeginPage{0.9 0.9 scale 29.75 42.1 translate}>> setpagedevice" \
  -f in.pdf

This example scales the page content to 90%, and to compensate, moves it right and up by 5% each to keep its center the same. The 5% has to be expressed in absolute measures of the DIN A4 page we're using here, using PostScript points as units (which are 1/72 inches). So a DIN A4 page is 595×842 pps (width×height) in Ghostscript, and 5% of this is the 29.75×42.1 pps you see in the command. Now you know how to calculate these numbers for other scales and page sizes …

Oh and, the "\" line ends in the command above are just for line continuation in bash. Remove them when making a single-line command of course.

Edit 2012-08-31: Integrated corrections and information from Thomas' comments below.

Alternative 2: using pstops

The task can be performed with pstops (whereof psnup is a simplified frontend).

  1. Convert your PDF file to a PS file, by printing to a file in Adobe Reader, or by using pdf2ps (Ghostscript-based) or pdftops (xpdf-based).
  2. Use pstops to adjust the marginspstops -p a4 "L@.9(1cm,1cm)" in.ps out.psOn mounting pages: here, the the task is to mount two A4 pages in A5 format on one A4 page, guaranteeing page margins of 3cm at left and right and 2cm at top and bottom. We need a width of 150mm and a height of 257mm. To scale 297mm (A4 height) to 150mm, use factor 0.505. Such n-up mounting together with freestyle adjustment of margins is not possible ith psnup, which has a simpler user interface.pstops -p a4 "2:0L@.505(18cm,2cm)+1L@.505(18cm,14.85cm)" in.ps out.ps
  3. Convert the PS file back to PDF by using pdftops.

The problem with psnup (from PSUtils Release 1 Patchlevel 17) and also of its frontend psnup is that it converts fonts to bitmap fonts (Adobe Type 3). This can be detected as rastered fonts when viewing the PDF file with Adobe Reader. It generates somewhat lower print quality, but is still acceptable. What is not acceptable (with respect to file size) is that pstops converts the whole file to an image if it has no idea how to treat it.

To debug pstops and psnup output, you can use the -b option, which will mark out the original pages' borders.

Alternative 3: Using Adobe Reader and printing the "current view"

Another alternative:

  1. Use Adobe Reader to print-to-file your page centered on a large sheet of paper, without scaling. This results in huge page margins, which we will reduce as desired in the other steps.
  2. Use ps2pdf to re-destill the PS file to PDF.
  3. Open the new PDF file in Adobe Reader and zoom and move so that you have an appropriate view, with your desired page margins.
  4. Use the "print current view" option in Adobe Reader to print exactly that view (which is, just what you see on the screen from the document right now).
  5. Again, use ps2pdf to re-destill the PS file to PDF.

The disadvantage of this technique is of course that margins cannot be chosen exactly.

But, this technique also allows to perform n-up mounting, should you need it. Say you start with a two-page document and want it 2-up mounted on one page. Then generate one page for each input page, using teh above technique: the first output page with a huge right margin, the second with a huge left margin. The margins should be large enough to contain the complete other page, respectively. You can now mount these output pages into one by overlaying them on top of each other using pdftk with the background option.

Alternative 4: Using Adobe Reader and printer margins

This has not yet been worked out, but might be possible. When printing (to a file or otherwise) Adobe Reader will fit the pages into the printable area of the selected printer. Now the idea would be to choose the special printer "Custom …". This allows you to enter a lp print command. Per the lp documentation, CUPS lp understands options to set the margins. For 2cm at top and bottom and 3cm at left and right, use this command:

/usr/bin/lp -o page-top=57 -o page-bottom=57 -o page-left=85 -o page-right=85

However, this currently does not work our, for an unknown reason. It does not change anything, i.e. the document is printed as if you selected "Page scaling: none" instead of "Page scaling: fit to printable area". If this command is not possible, another alternative would be to set up a printer definition with exactly the margins you desire, in a way that lets you change these margins easily.

If you just need "larger" margins around your page (without exact measures), you can do the following:

  1. Print the file with Adobe Reader to a PS file. Use the option "Page scaling: fit o printable area". Try several different printers including the "Custom …" special printer to find one that adds margins of the same size all around the page. This step will generate a PS file with larger margins than the original PDF file had, even though this is not correctly shown in the preview of Adobe Reader 8.1.1.
  2. Convert the file to a PDF file by using ps2pdf.
  3. Repeat from step 1 with your new PDF file until the margins are large enough for you.

This method has the obvious disadvantage of not allowing to specify the margins exactly, but at least the file retains vector fonts and graphics (unlike whenusing pstops, see above).

If you need the margin adjustents in combination with Adobe Readers n-up printing (e.g. 2 pages per sheet), and if you can adjust the margins in your source file (before generating the initial PDF), you can do the following: adust the margins in the original file so that, after the n-up scaling, these margins together with the selected printer's margins, result in the margins you desire. When printing one two pages A4 on one sheet A4 with the special prnter "Custom …" in Adobe Reader 8.1.1, the following margins are used (measured in the output A4 page):

  • left 5,25mm
  • right 13,71mm
  • top 6,59mm
  • bottom 6,59mm (probably)

 

 

7 thoughts on “How to scale the page content of PDF files?

  1. Thanks a lot! The Alternative 1 works for me. You need to change the BeginPage section to this:
    “<</BeginPage{0.9 0.9 scale 29.75 42.1 translate}>> setpagedevice”
    It scales the page down by 10%. The size of an a4 page is 595×842 in ghostscript. If you scale it down by 10% you need to adjust the content with the translate command by 5% right and 5% up so it will be 5-5% places on the edges of the paper.

  2. Thomas, great that you could work this thing out! I’m gonna correct it in the article. Thank you!

  3. I realize I’m late to this party, but it’s a high google result and I found the first solution excellent. so I whipped up a script that takes a filename and a scaling factor, (1.1 is 10% bigger, .9 10% smaller) (unix/linux, obviously):

    #! /bin/sh

    #idea from http://ma.juii.net/blog/scale-page-content-of-pdf-files

    IN=`echo $1 | sed -e “s/\(\.pdf\)$//”`
    OUT=${IN}.out.pdf
    IN=${IN}.pdf

    SCALE=$2

    if [ "x$SCALE" = "x" ] ; then
    SCALE=1.1
    fi

    V=`echo “( 1.0 – $SCALE ) * 29.75 * 10″ | bc –`
    H=`echo “( 1.0 – $SCALE ) * 42.1 * 10″ | bc –`

    echo $IN $OUT $SCALE $V $H

    gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER -dCompatibilityLevel=”1.5″ \
    -dPDFSETTINGS=”/printer” -dSubsetFonts=true -dEmbedAllFonts=true \
    -sPAPERSIZE=a4 -sOutputFile=$OUT \
    -c “<> setpagedevice” \
    -f $IN

  4. d’ohh. html problems. i’ll try again

    #! /bin/sh

    #idea from http://ma.juii.net/blog/scale-page-content-of-pdf-files

    IN=`echo $1 | sed -e “s/\(\.pdf\)$//”`
    OUT=${IN}.out.pdf
    IN=${IN}.pdf

    SCALE=$2

    if [ "x$SCALE" = "x" ] ; then
    SCALE=1.1
    fi

    V=`echo “( 1.0 – $SCALE ) * 29.75 * 10″ | bc –`
    H=`echo “( 1.0 – $SCALE ) * 42.1 * 10″ | bc –`

    echo $IN $OUT $SCALE $V $H

    gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER -dCompatibilityLevel=”1.5″ \
    -dPDFSETTINGS=”/printer” -dSubsetFonts=true -dEmbedAllFonts=true \
    -sPAPERSIZE=a4 -sOutputFile=$OUT \
    -c “<</BeginPage{ $SCALE $SCALE scale $V $H translate}>> setpagedevice” \
    -f $IN

  5. crooney, thanks for the contribution, much appreciated! I know code sucks in my comment section ;) but you’re welcome to put it into a Github gist (for example) and link it here.

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>