Skip to content

This document is a WORK IN PROGRESS.
This is just a quick personal cheat sheet: treat its contents with caution!


Ghostscript

Ghostscript is an interpreter for the PostScript language and PDF files. It consists of a PostScript interpreter layer and a graphics library.

There are a family of other products, including GhostPCL, GhostPDF, and GhostXPS that are built upon the same graphics library (note that GhostPDL, pulls all these languages into a single executable). Between them, this family of products offers native rendering of all major page description languages.

Reference(s)

Table of contents


Install

# emerge -a app-text/ghostscript-gpl
# pacman -S ghostscript
# nix-env -iA nixos.ghostscript
# nix-env -iA nixpkgs.ghostscript
# apt install ghostscript
# yum install ghostscript
# dnf install ghostscript

PDF manipulations

  • Compress PDF:
    $ gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed_PDF_file.pdf input_PDF_file.pdf
    

The dPDFSETTINGS option allows the following levels of compression: * /prepress: (default) higher quality output (300 dpi) but bigger size * /ebook: medium quality output (150 dpi) with moderate output file size * /screen: lower quality output (72 dpi) but smallest possible output file size

The -r option allows you to specify directly the resolution you want (e.g. -r42 for 42 dpi.

You can add the following to your zshrc:

pdfcompress ()
{
    gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=32 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=32 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=32 -dNOTRANSPARENCY -sOutputFile=${1%.*}_compressed.pdf $1;
}

  • Merge PDFs:

    $ gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf mine1.pdf mine2.pdf
    

  • Merge with compression:

    $ gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages -dCompressFonts=true -r150 -sOutputFile=merged.pdf mine1.pdf mine2.pdf
    

  • Convert PNG to PDF (the imagemagick package might be needed?):

    $ convert input.png output.pdf
    

  • Convert PDF to PNG (the imagemagick package might be needed?):

    $ convert output.pdf input.png
    

    If the the background is converted into a transparent background, then just add the flatten option: $ convert -flatten output.pdf input.png.

WIP


If this cheat sheet has been useful to you, then please consider leaving a star here.