Introduction to Ghostscript


Ghostscript is an interpreter for the PostScript (TM) 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 file (avoid killing trees).
  2. Display a PostScript file to decide if you really need to print it (reduce the number of trees killed).
  3. 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
  showpage
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 Windows 95 or NT, but Windows 3.1, OS/2 and Unix are similar.

Displaying a PostScript file

Let's assume that you wish to display the above PostScript example and it is in a file named example.ps in the current directory. If Ghostscript is in the directory c:\gs3.53 and its fonts are in c:\gs3.53\fonts, then you would start Ghostscript using:
    c:\gs3.53\gswin32.exe -Ic:\gs3.53;c:\gs3.53\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
  Aladdin Ghostscript 3.53 (1996-1-10)
  Copyright (c) 1995 Aladdin Enterprises, Menlo Park, CA.  All rights reserved.
  This software comes with NO WARRANTY: see the file PUBLIC for details.
  GS>
Ghostscript is now waiting for you to tell it what to do. Typing (example.ps) run will produce
  GS>(example.ps) run
  Loading NimbusSansL-Regular font from C:\gs3.53\fonts\n019003l.gsf... 
   1758852 452265 1300076 12334 0 done.
  >>showpage, press <return> to continue<<
An image window will appear with the text "Hello, world!" near the bottom. You will need to scroll down to see it. 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.
  GS>quit
If the file is not in the current directory, then you will need to give a full path. You must use forward slashes to separate directory names - do not use MS-DOS back slashes. For example, (c:/gs3.53/example.ps) run.

So now you know how to display a PostScript file. Some example PostScript files that come with Ghostscript are colorcir.ps, chess.ps and tiger.ps. These should be in the c:\gs3.53 directory.

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:
  c:\gs3.53\gswin32.exe -Ic:\gs3.53;c:\gs3.53\fonts -sDEVICE=djet500
The output will be sent to the default printer queue. For the Windows and OS/2 version, this does not happen until Ghostscript exits. 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:
  c:\gs3.53\gswin32.exe -Ic:\gs3.53;c:\gs3.53\fonts -sDEVICE=djet500 
      -dNOPAUSE example.ps -c quit
This tells Ghostscript not to pause after each showpage, to interpret the file example.ps 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 ==
What if the printer isn't on the default queue? The destination for the printed output can be specified on the command line. To output to the file c:\out.prn, add -sOutputFile="c:/out.prn" somewhere before the name of the PostScript file. To output to a Windows 95 queue named HP DeskJet Portable Printer, use -sOutputFile="\\spool\HP DeskJet Portable Printer". Get the spelling exact or it won't work. For Windows 3.1 or Win32s, to output to port LPT2:, use -sOutputFile="LPT2:"

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. I wrote a program called GSview to avoid having to remember or type all these commands. To display a file you select File | Open from the menu. To print a file you select File | Print from the menu, then select your printer from the presented list. Other advantages are the ability to display page 20 without having to display pages 1, 2, 3, 4, ... 20. For printing you can tell it to print an arbitrary selection of pages. GSview is available for Windows, OS/2 and X11/Linux.

A similar program called GV exists for X11 on Unix or VMS.


PostScript is a trademark of Adobe Systems.
Last updated 2000-12-15
If you have comments about this page, please email Russell Lang