LinuxCommandLibrary

ghostscript

Interpret and render PostScript and PDF documents

SYNOPSIS

gs [options] [input_files | PostScript_code]

Common examples:
To convert a PostScript file to PDF:
gs -sDEVICE=pdfwrite -o output.pdf input.ps

To convert a PDF file to a high-resolution PNG image:
gs -sDEVICE=png16m -r300 -o output.png input.pdf

To display a PDF document:
gs document.pdf

PARAMETERS

-sDEVICE=<device>
    Specifies the output device (e.g., pdfwrite, png16m, jpeg, pswrite).

-o <file>
    Sets the output file name. Use - for standard output.

-r<res>
    Sets the X and Y resolution (e.g., -r300 for 300 DPI). Can be -rXresxYres.

-dBATCH
    Exits Ghostscript after processing all input files.

-dNOPAUSE
    Disables the prompt and pause between pages.

-dSAFER
    Enables safer mode, restricting file system access and shell escapes (default in recent versions).

-dQUIET
    Suppresses informational messages and prompts.

-g<width>x<height>
    Sets the device width and height in pixels for raster devices.

-sPAPERSIZE=<size>
    Sets the paper size (e.g., a4, letter, legal).

-c '<PostScript_code>' -f
    Executes PostScript code directly from the command line, then processes files.

-I<directory>
    Adds a directory to the search path for fonts and other files.

-dPDFFitPage
    Scales PDF pages to fit the output device's paper size.

-dCompatibilityLevel=<level>
    Sets the PDF compatibility level (e.g., 1.4, 1.5) for pdfwrite device.

DESCRIPTION

Ghostscript is a versatile interpreter for the PostScript language and PDF (Portable Document Format). It serves as a core technology for rendering these document formats for display on various devices, printing, or conversion into other formats.

Beyond simple viewing and printing, Ghostscript functions as a powerful imaging engine. It can convert PostScript and PDF files into a multitude of image formats like JPEG, PNG, TIFF, and more, at specified resolutions and color depths. It's often used programmatically by other applications such as PDF viewers, print spoolers (like CUPS), and document processing tools for tasks like rasterization, compression, and format conversion.

Developed by Artifex Software and available under both open-source (GNU AGPL) and commercial licenses, Ghostscript is an indispensable utility in Unix-like operating systems, providing robust and flexible document manipulation capabilities.

CAVEATS

Running Ghostscript with untrusted PostScript or PDF files can pose security risks, as these formats can execute arbitrary code. While -dSAFER mode (often default) mitigates many risks, it's not foolproof. High-resolution rendering or complex documents can consume significant memory and CPU resources. Achieving desired output quality sometimes requires fine-tuning numerous options, which can have a steep learning curve.

OUTPUT DEVICES

Ghostscript's power lies in its extensive range of output devices. These include various image formats (e.g., pngalpha, jpeg, tiffg4), vector formats (e.g., pswrite, eps2write), and the critical pdfwrite device for PDF generation and manipulation. The choice of device dictates the output file type and available options.

COMMON USE CASES

Beyond simple viewing, Ghostscript is frequently used for:
Converting PostScript/PDF to raster images for web or display.
Creating smaller, optimized PDF files from larger ones.
Merging multiple PostScript/PDF files into a single document.
Extracting specific pages from a PDF.
Converting between PostScript and PDF formats.
Rendering documents for various printers, especially those that don't natively support PostScript or PDF.

HISTORY

Ghostscript was first developed by L. Peter Deutsch and released in 1986 under the name Ghostscript. It quickly became the de facto standard for interpreting PostScript outside of Adobe's proprietary systems. Its open-source nature, initially under the GNU GPL, contributed to its widespread adoption in Unix-like operating systems and as a core component in many printing systems, including CUPS (Common Unix Printing System). Over the years, its capabilities expanded significantly to include comprehensive PDF interpretation and a wide array of output devices, maintained by Artifex Software.

SEE ALSO

ps2pdf(1): PostScript to PDF converter (often a Ghostscript wrapper), pdftops(1): PDF to PostScript converter (from Poppler utilities), convert(1): ImageMagick utility for image conversion, often uses Ghostscript for PDF/PS, lpr(1): Print files, qpdf(1): Tool for structural, content-preserving transformations on PDF files, pdfinfo(1): PDF document information extractor

Copied to clipboard