PDF Tips & Tricks

by levien on ma 18 juli 2011 // Posted in misc // under

Repairing a damaged PDF-file

Ghostscript can "repair" some damaged or buggy PDF-files, by regenerating them. Try something like:

ps2pdf damaged.pdf fixed.pdf

Believe it or not but ps2pdf will happily accept PDF as input, and from it create a new PDF... You can use all the usual Ghostscript options. If you don't specify any, default values will be used to generate the output PDF. But for instance, if you're generating a high-quality PDF for printing, try:

ps2pdf -dPDFSETTINGS=/prepress damaged.pdf fixed.pdf

This is needed to fix PDF files generated by OpenOffice.org 3.2.1, which have invalid xref entries ([bug #615812][https://bugzilla.novell.com/show_bug.cgi?id=615812]). These PDF files can be viewed in Ubuntu and MacOS X, but cannot be printed in the Windows version of Adobe Reader.

Alternatively, you can use pdfopt, which uses Ghostscript to linearise a PDF file, and should also repair any damage it encounters (where possible).

Another method of regenerating a PDF-file is by running it through pdftk:

pdftk in.pdf output out.pdf

This should fix any problems in the input PDF-file (where possible), and produce a linearised output PDF. It may also be required to work around a bug in the 1.0.1 version of OpenOffice.org PDF import extension, which makes that it only accepts linearised PDF files as input. All other PDF files produce the rather useless message "General Error. General input/output error". Running them through pdftk should fix this.

Printing multiple PDF pages per page

To save paper (and weight in my bag), I usually prefer to print PDF files double-sided, with two PDF pages on each side of the paper. This is also called 2-up (or generally N-up) printing. Some programs and printer drivers have a setting for this (e.g. Windows-drivers for many laserprinters, and Evince in Linux, although this does not always work). Outside of printing-support,there are (at least) two ways of doing this on a Linux machine. The easiest is to install the package pdfjam and use the command pdfnup, e.g. to combine two portrait pages on one landscape A4 page:

pdfnup --nup 2 --orient landscape --outfile output2up.pdf input.pdf

There's even a nice Qt GUI for it: pdfnup-gui

Making folded (e.g. A5 or A4) booklets from a PDF-file

Either follow this guide, which requires converting to Postscript and using psbook and psnup: http://www.peppertop.com/blog/?p=35

Or download and compile this little program: http://www.ctan.org/tex-archive/support/pdfbook/

In Ubuntu, it depends on the following packages: texlive-latex-recommended (for pdfpages) and poppler-utils or xpdf-utils (for pdfinfo)

It works like a charm, e.g. to create an A5-booklet try something like:

pdfbook -p a4 -2 input.pdf booklet.pdf

Converting PDF to PNG

Sometimes you need to convert a vector image in PDF or PostScript (PS/EPS) into a bitmap image. For instance, if an application doesn't support vector images, has trouble rendering them correctly, or when an image is just too complex to render rapidly (e.g. huge graphs). There are many ways to perform this conversion, and almost all use Ghostscript internally.

For maximum control you can call Ghostscript directly, e.g. to render a PDF file into a 600 dpi colour PNG:

gs -dSAFER -dBATCH -dNOPAUSE -dEPSCrop -sDEVICE=png16m -r600 -sOutputFile=output.png input.pdf

However, you can also let ImageMagick do the dirty work for you:

convert -density 600x600 -unit PixelsPerInch input.pdf output.png

Or even:

convert -density 600 input.pdf output.png

More PDF Tips & Tricks