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

Table of Contents


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 \
-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 \
  • -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 (equivalent to -q).
  • -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 the 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.

Some examples

# Create a preview (a PDF with the first pages)
gs -dNOPAUSE -dBATCH -dQUIET -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=5 -sOutputFile=preview.pdf input.pdf
# Export a single-page PDF as a JPEG image (quality: 80%)
gs -dNOPAUSE -dBATCH -q -sDEVICE=jpeg -dJPEGQ=80 -sOutputFile=image.jpg input.pdf
# Compress a PDF using the 'ebook' template
gs -dNOPAUSE -dBATCH -q -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -sOutputFile=output.pdf input.pdf


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.

# This will use default settings for PDF export
soffice --convert-to pdf --outdir /output-folder input.docx


This program allows you to convert one or several images to PDF. It’s as simple as this:

gm convert image1.png image2.png file.pdf


See Poppler: command-line PDF tools.


See Encrypting PDFs and How to flatten PDF forms to avoid compatibility errors.

Test with this online terminal:

If you have any suggestion, feel free to contact me via social media or email.