LinuxCommandLibrary

gs

Convert or view PostScript and PDF files

TLDR

To view a file

$ gs -dQUIET -dBATCH [file.pdf]
copy

Reduce PDF file size to 150 dpi images for reading on a e-book device
$ gs -dNOPAUSE -dQUIET -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -sOutputFile=[output.pdf] [input.pdf]
copy

Convert PDF file (pages 1 through 3) to an image with 150 dpi resolution
$ gs -dQUIET -dBATCH -dNOPAUSE -sDEVICE=jpeg -r150 -dFirstPage=[1] -dLastPage=[3] -sOutputFile=[output_%d.jpg] [input.pdf]
copy

Extract pages from a PDF file
$ gs -dQUIET -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=[output.pdf] [input.pdf]
copy

Merge PDF files
$ gs -dQUIET -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=[output.pdf] [input1.pdf] [input2.pdf]
copy

Convert from PostScript file to PDF file
$ gs -dQUIET -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=[output.pdf] [input.ps]
copy

SYNOPSIS

gs [options] [files]

PARAMETERS

-dNOPAUSE
    Suppresses prompts and pauses during processing, allowing for unattended execution.

-dBATCH
    Exits Ghostscript after processing all input files. Useful for batch processing.

-sOutputFile=filename
    Specifies the output file name. Common for converting to different formats.

-sDEVICE=device_name
    Selects the output device. Examples include pdfwrite (for creating PDFs), png16m (for PNG images), and jpeg (for JPEG images).

-rresolution
    Sets the resolution (in DPI) for raster output devices.

-dSAFER
    Enables safe mode, restricting certain operations to prevent potentially malicious code execution.

-dPDFFitPage
    Scales page to fill the PDF display. Use for viewing purposes.

DESCRIPTION

gs is a powerful command-line interpreter for the PostScript and Portable Document Format (PDF) languages.
It allows users to render, convert, print, and manipulate these document formats.
The gs command acts as a front-end to the Ghostscript library, which provides the core rendering and processing capabilities.
It can be used to convert PDF files to various image formats (such as PNG, JPEG, TIFF), print PostScript files, and perform complex manipulations like merging PDF documents or extracting text. gs supports many output devices, including printers, raster displays, and vector graphics formats.
Ghostscript is widely used in document management systems, printing workflows, and PDF processing tools, due to its flexibility and extensive feature set.

CAVEATS

Ghostscript's extensive options can be overwhelming. Understanding the specific device and its parameters is crucial for achieving the desired output. Some device configurations may require significant memory.

EXAMPLES

Convert a PDF to PNG: gs -sDEVICE=png16m -r300 -o output.png input.pdf
Convert a PostScript file to PDF: gs -sDEVICE=pdfwrite -o output.pdf input.ps

SECURITY CONSIDERATIONS

Always use the -dSAFER option when processing untrusted PostScript or PDF files to mitigate potential security risks associated with malicious code embedded in the documents.

HISTORY

Ghostscript originated in the late 1980s as a free alternative to Adobe PostScript interpreters. It has evolved over decades to become a comprehensive document processing tool, supporting a wide array of formats and devices. Originally developed by Peter Deutsch, it has been crucial in open-source printing and document conversion.

SEE ALSO

ps2pdf(1), pdf2ps(1)

Copied to clipboard