home | codereading | contact | github | math | misc | notes | patches | tech | README

Ghostscript notes

Using Ghostscript to manipulate PDF files

Join PDF files with just a simple command, according to this page:

$ gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=finished.pdf file1.pdf file2.pdf

To generate a file with a subset of its pages, use gs as well:

$ gs -dBATCH -dSAFER -dNOPAUSE -dFirstPage=5 -dLastPage=10 -sDEVICE=pdfwrite -o output.pdf input.pdf

To compress a PDF (resizing the file size):

$ gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dCompatibilityLevel=1.4 -sOutputFile=output.pdf input.pdf

Settings can be /screen, /ebook, /printer and /prepress, ranging from low quality images resolution to better quality. I like to use /ebook. I could turn a 8 MB PDF into a 276 KB file suitable to be sent to e-mail.

Thanks to this page for this tip.

Editing PDF metadata

There are many ways to edit a PDF metadata, using other programs, but this answer (who refers another source) in Ask Ubuntu has a simple way to do that with Ghostscript!

First, create a file with the desired information. Let's call this file pdfmarks:

[ /Title (Document title)
  /Author (Author name)
  /Subject (Subject description)
  /Keywords (comma, separated, keywords)
  /ModDate (D:20061204092842)
  /CreationDate (D:20061204092842)
  /Creator (application name or creator note)
  /Producer (PDF producer name or note)
  /DOCINFO pdfmark

Now, just call Ghostscript passing both PDF file and the pdfmarks file as parameters:

gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=output.pdf no_marks.pdf pdfmarks

Your output.pdf file will be created with the metadata. :-)