GNU Ghostscript

Need to preview and print PostScript Files? Here's a utility that will do just that.
Viewing Images

Viewing images on-screen is only a little more difficult. Ghostscript for Linux comes with both X11 and SVGALib support. The executables for both versions should be in the /usr/bin directory: gs-with-X11 and gs-without-X11. You can run either version using its actual file name, but the more common implementation uses a soft link of the actual executable to the gs command. In either case, the executable's permissions should be setuid root so it can access the display. We'll describe how to use Ghostscript as a PostScript previewer with both the X Window System and SVGALib.

Ghostscript with X11

The X Window System provides a lot of the display support that Ghostscript would need to provide otherwise. As a result, it is by far the way to use Ghostscript as a previewer. As with the above example, we want to pass letter.tex through LaTeX and then convert the output to PostScript with the command line:

pslatex letter.tex; \
        dvips -f <letter.dvi >

Again, we need to specify the DEVICE string to gs, which is simply X11 for the X display. Ghostscript treats an X11 display frame as the standard output, and the X display services provide the geometry to display an entire page. A virtual window manager like FVWM already provides the facilities to scroll the view over the entire page. The ghostscript command, then, is simply:

gs -r72 -sDEVICE=X11
This will provide us with an actual-size page on the screen, because we've overridden the default resolution with the -r switch.

A VGA display provides about 72 dots per inch resolution, so a legible, actual-size full U.S. letter page will not fit on the screen. This is why we rely on FVWM or another virtual window manager to scroll the view across the entire page. Ghostscript draws a page-high window on your X display. About half of the page is visible at a time. You can, of course, view the full page at twice its actual size by specifying your output resolution as 36dpi.

The simplest way to execute the Ghostscript command is in an xterm window. The page appears in a window which displays the child process of the Ghostscript command. Ghostscript writes its messages to standard error, which here is the xterm. Pressing enter in the xterm window tells showpage to display each successive page of Ghostscript output. You can set the default resolution in your ~/.Xdefaults file by adding the lines:

Ghostscript*xResolution: 72 Ghostscript*yResolution: 72

and then merge the defaults with the other X server defaults:

xrdb -merge ~/.Xdefaults
If the resolution isn't specified, the page is displayed on the screen at 300dpi, about one-quarter its actual size, which is visible in a 640x480 view without scrolling.

Ghostscript with SVGALib

Things get a little more difficult when using a VGA display without X11 support. A standard Linux tty device provides no ready-made provision for paging over a full, U.S. letter-size image. Also, Ghostscript's SVGALib routines must be provided with geometry and resolution information to preview images. The following information is specific to my Compaq laptop, Chanel3, which has a 16 color, 640x480 standard VGA display, GNU Ghostscript Version 3.33 and SVGALib version You'll need to adjust the parameters to suit your hardware, but the basic procedure should be similar.

The settings that Ghostscript recognizes for various hardware configurations are listed in use.doc file and the gs man page. The DEVICE string is “linux” for a Linux virtual console; that is:

/dev/tty1 - /dev/tty9

This string corresponds to the virtual console's /etc/termcap entries. The -r resolution, parameter is one of several dozen VGA modes that SVGALib recognizes. Ghostscript defines single-digit mode numbers which correspond to standard width-by-height notation. There is a complete list in the Ghostscript man page. In this case, Mode 4 is 16-color, 640x480 VGA. Much of the following information depends on whether SVGALib provides information on display geometry for your particular display. Ghostscript requires this information to display anything. It must be provided, for one thing, with the aspect ratio of the display (the ratio of the display's width to its height) in order to scale the fonts correctly.

The geometry parameter (-g) tells Ghostscript the display dimensions. Ghostscript scales the output page to the geometry we specify. We'll address this problem in a moment. With these parameters set, our Ghostscript command is:

gs -sDEVICE=linux -r4 -g640x480

Remember that gs is really a link to the executable file /usr/bin/gs-without-X11. Here, we tell Ghostscript to display a full page by specifying a display geometry that's twice the size of the actual screen. This gives us a page that is slightly more than twice as large as the video display. To double the size of the virtual display—the “device space” in PostScript jargon—we use the command line:

gs -sDEVICE=linux -r4 -g640x960
which gives us an actual-size view of the top half of the printed page. To view the bottom half of the page, we can specify an offset of the image's Y origin as half of a U.S. letter-size page:
gs -sDEVICE=linux -r4 -g640x960 -dYO=5.5
The default units are inches. The image's X origin can be shifted similarly.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Emacs Tex mode printing via Ghostscript shell scripts

Anonymous's picture

The section "Ghostscript as a Document Post-Processor and Previewer" of this article sounds very interesting but doesn't work. More explanation would be nice.

Re: Emacs TeX mode printing via Ghostscript shell scripts

Anonymous's picture

Try to isolate the problem. Do the individual utilities run correctly from the shell prompt? Then, do the shell scripts run correctly from the shell? If they don't, then try to include the error messages. If they do, then try to include the debug output from the Emacs interpreter. Also include the version of each utility, so I can duplicate the errors produced by newer versions. Or, you can follow these procedures, and submit a patch.

Robert Kiesling

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState