Printing Complex Documents with Ghostscript

Have you ever tried printing a complex word-processing document or a Web page in Linux only to have it come out as complete garbage on your printer, with strange characters and all sorts of gobbledygook? That happens because some Linux programs, like Netscape, use PostScript, which is a page-description language that only some printers can understand and handle properly. Why is this so? Maybe this practice was inherited from old-style Unix publishing systems and Macintoshes, which use PostScript. Or maybe not.

Have you ever tried printing a complex word-processing document or a Web page in Linux only to have it come out as complete garbage on your printer, with strange characters and all sorts of gobbledygook? That happens because some Linux programs, like Netscape, use PostScript, which is a page-description language that only some printers can understand and handle properly. Why is this so? Maybe this practice was inherited from old-style Unix publishing systems and Macintoshes, which use PostScript. Or maybe not.

Odds are that there really isn’t a good explanation, and if there is one, it’s probably too complicated to cover in a column. Whatever the case, the bottom line is that if you want your documents to print perfectly, you have to make your applications and your printer talk the same lingo. And that, in turn, means you have to make one of them work in a language it does not normally understand.

To print PostScript documents with Linux, the stream of data intended for the printer must be re-directed to an open source PostScript interpreter program called Ghostscript, which then translates it to your printer’s native language. Different printer manufacturers use different native languages — HP LaserJet and LaserJet-compatible printers, for example, use a language called PCL, or Printer Control Language. Other manufacturers have different languages that their printers speak, but Ghostscript can translate PostScript, PDF, and TCL to most of them. Unfortunately, if you don’t have your system set up to filter PostScript properly, or if your Ghostscript program is horrendously out of date (more on this problem later) then your print job will end up looking like a complete mess.

Setting up Your Print Queue

While this article isn’t long enough to cover in detail the Unix printing system that Linux uses, you’ll be happy to know that for the most part, as an end user, you don’t really have to know that much about it.








Desktop/Fig 1
Figure One: Red Hat’s printtool can configure your printing services.

Suffice it to say that Linux’s printing system is made up of a bunch of small programs that take the output from your application, filter the output (through Ghostscript), and then send it along to the printer. This is referred to as the lpd system. It’s named after the Unix line-printing daemon, which is the main program in the chain.

Unless you really know what you’re doing, you don’t want to try manually editing the lpd configuration files and the filter that is used to invoke Ghostscript and stream the translated data to your printer. Otherwise, you could end up losing some hair — and in my case, I want to keep most of the hair I have. Luckily, virtually every Linux distribution in existence has some type of graphical program or installation utility that sets up lpd.

Red Hat Linux and Linux-Mandrake both have great utilities for setting up your printer — Red Hat has printtool (see Figure One) and Mandrake has DrakConf, both of which allow you to easily configure lpd’s /etc/printcap file using a GUI.

To help you get a better understanding of what your /etc/ printcap file looks like after one of these utilities is used, see Listing One.




Listing One: The /etc/printcap File


#
# Please don’t edit this file directly unless you know what you are doing!
# Look at the printcap(5) man page for more info.
# Be warned that the control-panel printtool requires a very strict format!
# Look at the printcap(5) man page for more info.
#
# This file can be edited with the printtool in the control-panel.
#
##PRINTTOOL3## LOCAL ljet4 600×600 letter {} LaserJet4 Default 1
lp:\
:sd=/var/spool/lpd/lp:\
:mx#0:\
:sh:\
:lp=/dev/lp0:\
:if=/var/spool/lpd/lp/filter:

Basically this tells lpd that when it receives a print job it should spool everything to the local printer port /dev/lp0 (which is Unix-speak for what Windows folks call LPT1:) and determine what kind of data it is by executing the logic in /var/spool/ lpd/lp/filter. If you take a close look at what’s in that file, you’ll see a fairly complex shell script that, if you’re anything like me, you’ll want to avoid like the plague.

In any case, once you’ve configured your lpd with your distro’s GUI configuration tool, you’ll be able to print out documents with gusto. Maybe.

The Problem with GNU GhostScript

Okay. Here’s where we get into one of the seedier neighborhoods of OpenSourceLand, and a political subject that I feel should finally be addressed and resolved by the Linux vendors. Many Linux distributions come with Ghostscript, which is an independent implementation of Adobe’s PostScript language. Adobe has made modifications and improvements to its PostScript language over time, and thus has released PostScript Level 2, and recently, PostScript Level 3. Many of the more serious desktop-publishing programs like FrameMaker and Quark XPress, which were once Macintosh-only products, used PostScript as their native print-rendering language. Way back when, it used to be that only very expensive laser printers had Adobe PostScript embedded in them, but you could get around the problem by using a software interpreter. At one point several companies marketed such programs, but as with many things in the computer-software industry, one company’s PostScript interpreter, Aladdin’s Ghostscript, ended up owning the lion’s share of the market.

Over the years, Aladdin has improved Ghostscript and made its software open source and free to the extent that businesses and individuals could download it and use it without charge. But here’s where it gets complicated: If you want to bundle Ghostscript or use it as part of a product, you must pay Aladdin’s licensing company, Artifex Software, a fee. In fact, many companies, like Xerox, have no problem paying Artifex to use Ghostscript in their products. Unfortunately, most Linux companies just refuse to allow (and pay for) any commercial software to be used in their free distributions, which puts Aladdin in a difficult position. Because it feels a duty to support the user community it periodically releases a build of Ghostscript under the GPL that is one version behind, called GNU Ghostscript that GPL-conscious Linux companies can put in their distros.

The current version of GNU Ghostscript is 5.50, which was released in February 2000. Unfortunately, almost no current distros at the time of this writing are even that up to date — most of them use version 5.10, which has an incomplete implementation of PostScript Level 3. This situation can make life difficult, or at least confusing, for users. If your application renders pages by using some of the more advanced features of PostScript, you could end up with complete garbage as output, and no clue whatsoever as to what’s gone wrong. It usually just looks like Linux is broken or perhaps like your printer is incompatible.

Of course, if some of the Linux companies like Red Hat, Caldera, Corel, SuSE, TurboLinux, and Mandrake decided to pool their resources, they could pay the license fee and this issue would go away faster than you could say “paper jam.” (And by the way, if any of you Linux CEOs are reading this article, I’m sure that Miles Jones, president of Artifex, would like to have a few words with you.)

Fortunately, there is a solution to this incompatibility problem that’s true to Linux’s “do it yourself” nature — you can download the latest version of Ghostscript from Aladdin directly. The most current version of Aladdin Ghostscript is 6.01, and you can find out about it at http://www.cs.wisc.edu/~ghost/aladdin/index.html. Ghostscript is distributed as both source code and precompiled binaries packaged as RPM files, and there are versions available for Linux, Windows, OS/2, and the Mac. You should also download and install the accompanying font files, which Ghostscript needs to print text.

Printing and Viewing PostScript Files

In the event that the printed output from your application looks like garbage, there are a few things you can do to debug the problem. First, you can print your document to a PostScript file. This can be very handy if you want to save copies of Web pages with all of their graphics and formatting intact and then print or distribute them later.








Desktop/Fig 2
Figure Two: Netscape printing to a PostScript file.

In Netscape, for example, you can opt to print to a PostScript file, as shown in Figure Two .

You can then view and print the file with the Ghostscript viewer, or gv. The syntax for launching the viewer is:


gv filename.ps

I normally print to PostScript files when using Netscape, because for some reason it has a nasty tendency to mangle the output if you print from it directly. I can then use gv (as shown in Figure Three) to verify that the pages were rendered okay, and I can then print them as needed. Many other X applications, like the Gnumeric spreadsheet program and the GIMP, will optionally print or save to PostScript files as well.








Desktop/Fig 3
Figure Three: Aladdin’s handy GhostView application, or gv, can be used to display a PostScript file.

You can also encode any text file of your choosing as a PostScript file. This is helpful if you’ve done a lot of text formatting and you want it to come out exactly the way it looks in your text editor. You can perform this conversion with the enscript utility, which is preloaded on most Linux distros, and uses the following basic syntax:


enscript
sourcefilename -p
outputfilename.ps

If you simply type the command enscript followed by the filename, the program will encode your file as PostScript and send it directly to the printer. You can display the extended help for the utility by typing:


enscript -help

Yet another cool option for working with PostScript files is turning them into Adobe Acrobat files. Ghostscript comes with a utility called ps2pdf, which will allow you to convert any .ps file into a .pdf file that you can distribute to anyone who has the Adobe Acrobat reader. As just about anyone who’s surfed the Web knows, Acrobat files are a very popular way to distribute electronic documents because they can handle complicated files with extended formatting and images, yet they can be viewed on many different platforms and are typically much smaller than PostScript files. Adobe offers a way to do this same conversion of PostScript files to PDF files, in the form of its Distiller program, which is part of the commercial version of Acrobat.

To encode a PostScript file as an Acrobat document with ps2pdf, simply use the command:


ps2pdf inputfile.ps

You should then see inputfile.pdf in the same directory.

Page Eject

In an ideal world all applications and printers would speak the same language, and we wouldn’t have to deal with the kinds of issues and solutions I’ve covered in this column. But that would be too easy. Luckily, this has been a problem long enough that there are already excellent open source solutions available. The good news is that like everything else with Linux, the printing system just keeps getting better with age.



Jason Perlow is a freelance writer and systems integrator. He can be reached at perlow@hotmail.com.

Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62