Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add white border to PDF (change paper format)

I have to change a given PDF from A4 (210mm*297mm) to 216mm*303mm.

The additional 6 mm for each dimension should be set as white border of 3mm on each side. The original content of the PDF pages should be centered on the output pages.

I tried with convert:

convert in.pdf -bordercolor "#FFFFFF" -border 9 out.pdf

This gives me exactly the needed result but I loose very much sharpness of the original images in the PDF. It is all kind of blurry.

I also checked with

convert in.pdf out.pdf

which does no changes at all but also screws up the images.

So I tried Ghostcript but did not get any result. The best approach I found so far from a German side is:

gs -sOutputFile=out.pdf -sDEVICE=pdfwrite -g6120x8590 \
-c "<</Install{1 1 scale 8.5 8.5}>> setpagedevice" \
-dNOPAUSE -dBATCH in.pdf

but I get Error: /typecheck in --.postinstall--.

like image 839
DaSteph Avatar asked Aug 18 '14 17:08

DaSteph


2 Answers

By default, Imagemagick converts input PDF files into images with 72dpi. This is awfully low resolution, as you experienced firsthad. The output of Imagemagick is always a raster image, so if your input PDF was text, it will no longer be.

If you don't mind the output PDF's getting bigger, you can simply increase the ratio Imagemagick is probing the original PDF using -density option, like this:

convert -density 600 in.pdf -bordercolor "#FFFFFF" -border 9 out.pdf

I used 600 because it is the sweet spot that works well for OCR. I recomment trying 300, 450, 600, 900 and 1200 and picking the best one that doesn't get unwieldably huge.

like image 156
Karol S Avatar answered Sep 27 '22 01:09

Karol S


Shifting the content on the media is not especially hard, but it does mean altering the content stream of the PDF file, which most PDF manipulation packages avoid, with good reason.

The code you quote above really won't work, it leaves garbage on the operand stack, and the PLRM explicitly states that it is followed by an implicit initgraphics which will reset all the standard parameters anyway.

You could try instead setting a /BeginPage procedure to translate the origin, which will probably work:

<</BeginPage {8.5 8.5 translate} >> setpagedevice

Note that you aren't simply manipulating the original PDF file; Ghostscript takes the original PDF file, interprets it into graphics primitives, then reassembles those primitives into a new PDF file, this has implications... For example, if an image is DCT encoded (a JPEG) in the original, it will be decompressed before being passed into the output file. You probably don't want to reapply DCT encoding as this will introduce visible artefacts.

A simpler alternative, but involving multiple processing steps and therefore more potential for problems, is to first convert the PDF to PostScript with the ps2write device, specifying your media size, and also the -dCenterPages switch, then use the pdfwrite device to turn the resulting PostScript into a new PDF file.

like image 42
KenS Avatar answered Sep 25 '22 01:09

KenS