LinuxCommandLibrary

gs

Convert or view PostScript and PDF files

TLDR

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] [input-files]

PARAMETERS

-dBATCH
    Exit after processing all files, no interactive prompts

-dNOPAUSE
    Disable page pauses, process continuously

-dSAFER
    Sandbox mode; restricts file access for security

-dQUIET
    Suppress routine startup messages

-sDEVICE=device
    Select output device (e.g., pdfwrite, png16m, x11)

-sOutputFile=file
    Specify output filename or pattern

-o output-file
    Shorthand for -dBATCH -dNOPAUSE -sOutputFile=file

-dNOPROMPT
    Skip interpreter prompt and execute file

-dTextAlphaBits=n
    Set antialiasing bits for text (1-4)

-dGraphicsAlphaBits=n
    Set antialiasing bits for graphics (1-4)

-rx[xy]
    Set resolution in DPI (e.g., -r300)

-dFirstPage=n
    Start processing from page n

-dLastPage=n
    Stop after page n

DESCRIPTION

Ghostscript (gs) is a versatile interpreter for the PostScript language and PDF files, capable of rendering high-quality graphics, converting between formats, and processing vector images. It serves as the foundation for many printing systems, document viewers, and conversion tools on Unix-like systems.

Primarily used for viewing, printing, or converting PostScript (.ps), Encapsulated PostScript (.eps), and PDF files, gs supports numerous output devices including screen display, printers, and file formats like PDF, PNG, JPEG, and TIFF. It excels in batch processing for document workflows, such as compressing PDFs or rasterizing images.

The command processes input files by interpreting page descriptions, applying device-specific rendering, and outputting results accordingly. Interactive mode allows manual page navigation, while options enable automation. Widely used in publishing, it handles complex graphics, fonts, and color management effectively.

CAVEATS

PostScript/PDF files may execute arbitrary code; always use -dSAFER. High memory usage for large files. Deprecated devices may be removed in future versions.

INTERACTIVE USE

Run gs without files for interactive mode: navigate with <Enter> (next), <q> (quit).

DEVICE LIST

Query devices with gs -h or man gs; common: pdfwrite (PDF out), pngalpha (PNG), jpeg, tiff24.

EXAMPLES

gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -sOutputFile=out.pdf input.ps
gs -sDEVICE=x11 input.pdf (view on screen)

HISTORY

Developed in 1988 by Thomas and L. Peter Deutsch as an open-source PostScript interpreter. Acquired by Artifex Software in 1990s; AGPL-licensed with commercial variants. Evolved to support PDF 1.7 and modern features.

SEE ALSO

ps2pdf(1), pdf2ps(1), gv(1), evince(1), convert(1)

Copied to clipboard