How to change the margins of PDF files while keeping the page size (”scale the content”)?
August 2nd, 2008 by matthias
This is on how to achieve that on Linux. There are multiple alternatives, but none of them proved perfect yet. Let’s start.
Alternative 1: 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.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 - 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 2: 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:
- 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)
Alternative 3: Using Adobe Reader and printing the “current view”
Another alternative:
- Use Adobe Reader to print your page to a large sheet without scaling.
- Use ps2pdf to re-destill the PS file to PDF.
- Open the new PDF file in Adobe Reader and choose an appropriate view. Use the “print current view” option in Adobe Reader to print exactly thet view.
- Re-destill the PS output to PDF.
- That way, you can even achieve n-up mounting: say you generated to pages, each with content in different places and the rest white space. You can overlay these pages on top of each other using pdftk with the background option.
Alternative 4: Using ghostscript
The following would constitute an elegant solution: freely adjustable margins and vector fonts and images, and avoiding the GUI hassles. Sad enough, it does not work yet. The idea come from this thread.
gs \
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
-dCompatibilityLevel="1.3" -dPDFSETTINGS="/printer" \
-dSubsetFonts=true -dEmbedAllFonts=true \
-sPAPERSIZE=a4 -sOutputFile="out.pdf" \
-c "<</BeginPage{0.5 0.5 scale -90 rotate -2384 0 translate}>> setpagedevice" \
-f in.pdf
The instruction works, apart from the important -c option part, which adds a postscript command. This instruction would also work with PDF files.
Posted in Acrobat Reader, DTP, Sprache: Englisch, alle Artikel |