Playing around with .pdf files

Posted on Mon 26 January 2015 in Code snippets

Here's a bunch of commands that I found useful when working with .pdf files.

1. How to convert an image into a .pdf file?

Install imagemagick:

sudo apt-get install imagemagick

Convert the image:

convert image.jpg output.pdf

2. How to concatenate multiple .pdf files into one .pdf file?

Install pdftk:

sudo apt-get install pdftk

Run:

pdftk p1.pdf p2.pdf p3.pdf p4.pdf p5.pdf output all_pages.pdf

Or, if you have all the files in one directory, you could do

pdftk *.pdf output all_pages.pdf

3. How to rotate all the pages in a .pdf file 90 degrees counterclockwise?

pdftk input.pdf cat 1-endW output rotatedW.pdf

If you want to rotate clockwise:

pdftk input.pdf cat 1-endE output rotatedE.pdf

4. How to set the size of the .pdf file to A4?

Install ghostscript:

sudo apt-get install ghostscript

Run:

gs -o resized_a4.pdf -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dFIXEDMEDIA \ -dPDFFitPage -dCompatibilityLevel=1.4 input.pdf

5. How to convert a colour .pdf file into a greyscale one?

gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET \ -dBATCH -sColorConversionStrategy=Gray \ -dProcessColorModel=/DeviceGray \ -sOutputFile="output_greyscale.pdf" "input_coloured.pdf"

That's it for now!