Опубликован: 06.08.2012 | Доступ: свободный | Студентов: 1189 / 25 | Оценка: 5.00 / 5.00 | Длительность: 53:41:00
Лекция 15:


< Лекция 14 || Лекция 15: 12 || Лекция 16 >

Using the spooler

Using the spooler is relatively simple. Instead of outputting data directly to the printer, you pipe it to the spooler lpr command. For example, here is the same print command, first printing directly to the printer, and secondly via the spooler:

# ps waux > /dev/lpt0
$ ps waux | lpr

Note the difference in prompt: you have to be root to write directly to the printer, but normally anybody can write to the spooler. The spooler creates a job from this data. You can look at the current print queue with the lpq program:

$ lpq
waiting for lp to become ready (offline ?)
Rank  Owner   Job  Files             Total Size
1st   grog    313  (standard input)  9151  bytes
2nd   grog    30   (standard input)  3319m bytes
3rd   Yvonne  31   (standard input)  3395  bytes
4th   root    0    (standard input)  2611  bytes

The first line is a warning that lpd can't currently print. Take it seriously. In this example, the printer was deliberately turned off so that the queue did not change from one example to the next.

Normally, the job numbers increase sequentially: this particular example came from three different machines. You can get more detail with the -l option:

$ lpq -l
waiting for lp to become ready (offline ?)
grog: 1st           [job 313freebie.example.org]
  (standard input)  9151 bytes
grog: 2nd           [job 030presto.example.org]
  (standard input)  3319 bytes
yvonne: 3rd         [job 031presto.example.org]
  (standard input)  3395 bytes
root: 4th           [job 000bumble.example.org]
  (standard input)  2611 bytes

Removing print jobs

Sometimes you may want to delete spool output without printing it. You don't need to do this because of a printer configuration error: just turn the printer off, fix the configuration error, and turn the printer on again. The job should then be printed correctly. But if you discover that the print job itself contains garbage, you can remove it with the lprm program. First, though, you need to know the job number. Assuming the list we have above, we might want to remove job 30:

  lprm 30
dfA030presto.example.org dequeued
cfA030presto.example.org dequeued
waiting for lp to become ready (offline ?)
Rank  Owner   Job  Files             Total Size
1st   grog    313  (standard input)  9151 bytes
2nd   yvonne  31   (standard input)  3395 bytes
3rd   root    0    (standard input)  2611 bytes

If the printer is offline, it may take some time for the lprm to complete.


We've encountered the term PostScript several times already. It's a Page Description Language. With it, you can transmit detailed documents such as this book electronically and print them out in exactly the same form elsewhere1This is in fact the way this book was sent to the printers. PostScript is a very popular format on the World Wide Web, and web browsers like Netscape usually print in PostScript format.

Most other document formats describe special print features with escape sequences, special commands that start with a special character. For example, the HP LaserJet and PCL formats use the ASCII ESC character (Oxlb) to indicate the beginning of an escape sequence. PostScript uses the opposite approach: unless defined otherwise, the contents of a PostScript file are commands, and the printable data is enclosed in parentheses. PostScript documents start with something like:

%%Creator: groff version 1.10
%%CreationDate: Fri Oct 31 18:36:45 1997
%%DocumentNeededResources: font Symbol
%%+ font Courier
%%+ font Times-Roman
%%DocumentSuppliedResources: file images/vipw.ps
%%Pages: 32
%%PageOrder: Ascend
%%Orientation: Portrait

This is the prologue (the beginning) of the PostScript output for this chapter. The prologue of such a program can be several hundred kilobytes long if it includes embedded fonts or images. Amore typical size is about 500 lines.

You can do a number of things with PostScript:

  • You can look at it with gv, which is in the Ports Collection. We'll look at this option below.
  • Many printers understand PostScript and print it directly. If yours does, you probably know about it, since it's an expensive option. In case of doubt, check your printer manual.
  • If your printer doesn't understand PostScript, you can print with the aid of ghostscript. The apsfilter port does this for you.

Viewing with gv

gv is part of the instant workstation port that we discussed on page 93. To view a file with gv, simply start it:

$ gv filename &

If you don't specify a file name, you get a blank display. You can then open a file window by pressing o, after which you can select files and display them. Figure 15-1 shows the display of a draft version of this page with an overlaid open window at the top right. The Open File window contains a field at the top into which you can type the name of a file. Alternatively, the columns below, with scroll bars, allow you to browse the current directory and the parent directories.

The window below shows the text of the previous page (roughly) on the right hand side. Instead of scroll bars, there is a scroll area below the text Save Marked. You can scroll the image in all directions by selecting the box with the left mouse button and moving around. At top left are menu buttons that you can select with the left mouse button. Note also the button 1.414 at the top of the window: this is the magnification of the image. You can change it by selecting this button: a menu appears and gives you a range of magnifications to choose from.

The column to the right of these buttons is a list of page numbers. You can select a page number with the middle mouse button. You can also get an enlargement display of the text area around the mouse cursor by pressing the left button.

gv display

Рис. 15.1. gv display

Printing with ghostscript

If your printer doesn't support PostScript, you can still print some semblance of the intended text with the help of ghostscript. The results are very acceptable with modern laser and inkjet printers, less so with older dot matrix printers.

To print on your particular printer, you first need to find a driver for it in ghostscript. In this context, the term driver means some code inside ghostscript that converts the data into something that the printer can print.

We've already seen how to use /etc/printcap. In this case, we'll need an input filter, a script or program that transforms the PostScript data into a form that the printer understands. The entry in /etc/printcap is pretty much the same for all printers:

ps|HP Office Jet 725 with PostScript:\

This entry defines a printer called ps. The comment states that it's an HP Office Jet, but that's only a comment. Obviously you should choose a comment that matches the printer you really have.

The printer is connected to /dev/lpt0,the first parallel printer. Spool data is collected in the directory /var/spool/output/colour. You must create this directory, or printing will fail, and depending on what you use to print, you may not even see any error messages. They also don't appear on the log file, which in this case is /var/log/lpd-errs.

The important entry is in the last line, which refers to the input filter /usr/local/libexec/ps-filter. This file contains the instructions to convert the PostScript into something that the printer can understand. For example, for the HP Office Jet we're talking about here, it contains:

/usr/local/bin/gs -sDEVICE=pcl3 -q -sPaperSize=a4 -dNOPAUSE -sOutputFile=- -

These options state:

  • Use ghostscript device pcl3. This is the driver to choose for most Hewlett Packard inkjet printers. We'll see alternatives for other printers below.
  • The output file is stdin (see page 127). By convention, a number of programs use the character - to represent the stdout stream.
  • -q means guiet. Normally ghostscript outputs a message on startup, and it often outputs other informative messages as well. In this case, we're using it as a filter, so we don't want any output except what we print.
  • Don't pause between pages. If you don't specify this parameter, ghostscript waits for a key press at the end of each page.
  • The paper size is the international A4 format. By default, ghostscript produces output for American standard 8.5 x 10 inch "letter" paper.
  • The character - by itself tells ghostscript that the input is from stdin. Together with the output to stdout, this makes ghostscript function as a filter.

Which driver?

The previous example used the driver for the HP DeskJet. Well, to be more precise, it used one of a plethora of drivers available. You can find more information in the HTML driver documentation at /usr/local/share/ghostscript/7.05/doc/Devices.htm. The 7.05 in the name refers to the release of ghostscript, which will change.

The documentation isn't the easiest to read. It's probably older than your printer, so there's a good chance that it won't mention your specific printer model. You may need to experiment a little before you get things working the way you want.

Printer drivers for DeskJets

There are at least six sets of drivers for HP DeskJets. They're all described in Devices.htm, but the following summary may help:

  • Hewlett Packard supply their own drivers. In addition to ghostscript, they require server software that you can install from the Ports collection (/usr/ports/print/hpijs).
  • Next come three different independently written drivers for specific models of DeskJet, probably all now obsolete. If you recognize your printer or something similar in one of them, that's a good first choice.
  • Next comes the generic pcl3 driver that was used in the example above. It's not mentioned in the documentation.
  • Finally, uniprint is a completely different driver framework for a number of different makes of printer. It requires a slightly different command line, and we'll look at it separately below.

If you're using a DeskJet, you have the choice. Unfortunately, there's no way to know which is best until you've tried them all. Similar considerations apply to other makes of printer.

uniprint drivers

The uniprint drivers have a somewhat different kind of interface. They're described towards the end of the same Devices.htm file. To use them, change the driver specification as in the following example, that refers to an Epson

/usr/local/bin/gs @stc500ph.upp -q -sPaperSize=a4 -dNOPAUSE -sOutputFile=- - -c quit

The differences here are:

  • The name of the driver (stc500ph.upp) is specified differently.
  • The line ends with a command to the driver it self (-c quit). The exact meaning is not documented, though it's easy to guess.
Which drivers?

Another problem you might encounter is that it's possible to specify the drivers you want in your ghostscript executable when you build the port. It's quite possible that the drivers described in Devices.htm don't exist on your system. To find out, run ghostscript interactively with the -h (help) option:

$ gs -h
GNU Ghostscript 7.05 (2002-04-22)
Copyright (C) 2002 artofcode LLC, Benicia, CA.   All rights reserved.
Usage: gs  [switches]   [file1.ps file2.ps ...]
Most frequently used switches:  (you can use # in place of =)
  -dNOPAUSE   no pause after page           -q       'quiet', fewer messages
  -g<width>x<height>   page size in pixels  -r<res>  pixels/inch resolution
  -sDEVICE=<devname>   select device        -dBATCH  exit after last file
  -sOutputFile=<file> select output file: - for stdout,   |command for pipe,
                                         embed %d or %ld for page #
Input formats: PostScript PostScriptLevell PostScriptLevel2 PDF Available devices:
  x11 x11alpha x11cmyk x11gray2 x11gray4 x11mono x11rg16x x11rg32x md2k
  md5k md50Mono md50Eco md1xMono bj10e bj10v bj10vh bj200 bjc600 bjc800
  lips2p lips3 lips4 bjc880j lips4v uniprint dmprt epag escpage lp2000
  alc8600 alc8500 alc2000 alc4000 lp8800c lp8300c lp8500c lp3000c lp8200c
  lp8000c epl5900 epl5800 epl2050 epl2050p epl2120 lp7500 lp2400 lp2200
  lp9400 lp8900 lp8700 lp8100 lp7700 lp8600f lp8400f lp8300f lp1900
  lp9300 lp9600 lp8600 lp1800 mjc180 mjc360 mjc720 mj500c deskjet djet500
cdeskjet cdjcolor cdjmono cdj550 cdj670 cdj850 cdj880 cdj890 cdj1600
  cdj970 laserjet ljetplus ljet2p ljet3 ljet3d ljet4 ljet4d cljet5
  cljet5c cljet5pr lj5mono lj5gray pj pjxl pjxl300 pxlmono pxlcolor pcl3 hpdj ijs
  npdl rpdl gdi bmpmono bmpgray bmp16 bmp256 bmp16m bmp32b bmpsep1 bmpsep8
  faxg3 faxg32d faxg4 jpeg jpeggray pcxmono pcxgray pcx16 pcx256 pcx24b
  pcxcmyk pdfwrite bit bitrgb bitcmyk pbm pbmraw pgm pgmraw pgnm pgnmraw pnm 
pnmraw ppm ppmraw pkm pkmraw pksm pksmraw pngmono pnggray png16
  png256 png16m psmono psgray psrgb pswrite epswrite tiffcrle tiffg3
  tiffg32d tiffg4 tiff12nc tiff24nc tifflzw tiffpack nullpage
Search path:
  .  :  /opt/lib/ghostscript  :  /opt/lib/ghostscript/fonts  :
  /opt/lib/ghostscript/garamond  :  /usr/local/share/ghostscript/7.05/lib  :
For more information, see /usr/local/share/ghostscript/7.05/doc/Use.htm.
Report bugs to bug-gs@ghostscript.com, using the form in Bug-form.htm.


PDF, or Portable Document Format, is a newer format for transferring print documents. Like PostScript, it comes from Adobe, and it is becoming increasingly important as a document interchange format on the Internet.

There are two ways to handle PDF:

  • Use Acrobat Reader, available in the Ports Collection as /usr/src/print/acroread5. The 5 refers to the version of Acrobat Reader and may change. Acrobat Reader is proprietary, but it's available for free, unfortunately only in binary form. It is quite a convenient way to view PDF documents, and it can print them in PostScript formats. This means that you can also use it to convert PDF to PostScript.
  • ghostscript also understands PDF, and it is capable of converting between PostScript and PDF in both directions. ghostscript provides two scripts, pdf2ps and ps2pdf, which act as a front end to ghostscript to make the job easier.

Un like PostScript, an editor is available for PDF (Acrobat, the big brother of Acrobat Reader). Unfortunately, it's proprietary and not free, and worse still, it's not available for FreeBSD.

< Лекция 14 || Лекция 15: 12 || Лекция 16 >
Александр Устинов
Александр Устинов
Украина, Киев