GhostScript is incredibly powerful tool for manipulating many different document formats, among others also PDF.
Yet, its cryptic command format makes it sometimes hard to get something done; but most of the operations can be simply done by template.
Merging PDF Files
Most often, I need to merge few PDF files into a single file.
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=output.pdf -dBATCH file1.pdf file2.pdf ...
This merges files file1.pdf, file2.pdf, … into a single output.pdf.
-dNOPAUSE means that gs won’t stop along the way to ask you questions, like “do you want to continue?”.
Cutting PDF Files
Are you interested only pages 50 through 60 of the my_file.pdf document?
gs -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=my_cut_file.pdf \ -dFirstPage=50 -dLastPage=60 -dBATCH my_file.pdf
Converting PostScript (or anything else) to PDF
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=output.pdf -dBATCH my_file.ps
GhostScript actually supports incredible array of formats; to check what all you can work with, just run
echo 'devicenames == ' | gs -q | tr " " "\n" | sort