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: pdfScale.sh, using ghostscript (works best!)
Since Thomas enabled this to work (see comments), this is the single best-working alternative in this list. For the latest and most comfortable implementation, use the tavinus pdfScale software. It is very elaborate, including both functions for scaling the content, for resizing the page, and for both.
The history behind that piece of software is really funny, actually. Haven’t seen something like this yet: software emerging “by itself” from a non-working 7 lines of code to an elaborate command-line utility over the course of 10 years. Here’s that history, to give credit where credit is due:
- ReproRog, in this comp.lang.postscript message from December 2007. (not working yet)
- Helge Blischke, adding to it in this comp.lang.postscript message. (not working yet)
- Myself, providing it as an incomplete solution in this post. (not working yet)
- Thomas, getting it to work in the first comment below.
- crooney, extending it into a script in the comments below.
- Walt, improving the script in the comments below.
- Håkon Hægland, improving the script further in this StackOverflow answer.
- user2697605, improving the script further in this StackOverflow answer.
- Michael J. Cole, again improving the script over the previous version and providing it in this Github gist (also, see his comment below and hist StackOverflow answer).
- Gustavo Arnosti Neves, using Michael J. Cole’s version and extending it in the tavinus pdfScale Github repository and working on it a lot, creating an elaborate solution. See also his comment below.
Woah. Nice little demo of the power of open source collaboration 🙂
This solution has no obvious drawbacks. It is quite elegant, includes freely adjustable margins, vector fonts and images, and avoids the GUI hassles. The same instruction should also work with Postscript (PS) files. The core Ghostscript command used in all of the above scripts is this:
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.
Edit 2014-12-27: Integrated a link to Michael’s nicely bundled Github gist version, from the comments below.
Edit 2017-06-18: Added tavinus’ version, and history of how it all happened.
Alternative 2: using pstops
The task can be performed with pstops (whereof psnup is a simplified frontend).
- 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).
- Use pstops to adjust the margins
pstops -p a4 "L@.9(1cm,1cm)" in.ps out.ps
On 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
- 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:
- 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.
- Use ps2pdf to re-destill the PS file to PDF.
- Open the new PDF file in Adobe Reader and zoom and move so that you have an appropriate view, with your desired page margins.
- 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).
- 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 out, 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:
- 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.
- Convert the file to a PDF file by using ps2pdf.
- 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)
Leave a Reply