6.6. Working with Ghostscript

Ghostscript is a program that accepts PostScript and PDF files as input then converts them into several other formats. Ghostscript includes a number of drivers to achieve this. These are sometimes also referred to as devices.

Ghostscript converts files in two steps:

  1. PostScript data is rasterized — the graphical image is broken into a fine-grained raster of pixel dots. This step is performed independently from the Ghostscript driver used later. The finer the raster (the higher the resolution), the higher the output quality. On the other hand, doubling the resolution both horizontally and vertically (for example) means that the number of pixels must quadruple. Accordingly, the computer needs four times the CPU time and amount of memory to double the resolution.

  2. The dot matrix that makes up the image is converted into the desired format (a printer language, for example) with the help of a Ghostscript driver.

Ghostscript can also process PostScript files to display them on screen or convert them into PDF documents. To display PostScript files on screen, you should probably use the program gv (rather than relying on bare Ghostscript commands), which gives a more convenient graphical interface.

Ghostscript is a very big program package and has a number of command-line options. Apart from the information available with the manual page of gs, the most important part of the documentation is the list of Ghostscript drivers, which is found in:

/usr/share/doc/packages/ghostscript/catalog.devices

and the files:

/usr/share/doc/packages/ghostscript/doc/index.html

/usr/share/doc/packages/ghostscript/doc/Use.htm

/usr/share/doc/packages/ghostscript/doc/Devices.htm

/usr/share/doc/packages/ghostscript/doc/hpdj/gs-hpdj.txt

/usr/share/doc/packages/ghostscript/doc/hpijs/hpijs_readme.html

/usr/share/doc/packages/ghostscript/doc/stp/README

When executed from the command line, Ghostscript processes any options then presents its own GS> prompt. Exit from this dialog mode by entering quit.

If you enter gs -h, Ghostscript displays its most important options and lists the available drivers. This listing, however, only includes generic driver names, even for drivers that support many different models, such as uniprint or stp. The parameter files for uniprint and the models supported by stp are explicitly named in /usr/share/doc/packages/ghostscript/catalog.devices

6.6.1. Sample Operations with Ghostscript

Find a number of PostScript examples in the directory /usr/share/doc/packages/ghostscript/examples. The color circle in /usr/share/doc/packages/ghostscript/examples/colorcir.ps is well suited for test printouts.

6.6.1.1. Displaying PostScript under X

Under X, the graphical environment, use gs to view a PostScript file on screen. To do so, enter the following command as a single line, omitting the backslash (\):

gs -r60 \ 
/usr/share/doc/packages/ghostscript/examples/colorcir.ps

In the above command, the -r option specifies the resolution, which must be appropriate for the output device (printer or screen). Test the effect of this option by specifying a different value, for example, -r30. To close the PostScript window, press Ctrl-C in the terminal window from which gs was started.

6.6.1.2. Conversion into PCL5e

The conversion of a PostScript file into the printer-specific format of a PCL5e or PCL6 printer can be achieved with a command like

gs -q -dNOPAUSE -dSAFER -sOutputFile=/tmp/out.prn \
 -sDEVICE=ljet4 -r300x300  \
 /usr/share/doc/packages/ghostscript/examples/colorcir.ps \
 quit.ps

Again, the command must be entered as a single line and without any backslash (\). With this command, it is assumed that the file /tmp/out.prn does not exist yet.

6.6.1.3. Conversion into PCL3

The conversion of a PostScript file into the printer-specific format for a PCL3 printer can be achieved with a command such as the following:

gs -q -dNOPAUSE -dSAFER -sOutputFile=/tmp/out.prn \
 -sDEVICE=deskjet -r300x300 \
 /usr/share/doc/packages/ghostscript/examples/colorcir.ps \
 quit.ps

Depending on the model, you can replace deskjet with cdjmomo, cdj500, or cdj550 or use the alternative driver hpdj:

gs -q -dNOPAUSE -dSAFER -sOutputFile=/tmp/out.prn \
 -sDEVICE=hpdj -r300x300 \
 -sModel=500 -sColorMode=mono -dCompressionMethod=0 \
 /usr/share/doc/packages/ghostscript/examples/colorcir.ps \
 quit.ps

The individual commands can also be entered without \ in a single line.

6.6.1.4. Conversion into ESC/P, ESC/P2, or ESC/P Raster

These are some sample commands to convert a PostScript file into the printer-specific format of an ESC/P2, ESC/P, or ESC/P raster printer.

gs -q -dNOPAUSE -dSAFER -sOutputFile=/tmp/out.prn \
 @stcany.upp \
 /usr/share/doc/packages/ghostscript/examples/colorcir.ps \
 quit.ps
gs -q -dNOPAUSE -dSAFER -sOutputFile=/tmp/out.prn \
 -sDEVICE=stcolor -r360x360 \
 -dBitsPerPixel=1 -sDithering=gsmono -dnoWeave \
 -sOutputCode=plain \
 /usr/share/doc/packages/ghostscript/examples/colorcir.ps \
 quit.ps

The above commands also show that the uniprint Ghostscript driver, which is called through a parameter file (stcany.upp in our example), requires a different command syntax than regular Ghostscript drivers. Because all driver options are stored in the uniprint parameter file, they do not have to be specified on the Ghostscript command line itself.

6.6.1.5. Sending the Output Directly to the Printer

With each of the above commands, the output is written in the corresponding printer language and stored in the file /tmp/out.prn. This file can be sent directly to the printer by root without the use of a print spooler or any filtering. For a printer connected to the first parallel port, this can be achieved with the command cat /tmp/out.prn >/dev/lp0.

6.6.1.6. Processing PostScript and PDF Files

Ghostscript can generate PostScript and PDF files, convert both formats to each other, and even merge PostScript and PDF files in mixed order.

Conversion from PostScript to PDF:

gs -q -dNOPAUSE -dSAFER \
 -sOutputFile=/tmp/colorcir.pdf -sDEVICE=pdfwrite \
 /usr/share/doc/packages/ghostscript/examples/colorcir.ps \
 quit.ps

Conversion of the generated PDF file /tmp/colorcir.pdf to PostScript:

gs -q -dNOPAUSE -dSAFER \
 -sOutputFile=/tmp/colorcir.ps -sDEVICE=pswrite \
 /tmp/colorcir.pdf quit.ps

Following the reconversion from PDF to PostScript, the file /tmp/colorcir.ps does not match the original file /usr/share/doc/packages/ghostscript/examples/colorcir.ps. However, there should be no visible difference in the printout.

Merging PostScript and PDF files into a PostScript file:

gs -q -dNOPAUSE -dSAFER -sOutputFile=/tmp/out.ps \
 -sDEVICE=pswrite \
 /usr/share/doc/packages/ghostscript/examples/escher.ps \
 /tmp/colorcir.pdf quit.ps

Merging PostScript and PDF files into a PDF file:

gs -q -dNOPAUSE -dSAFER -sOutputFile=/tmp/out.pdf \
 -sDEVICE=pdfwrite /tmp/out.ps \
 /usr/share/doc/packages/ghostscript/examples/golfer.ps \
 /tmp/colorcir.pdf quit.ps

Depending on the files you use, it may not be possible to merge some PostScript and PDF files.