Learn how to use some commands to create and process PDF files using a Terminal.

Table of Contents

GhostScript

With GhostScript you can make several processing tasks to a PDF file, like compressing.

Following command may seem a bit complex, but this is useful to understand how to use every parameter.

gs \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sDEVICE=pdfwrite \
-dPDFSETTINGS=/printer \
-dAutoFilterColorImages=false \
-dAutoFilterGrayImages=false \
-dDownsampleColorImages=true \
-dDownsampleGrayImages=true \
-dDownsampleMonoImages=true \
-dColorImageResolution=150 \
-dGrayImageResolution=150 \
-dMonoImageResolution=150 \
-dPrinted=false \
-sOutputFile=output.pdf \
input.pdf
  • -dNOPAUSE -dQUIET -dBATCH: By default, gs will show every page of the PDF file and process it, one by one, with a manual confirmation between pages. -dNOPAUSE eliminates the manual confirmation and -dBATCH automatically close gs after the process. -dQUIET hides visual output of the process.
  • -sDEVICE=pdfwrite: this specifies output file format. There are several options: “pdfwrite”, “ps2write”, “png16m”, “jpeg”, etc.
  • -dPDFSETTINGS=/printer: these are predefined templates for processing a PDF. Allowed values are (from worse quality to better): “/screen”, “/ebook”, “/printer” and “/prepress” (More info). There is also a “/default” template. You can overwrite individual settings, and this is what I do with following parameters.
  • -dAutoFilterColorImages=false -dAutoFilterGrayImages=false: I’m not sure what kind of filtering does this parameter, but setting it to “false” makes output to weigh less.
  • -dDownsampleColorImages=true -dDownsampleGrayImages=true -dDownsampleMonoImages=true: this allows to reduce image resolution below the current level.
  • -dColorImageResolution=150 -dGrayImageResolution=150 -dMonoImageResolution=150: this sets image resolution in DPI (dots per inch).
  • -dPrinted=false: if this parameter is equal to “true”, means output will be printed and therefore is not necessary to keep hyperlinks.
  • sOutputFile=output.pdf: in this parameter you type filename of the output.

LibreOffice

Using LibreOffice with the command line is not very common, but it’s useful if you want to use a script to convert a text file to PDF.

soffice \
--headless \
--norestore \
--invisible \
--nodefault \
--nofirststartwizard \
--nolockcheck \
--nologo \
--convert-to "pdf:writer_pdf_Export" \
--outdir /output-folder \
input.docx

GraphicsMagick

This program allows you to convert an image to PDF. It’s as simple as this:

gm convert image.png file.pdf