Annotation of wikisrc/guide/print.mdwn, revision 1.4

1.2       jdf         1: **Contents**
                      3: [[!toc levels=3]]
1.1       jdf         5: # Printing
                      7: This chapter describes a simple configuration for printing, using an HP Deskjet
                      8: 690C printer connected to the first parallel port and the lpd printing system
                      9: that comes with NetBSD. First, the system will be configured to print text
                     10: documents, and next the configuration will be extended to print PostScript
                     11: documents using the Ghostscript program
1.4     ! nia        12: ([`print/ghostscript`](
1.1       jdf        13: Please note that there are other, alternative printing systems available from
                     14: the [packages collection](,
                     15: like LPRng
1.4     ! nia        16: ([`print/LPRng`](
1.1       jdf        17: and the Common Unix Printing System (CUPS)
1.4     ! nia        18: ([`print/cups`](
1.1       jdf        19: which are not covered here.
                     21: ## Enabling the printer daemon
                     23: After installation it is not yet possible to print, because the
1.3       plunky     24: [[!template id=man name="lpd" section="8"]] printer
1.1       jdf        25: spooler daemon is not enabled. To enable `lpd`, one line in the `/etc/rc.conf`
                     26: file must be changed from:
                     28:     lpd=NO
                     30: to
                     32:     lpd=YES
                     34: Or rather, insert it, it won't be in your `rc.conf` by default.
                     36: The change will come into effect at the next boot, but the daemon can be started
                     37: manually now:
                     39:     # sh /etc/rc.d/lpd start
                     41: To check if lpd is active, type the following command:
                     43:     # ps ax | grep lpd
                     44:       179 ??  Is     0:00.01 lpd
                     46: If you don't see an entry for lpd in the output of the previous command, the
                     47: daemon is not active.
                     49: The lpd system is configured via `/etc/printcap`. Before configuring
                     50: `/etc/printcap` it is a good idea to make a printer test, to check if the
                     51: physical connection between your computer and the printer is working. The test
                     52: sends out some data directly to the printer device. Assuming you use a printer
                     53: connected to the parallel port, this is `/dev/lpt0`; if you use an USB printer
                     54: try `/dev/ulpt0`. Please check the manpages of these devices
1.3       plunky     55: ([[!template id=man name="lpt" section="4"]],
                     56: [[!template id=man name="ulpt" section="4"]])
1.1       jdf        57: for more information!
                     59: In our example we have a printer attached to the parallel port, so we run this:
                     61:     # lptest 70 5 > /dev/lpt0
                     63: To see what the output should look like, try the same command without
                     64: redirecting the output to the printer:
                     66:     # lptest 70 5
                     67:     !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdef
                     68:     "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefg
                     69:     #$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefgh
                     70:     $%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghi
                     71:     %&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghij
                     73: A frequent problem is that the output on the printer is not correctly aligned in
                     74: columns but has a "staircase" configuration. This usually means that the printer
                     75: is configured to begin a new line at the left margin after receiving both a
                     76: `<CR\>` (carriage return, ASCII 13) character and a `<LF\>` (line feed, ASCII
                     77: 10) character. NetBSD only sends a <LF\> character. You can fix this problem in
                     78: two ways:
                     80:  * by changing the configuration of the printer
                     81:  * by using a simple printer filter (described later)
                     83: *Note*: In the previous example the lpd spooler is not involved because the
                     84: program output is sent directly to the printer device (`/dev/lpt0`) and is not
                     85: spooled.
                     87: ## Configuring `/etc/printcap`
                     89: This section explains how to configure the example printer to print text
                     90: documents.
                     92: The printer must have an entry in the `/etc/printcap` file; the entry contains
                     93: the printer id (the name of the printer) and the printer description. The
1.3       plunky     94: [[!template id=man name="lp" section="1"]]
1.1       jdf        95: id is the default used by many programs. Here is an example entry:
                     97: **Example `/etc/printcap`**
                     99:     lp|local printer|HP DeskJet 690C:\
                    100:             :lp=/dev/lpa0:sd=/var/spool/lpd/lp:lf=/var/log/lpd-errs:\
                    101:             :sh:pl#66:pw#80:if=/usr/local/libexec/lpfilter:
                    103: The file format and options are described in detail in the
1.3       plunky    104: [[!template id=man name="printcap" section="5"]]
1.1       jdf       105: manpage. Please note that an *input filter* has been specified (with the `if`
                    106: option) which will take care of eliminating the staircase problem:
                    108:     if=/usr/local/libexec/lpfilter
                    110: ### Printer driver and HP printers
                    112: The example above uses the `lpa0` device (polled driver) for the printer,
                    113: instead of the `lpd0` (interrupt driven driver). Using interrupts there is a
                    114: communication problem with some printers, and the HP Deskjet 690C is one of
                    115: them: printing is very slow and one PostScript page can take hours. The problem
                    116: is solved using the `lpa` driver. It is also possible to compile a custom kernel
                    117: where lpt is polled.
                    119: The printcap entry for the printer also specifies a spool directory, which must
                    120: be created; this directory will be used by the lpd daemon to accumulate the data
                    121: to be printed:
                    123:     # cd /var/spool/lpd
                    124:     # mkdir lp
                    125:     # chown daemon:daemon lp
                    126:     # chmod 770 lp
                    128: The only missing part is the `lpfilter` input filter, which must be written. The
                    129: only task performed by this filter is to configure the printer for the
                    130: elimination of the staircase problem before sending the text to be printed. The
                    131: printer used in this example requires the following initialization string:
                    132: `<ESC>&k2G`.
                    134: **Example `/usr/local/libexec/lpfilter`**
                    136:     #!/bin/sh
                    137:     # Treat LF as CR+LF
                    138:     printf "\033&k2G" && cat && exit 0
                    139:     exit 2
                    141: After saving this script into the name you used in `/etc/printcap`, you need to
                    142: make sure it's executable:
                    144:     # chmod 755 /usr/local/libexec/lpfilter*
                    146: *Note*: There is another filter that can be used:
                    148:     if=/usr/libexec/lpr/lpf:
                    150: This filter is much more complex than the one presented before. It is written to
                    151: process the output of
1.3       plunky    152: [[!template id=man name="nroff" section="1"]]
1.1       jdf       153: and handles underline and overprinting, expands tab characters and converts `LF`
                    154: to `CR + LF`. The source to this filter program can be found in
                    155: `/usr/src/usr.sbin/lpr/filters/lpf.c`.
                    157: After everything is in place now, the
1.3       plunky    158: [[!template id=man name="lptest" section="1"]]
1.1       jdf       159: command can be run again now, this time using the
1.3       plunky    160: [[!template id=man name="lpr" section="1"]] command,
1.1       jdf       161: which will first send the data to the lpd spooler, then runs the filter and
                    162: sends the data off to the printer:
                    164:     # lptest 70 5 | lpr -h
                    166: The `lpr` program prints text using the spooler to send data to the printer; the
                    167: `-h` option turns off the printing of a banner page (not really necessary,
                    168: because of the `sh` option in `/etc/printcap`). Users more familiar with the
                    169: System V printing system can also use the
1.3       plunky    170: [[!template id=man name="lp" section="1"]] command
1.1       jdf       171: that comes as an alternative to
1.3       plunky    172: [[!template id=man name="lpr" section="1"]].
1.1       jdf       173: 
                    174: ## Configuring Ghostscript
                    176: Now that basic printing works, the functionality for printing PostScript files
                    177: can be added. The simple printer used in this example does not support native
                    178: printing of PostScript files; a program must be used which is capable of
                    179: converting a PostScript document in a sequence of commands that the printer
                    180: understands. The Ghostscript program, which can be found in packages collection,
                    181: can be used to this purpose. This section explains how to configure lpd to use
                    182: Ghostscript to print PostScript files on the HP Deskjet 690C.
                    184: A second id for the printer will be created in `/etc/printcap`: this new id will
                    185: use a different input filter, which will call Ghostscript to perform the actual
                    186: print of the PostScript document. Therefore, text documents will be printed on
                    187: the *lp* printer and PostScript documents on the *ps* printer: both entries use
                    188: the same physical printer but have different printing filters.
                    190: The same result can be achieved using different configurations. For example, a
                    191: single entry with only one filter could be used. For this, the filter should be
                    192: able to automatically determine the format of the document being printed, and
                    193: use the appropriate printing program. This approach is simpler but leads to a
                    194: more complex filter; if you like it you should consider installing the
                    195: magicfilter program from the packages collection: it does this and many other
                    196: things automatically.
                    198: For our approach, the new `/etc/printcap` file looks like this:
                    200: **Example `/etc/printcap`**
                    202:     lp|local printer|HP DeskJet 690C:\
                    203:             :lp=/dev/lpa0:sd=/var/spool/lpd/lp:lf=/var/log/lpd-errs:\
                    204:             :sh:pl#66:pw#80:if=/usr/local/libexec/lpfilter:
                    206:     ps|Ghostscript driver:\
                    207:             :lp=/dev/lpa0:sd=/var/spool/lpd/ps:lf=/var/log/lpd-errs:\
                    208:             :mx#0:sh:if=/usr/local/libexec/lpfilter-ps:
                    210: Option `mx#0` is very important for printing PostScript files because it
                    211: eliminates size restrictions on the input file; PostScript documents tend to be
                    212: very big. The `if` option points to the new filter. There is also a new spool
                    213: directory.
                    215: The next steps are the creation of the new spool directory and of the filter
                    216: program. The procedure for the spool directory is the same as above:
                    218:     # cd /var/spool/lpd
                    219:     # mkdir ps
                    220:     # chown daemon:daemon ps
                    221:     # chmod 770 ps
                    223: The filter program for PostScript output is more complex than the text base one:
                    224: the file to be printed is fed to the interpreter which converts it into a
                    225: sequence of commands in the printer's control language, and then sends that off
                    226: to the printer. We have achieved to transform a cheap color printer in a device
                    227: suitable for PostScript output, by virtue of the NetBSD operating system and
                    228: some powerful freeware packages. The options used to configure Ghostscript are
                    229: described in the Ghostscript documentation: `cdj550` is the device used to drive
                    230: the HP printer.
                    232: **Example `/usr/local/libexec/lpfilter-ps`**
                    234:     #!/bin/sh
                    235:     # Treat LF as CR+LF
                    236:     printf "\033&k2G" || exit 2
                    237:     # Print the postscript file
                    238:     /usr/pkg/bin/gs -dSAFER -dBATCH -dQUIET -dNOPAUSE -q -sDEVICE=cdj550 \
                    239:     -sOutputFile=- -sPAPERSIZE=a4 - && exit 0
                    240:     exit 2
                    242: To summarize: two different printer names have been created on the system, which
                    243: point to the same physical printer but use different options, different filters
                    244: and different spool directories. Text files and PostScript files can be printed.
                    245: To print PostScript files the Ghostscript package must be installed on the
                    246: system.
                    248: ## Printer management commands
                    250: This section lists some useful BSD commands for printer and print jobs
                    251: administration. Besides the already mentioned `lpr` and `lpd` commands, we
                    252: have:
1.3       plunky    254:  * [[!template id=man name="lpq" section="1"]]
1.1       jdf       255:    -- examine the printer job queue.
1.3       plunky    256:  * [[!template id=man name="lprm" section="1"]]
1.1       jdf       257:    -- delete jobs from the printer's queue.
1.3       plunky    258:  * [[!template id=man name="lpc" section="8"]]
1.1       jdf       259:    -- check the printing system, enable/disable printers and printer
                    260:    features.
                    262: ## Remote printing
                    264: It is possible to configure the printing system in order to print on a printer
                    265: connected to a remote host. Let's say that, for example, you work on the *wotan*
                    266: host and you want to print on the printer connected to the *loge* host. The
                    267: `/etc/printcap` file of loge is the one of the last example. From wotan it will
                    268: be possible to print Postscript files using Ghostscript on loge.
                    270: The first step is to accept the print jobs submitted from the wotan host to the
                    271: loge host. To accomplish this, a line with the wotan host name must be added to
                    272: the `/etc/hosts.lpd` file on loge:
                    274:     # hostname
                    275:     loge
                    276:     # cat /etc/hosts.lpd
                    277:     wotan
                    279: The format of this file is very simple: each line contains the name of a host
                    280: which is permitted to print on the local system. By default the lpd daemon only
                    281: listens on UNIX domain sockets for local connections, it won't accept any
                    282: network connects. To ensure the daemon also accepts incoming network traffic,
                    283: the following will need to be added to `/etc/rc.conf`:
                    285:     lpd_flags=""
                    287: Next, the `/etc/printcap` file on wotan must be configured in order to send
                    288: print jobs to loge. For example:
                    290:     lp|line printer on loge:\
                    291:         :lp=:sd=/var/spool/lpd/lp:lf=/var/log/lp-errs:\
                    292:         :rm=loge:rp=lp
                    294:     ps|Ghostscript driver on loge:\
                    295:         :lp=:sd=/var/spool/lpd/ps:lf=/var/log/lp-errs:\
                    296:         :mx#0:\
                    297:         :rm=loge:rp=ps
                    299: There are four main differences between this configuration and the earlier one:
                    301:  1. The definition of `lp` is empty.
                    302:  2. The `rm` (remote machine) entry defines the name of the host to which the printer is connected.
                    303:  3. The `rp` (remote printer) entry defines the name of the printer connected to the remote host.
                    304:  4. It is not necessary to specify input filters because the definitions on the loge host will be used.
                    305:  5. The spool directories must still be created locally on wotan:
                    307:         # cd /var/spool/lpd
                    308:         # mkdir lp
                    309:         # chown daemon:daemon lp
                    310:         # chmod 770 lp
                    311:         # mkdir ps
                    312:         # chown daemon:daemon ps
                    313:         # chmod 770 ps
                    315: Now the print jobs for the `lp` and `ps` queues on wotan will be sent
                    316: automatically to the printer connected to loge.

CVSweb for NetBSD wikisrc <> software: FreeBSD-CVSweb