Section: Ghostscript (1)
Updated: 8 August 2012
The gs command invokes Ghostscript, an interpreter of Adobe Systems' PostScript(tm) and Portable Document Format (PDF) languages. gs reads "files" in sequence and executes them as Ghostscript programs. After doing this, it reads further input from the standard input stream (normally the keyboard), interpreting each line separately and output to an output device (may be a file or an X11 window preview, see below). The interpreter exits gracefully when it encounters the "quit" command (either in a file or from the keyboard), at end-of-file, or at an interrupt signal (such as Control-C at the keyboard).
The interpreter recognizes many option switches, some of which are described below. Please see the usage documentation for complete information. Switches may appear anywhere in the command line and apply to all files thereafter. Invoking Ghostscript with the -h or -? switch produces a message which shows several useful switches, all the devices known to that executable, and the search path for fonts; on Unix it also shows the location of detailed documentation.
Ghostscript may be built to use many different output devices. To see which devices your executable includes, run "gs -h".
Unless you specify a particular device, Ghostscript normally opens the first one of those and directs output to it.
If you have installed the ghostscript-x Debian package and are under X, the default device is an X11 window (previewer), else ghostscript will use the bbox device and print on stdout the dimension of the postscript file.
So if the first one in the list is the one you want to use, just issue the command
You can also check the set of available devices from within Ghostscript: invoke Ghostscript and type
but the first device on the resulting list may not be the default device you determine with "gs -h". To specify "AbcXyz" as the initial output device, include the switch
For example, for output to an Epson printer you might use the command
gs -sDEVICE=epson myfile.ps
The "-sDEVICE=" switch must precede the first mention of a file to print, and only the switch's first use has any effect.
Finally, you can specify a default device in the environment variable GS_DEVICE. The order of precedence for these alternatives from highest to lowest (Ghostscript uses the device defined highest in the list) is:
Some devices can support different resolutions (densities). To specify the resolution on such a printer, use the "-r" switch:
gs -sDEVICE=<device> -r<xres>x<yres>
For example, on a 9-pin Epson-compatible printer, you get the lowest-density (fastest) mode with
gs -sDEVICE=epson -r60x72
and the highest-density (best output quality) mode with
gs -sDEVICE=epson -r240x72.
If you select a printer as the output device, Ghostscript also allows you to choose where Ghostscript sends the output -- on Unix systems, usually to a temporary file. To send the output to a file "foo.xyz", use the switch
You might want to print each page separately. To do this, send the output to a series of files "foo1.xyz, foo2.xyz, ..." using the "-sOutputFile=" switch with "%d" in a filename template:
Each resulting file receives one page of output, and the files are numbered in sequence. "%d" is a printf format specification; you can also use a variant like "%02d".
You can also send output to a pipe. For example, to pipe output to the "lpr" command (which, on many Unix systems, directs it to a printer), use the option
You can also send output to standard output:
In this case you must also use the -q switch, to prevent Ghostscript from writing messages to standard output.
To select a specific paper size, use the command line switch
Most ISO and US paper sizes are recognized. See the usage documentation for a full list, or the definitions in the initialization file "gs_statd.ps".
Ghostscript can do many things other than print or view PostScript and PDF files. For example, if you want to know the bounding box of a PostScript (or EPS) file, Ghostscript provides a special "device" that just prints out this information.
For example, using one of the example files distributed with Ghostscript,
gs -sDEVICE=bbox golfer.ps
%%BoundingBox: 0 25 583 732 %%HiResBoundingBox: 0.808497 25.009496 582.994503 731.809445
/name 35 def
whereas -sname=35 is equivalent to
/name (35) def
Note that the normal initialization file "gs_init.ps" makes "systemdict" read-only, so the values of names defined with -D, -d, -S, or -s cannot be changed (although, of course, they can be superseded by definitions in "userdict" or other dictionaries.)
The -dSAFER option disables the "deletefile" and "renamefile" operators and prohibits opening piped commands ("cmd"). Only "%stdout" and "%stderr" can be opened for writing. It also disables reading from files, except for "%stdin", files given as a command line argument, and files contained in paths given by LIBPATH and FONTPATH or specified by the system params /FontResourceDir and /GenericResourceDir.
This mode also sets the .LockSafetyParams parameter of the initial output device to protect against programs that attempt to write to files using the OutputFile device parameter. Since the device parameters specified on the command line, including OutputFile, are set prior to SAFER mode, use of "-sOutputFile=..." on the command line is unrestricted.
SAFER mode prevents changing the /GenericResourceDir, /FontResourceDir, /SystemParamsPassword, and /StartJobPassword.
While SAFER mode is not the default, it is the default for many wrapper scripts such as ps2pdf and may be the default in a subsequent release of Ghostscript. Thus when running programs that need to open files or set restricted parameters you should pass the -dNOSAFER command line option or its synonym -dDELAYSAFER.
When running with -dNOSAFER it is possible to perform a "save" followed by ".setsafe", execute a file or procedure in SAFER mode, and then use "restore" to return to NOSAFER mode. In order to prevent the save object from being restored by the foreign file or procedure, the ".runandhide" operator should be used to hide the save object from the restricted procedure.
The locations of many Ghostscript run-time files are compiled into the executable when it is built. Run "gs -h" to find the location of Ghostscript documentation on your system, from which you can get more details. On a Debian system they are in /usr.
When looking for the initialization files "gs_*.ps", the files related to fonts, or the file for the "run" operator, Ghostscript first tries to open the file with the name as given, using the current working directory if no directory is specified. If this fails, and the file name doesn't specify an explicit directory or drive (for instance, doesn't contain "/" on Unix systems), Ghostscript tries directories in this order:
Ghostscript, or more properly the X11 display device, looks for the following resources under the program name "Ghostscript":
See the usage document for a more complete list of resources. To set these resources on Unix, put them in a file such as "~/.Xresources" in the following form:
Ghostscript*geometry: 612x792-0+0 Ghostscript*xResolution: 72 Ghostscript*yResolution: 72
Then merge these resources into the X server's resource database:
% xrdb -merge ~/.Xresources
See http://bugs.ghostscript.com/ and the Usenet news group comp.lang.postscript.
Artifex Software, Inc. are the primary maintainers of Ghostscript. Russell J. Lang, gsview at ghostgum.com.au, is the author of most of the MS Windows code in Ghostscript.
Tutoriais de Tecnologia Web