Introduction to GNU Ghostscript

Ghostscript is an interpreter for the PostScript language. A PostScript interpreter usually takes as input a set of graphics commands. The output is usually a page bitmap which is then sent to an output device such as a printer or display. PostScript is embedded in many printers.

Ghostscript has several main uses:

  1. Display a PostScript or PDF file (avoid killing trees).
  2. Convert a PostScript file to a PDF (or vice versa)
  3. Display a PostScript file to decide if you really need to print it (reduce the number of trees killed).
  4. Print a PostScript file to a non-PostScript printer (kill more trees).

An example of a very simple PostScript file is:

  /Helvetica findfont 72 scalefont setfont
  72 72 moveto
  (Hello, world!) show
The first line is a PostScript comment. It is ignored by the PostScript interpreter, but enables other programs to recognise that the file contains PostScript commands. The second line says to find the Helvetica font, scale it to 72 points high (1 inch) and then set it as the current font. The third line moves the current point to 1 inch in from the left and bottom edges of the page. The fourth line draws the text into the page bitmap at the current point. The final line causes the page bitmap to be output to paper or the display.

The information below assumes the use of GNU Ghostscript 6.51. Other versions of Ghostscript are similar. The Ghostscript program is typically found in /usr/local/bin/gs
Ghostscript needs some initialization files in /usr/local/lib/ghostscript/6.51
and some fonts in /usr/local/lib/ghostscript/fonts.
The documenation files are in /usr/local/lib/ghostscript/6.51/doc
and some example PostScript files are in /usr/local/lib/ghostscript/6.51/examples.

Displaying a PostScript file

Let's assume that you wish to display the above PostScript example and it is in a file named in the current directory. If Ghostscript has been correctly installed, then you would start Ghostscript using:
If Ghostscript has not been correclty installed, you will need to specify the full path, for example:
    /usr/local/bin/gs -I/usr/local/lib/ghostscript/6.51:/usr/local/lib/ghostscript/fonts
The -I option tells Ghostscript to look for its configuration files and fonts in the two directories listed. After Ghostscript starts it will display
  GNU Ghostscript 6.51 (2001-06-06)
  Copyright (C) 2001 artofcode LLC, Benicia, CA.  All rights reserved.
  This software comes with NO WARRANTY: see the file COPYING for details.
An X11 image window will appear. Ghostscript is now waiting for you to tell it what to do. Typing ( run will produce
  GS>( run
  Loading NimbusSanL-Regu font from /usr/local/lib/ghostscript/fonts\n019003l.pfb... 
    1669824 366951 1300076 11892 0 done.
  >>showpage, press <return> to continue<<
The text "Hello, world!" will appear near the bottom of the X11 image window. When you have finished viewing the image window, switch to the text window and press the <return> key. You will be returned to the GS> prompt. Type quit to close Ghostscript.
If the file is not in the current directory, then you will need to give a full path. For example, (/user/john/ run.

So now you know how to display a PostScript file. The same instructions apply for .pdf files as well. Some example PostScript files that come with Ghostscript are, and These should be in the examples/ directory under the ghostscript tree.

Printing a PostScript file

In the above display example, we did not specify which output device Ghostscript should use. Ghostscript used the default device, which happened to be the display. To print a PostScript file, we need to tell Ghostscript the name of the printer device. For an HP DeskJet 500 or an HP DeskJet Portable printer, this is djet500. We would instead start Ghostscript with:
  gs -sDEVICE=djet500
The output will be sent to a scratch file. Ghostscript will come up with the GS> prompt again, we give it a file name, and after processing each page it will pause. This is probably undesirable. A better way to start Ghostscript is:
  gs -sDEVICE=djet500 -dNOPAUSE -c quit
This tells Ghostscript not to pause after each showpage, to interpret the file and afterwards to exit. This is much more useful.

To get a list of available printer devices, start Ghostscript for displaying, then type:

  GS>devicenames ==
The destination for the printed output can be specified on the command line. To output to the file out.prn, add -sOutputFile="out.prn" somewhere before the name of the PostScript file. To pipe the output to the lpr program, use -sOutputFile="\|lpr queuename"

More details

If you want more details about the Ghostscript command line options, see the file Use.htm distributed with Ghostscript.

A better way

If you are like me, you get sick of typing the long commands above. A program called gv and several derivatives exist for X11 on Unix or VMS. This allows you to display page 20 without having to display pages 1, 2, 3, 4, ... 20. You can also print selected pages.

You may also find useful the ggv application distributed with the GNOME desktop environment.