Post

Ghostscript Cheatsheet

Combine, manipulate, Merge, convert and Extract PDF files using Ghostscript with these powerful ghostscript commands

What is Ghostscript

Ghostscript is an interpreter for PostScript and Portable Document Format (PDF) files. It is used for processing, viewing, and converting documents in these formats. Ghostscript is commonly utilized as a backend for PDF and PostScript printers and for creating PDF documents from a wide range of sources. It can also be used for manipulating and optimizing PDFs, such as merging or splitting documents, and converting them to various image formats.

Below are some useful Ghostscript commands for working with PDF Files.

Install Ghostscript

Type the command sudo apt-get install ghostscript to download and install the ghostscript package and all of the packages it depends on.

Merge 2 pdfs

A simple Ghostscript command to merge two PDFs in a single file is shown below:

1
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=combine.pdf -dBATCH 1.pdf 2.pdf

Combine all the files

1
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=ANDHealth_website_wireframes.pdf -dBATCH homepage.pdf programs_overview.pdf plus.pdf bootcamp.pdf bright_future.pdf investor.pdf alumni.pdf partners.pdf events_archive_custom_post_type.pdf events_single.pdf News_archive.pdf news_single.pdf Rescources_custom_post_type.pdf about.pdf

Convert to v1.4 printer resolution pdf

1
ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Convert to v1.4 screen resolution pdf

1
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -sPAPERSIZE=a5 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=antarctic.pdf Antarctica.pdf

Extract pages 22-36 from a 100-page PDF file

1
2
3
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER \
-dFirstPage=22 -dLastPage=36 \
-sOutputFile=outfile_p22-p36.pdf 100p-inputfile.pdf

Extract all pages as png with transparent background

1
gs -sDEVICE=pngalpha -o file-%03d.png -r144 antarctic.pdf

Out of all the available alternatives I found Inkscape to produce the most accurate results when converting PDFs to PNG. Especially when the source file had transparent layers, Inkscape succeeded where Imagemagick and other tools failed.

1
inkscape "$pdf" -z --export-dpi=600 --export-area-drawing --export-png="$pngfile"

Non transparent background

  • not increase resolution use -r1000
    1
    
    gs -sDEVICE=png16m -o file-%03d.png -r144 antarctic.pdf
    
This post is licensed under CC BY 4.0 by the author.