-->
Previous Table of Contents Next


Redirecting Ghostscript Output

Ghostscript allows you considerable flexibility in redirecting output to other devices. On the command line you can specify a device such as a printer with the -sDEVICE option. For example, if you have a device called HPLaser, you could redirect the output of the gs command to that printer with the command:


gs -sDEVICE=HPLaser filename.ps

If you issue this command, you won’t see any output in windows because the output has been redirected. If you specified multiple files on the command line, such as the following, all the files would have been sent to HPLaser:


gs -sDEVICE=HPLaser file1.ps file2.ps file3.ps

The redirection is in effect until the command terminates. Of course the device you are redirecting to must exist, or gs will return an “unknown device” message. You can see a list of all known devices with the command:


gs -h

A good chunk of the screen (shown in Figure 24.4) lists the devices that Ghostscript knows about.

You can use the order of the arguments to play with Ghostscript a little. Since the order in which arguments are given matters to Ghostscript, you can issue a command like the following, which displays file1.ps and file2.ps on your X session, and then sends file3.ps to the device HPLaser:


gs file1.ps file2.ps -sDEVICE=HPLaser file3.ps

You can change the device on the command line multiple times if you want, so that this next command will send file1.ps to the device HPLaser and file2.ps to the device CanonBJet:


gs -sDEVICE=HPLaser file1.ps -sDEVICE=CanonBJet file2.ps

In some cases it’s easier just to issue separate commands; otherwise the gs command line gets pretty complicated.

Several other features are built into Ghostscript for modifying output devices. If you have an older dot matrix printer with several resolutions (such as draft and final), you can define different options for Ghostscript to trigger each device. For example, the following command tells Ghostscript to print on the device Epson using the highest resolution (24 pin) available:


gs -sDEVICE=Epson -r360x180

Since these older devices are disappearing, these command options are fading from general use.

If for some reason you don’t want to use a printer or screen as output, you can redirect Ghostscript output with the option -sOutputFile, like this:


gs -sOutputFile out1.ps file1.ps file2.ps

Although Linux lets you use pipes and redirection, which are much more capable, this capability was included for those operating systems that are not as powerful as Linux!

Changing Paper Size

By default Ghostscript uses the paper sizes specified in the file gs_statd.ps, which is a configuration file for the printing utility. This file has a couple of dozen paper styles predefined for you, such as all the European and U.S. sizes. To use a size other than the default, use the -sPAPERSIZE option, followed by the gs_statd.ps name that indicates the size you want to use. For example, to use European A4 size, use the command:


gs -sPAPERSIZE=a4 -sDEVICE=HPLaser file1.ps

If the paper size you want to use is not defined already in the gs_stats.ps file, you can modify one of the existing entries or create your own.


Tip:  
Sometimes you’ll change the paper size with the -sPAPERSIZE option only to find Ghostscript has used another paper size. That’s because PostScript (and hence Ghostscript) allows a particular paper size to be embedded in the contents of a PostScript file. Even though you specify a paper size on the command line, the file’s contents overrule this setting. The only way to correct this problem is to manually edit the PostScript file.

Ghostscript Environment Variables

Ghostscript uses a set of environment variables that can alter its behavior. The environment variables used by Ghostscript and their functions are shown in Table 24.2.

Table 24.2. Ghostscript variables and their meanings.

Variable What it does

GS_DEVICE Defines the default output device.
GS_FONTPATH Specifies a list of directories that should be scanned for fonts.
GS_LIB Provides a search path for initialization files and fonts. Directories in the search path are separated by colons.
GS_OPTIONS Defines a list of command line arguments to be processed before the ones actually specified on the command line.
TEMP Defines a directory name for temporary files.

The GS_OPTIONS variable can be used to change the way Ghostscript behaves. For example, the option GS_DEVICE can be used to set the device to some value without requiring you to specify it on each command line. GS_OPTIONS can contain both arguments and options.

The TEMP variable usually points to the /tmp directory on Linux systems, but you can change this. When Ghostscript creates a temporary file it starts the filename with gs_. Unfortunately, Ghostscript doesn’t always delete these files properly so you may occasionally have to go to your /tmp directly and erase any files starting with gs_. If you use Ghostscript a lot, the size of this directory can increase dramatically in short order, so keep an eye on it.


Previous Table of Contents Next