gs
Convert or view PostScript and PDF files
TLDR
To view a file
Reduce PDF file size to 150 dpi images for reading on a e-book device
Convert PDF file (pages 1 through 3) to an image with 150 dpi resolution
Extract pages from a PDF file
Merge PDF files
Convert from PostScript file to PDF file
SYNOPSIS
gs [options] [input_files]
Common usage to convert PDF to PNG at 300 DPI:
gs -sDEVICE=pngalpha -o output.png -r300 input.pdf
For batch processing multiple pages to JPEG:
gs -dNOPAUSE -dBATCH -sDEVICE=jpeg -r150 -o page%03d.jpg document.ps
PARAMETERS
-sDEVICE=name
Selects the output device for rendering. name can be pngalpha, jpeg, pswrite, pdfwrite, etc.
-o file
Specifies the output filename. Use %d or %0Xd for page numbering with multi-page devices.
-rresXresY or -rres
Sets the X and Y resolution (DPI) for rendering bitmap devices. If one value, it's used for both.
-dname or -dname=value
Defines a PostScript name with a true value (boolean flag) or a specific value. E.g., -dNOPAUSE (no prompts), -dBATCH (exit after files), -dSAFER (safe mode).
-sname=string
Defines a PostScript name with a string value. E.g., -sPAPERSIZE=a4, -sOutputFile=%stdout.
-gwidthxheight
Sets the graphics region (page size) in pixels for bitmap devices.
-q
Operates in quiet mode, suppressing informative messages and the Ghostscript prompt.
-f
Causes `gs` to interpret subsequent arguments as file names to be processed, rather than options.
DESCRIPTION
The `gs` command, short for Ghostscript, is a powerful and versatile interpreter for PostScript and PDF files. It can render these page description languages to various output devices, including printers, display devices (like X11 windows), and image files (e.g., JPEG, PNG, TIFF). It's widely used for viewing PostScript/PDF documents, converting them between formats, processing them for printing, and creating images from them. Ghostscript also includes utilities for creating PDF files from PostScript and for processing PostScript code programmatically.
CAVEATS
Ghostscript can be resource-intensive, especially when rendering high-resolution or complex documents. Running `gs` with untrusted PostScript or PDF files can pose security risks, as these formats are Turing-complete and can execute arbitrary code. It's crucial to use the -dSAFER option when processing untrusted documents to disable file system access and other potentially dangerous operations.
DEVICES
Ghostscript's output capabilities are managed through "devices." Each device specifies how PostScript/PDF output should be rendered (e.g., to a specific image format, a printer driver, or an on-screen display). Users select a device using the -sDEVICE option, enabling flexibility in output format and quality.
OPERATORS
PostScript and PDF are interpreted by `gs` using a sophisticated set of built-in operators. These operators perform various tasks, from drawing shapes and text to managing memory and file I/O. `gs` acts as the execution engine for these operations, processing the commands defined within the document.
HISTORY
Ghostscript was originally developed by L. Peter Deutsch, with its first release under the Aladdin Free Public License (AFPL) in 1990. Over time, its licensing shifted to the GNU Affero General Public License (AGPL) for newer versions, solidifying its role as a cornerstone of open-source document processing on Unix-like systems. It became an essential backend for many printing systems (like CUPS) and PDF manipulation tools, widely adopted due to its robustness and comprehensive PostScript and PDF interpretation capabilities.