Manipulating .pdf files on Linux using Ghostscript

by gorthx

I have to digitally fold, spindle, and mutilate .pdf documents frequently. On Ubuntu, I tried the GiMP, pdftops, pdftk, and some truly tortuous gymnastics involving screencaps, but none of them really did what I wanted.

Then I found Ghostscript.

It’s a command line tool, which I dig, because it means that I can type instead of having to point & click, and I can write quick shell scripts to do my dirty work.

Here’s how I use it most often:

Combine multiple .pdfs into a single file:
gs -sDEVICE=pdfwrite \
-o 2012_final_report.pdf \
2012-*_receipts.pdf

Pull first page only from multiple files:
for each in `ls 2012_Account_Statement_*`
do
cp $each ${each}.backup
gs -sDEVICE=pdfwrite \
-dFirstPage=1 -dLastPage=1 \
-o ${each%.pdf}_firstpage.pdf \
${each}
done

Combine multiple .pdfs and convert them to B&W:
gs -sDEVICE=pdfwrite \
-sColorConversionStrategy=Gray \
-dProcessColorModel=/DeviceGray \
-dCompatibiltyLevel=1.4 \
-dAutoRotatePages=/None \
-o 2012_final_report.pdf \
2012-*_receipts.pdf

The Ghostscript Quick Start guide is here.

Advertisements
Tags: ,
%d bloggers like this: