9.5. LPD (行列式印表機 Daemon)
前一頁 | 章 9. 列印 | 下一頁

## 9.5. LPD (行列式印表機 Daemon)

在背景列印一個檔案稱作 Spooling，緩衝程式 (Spooler) 讓使用者能夠繼續執行電腦的其他程式而不需要等候印表機緩慢的完成列印工作。

FreeBSD 內含的緩衝程式 (Spooler) 稱作 lpd(8)，而列印工作會使用 lpr(1) 來提交。

### 9.5.1. 初始設定

建立要用來儲存列印工作的目錄、設定擁有關係以及權限來避免其他使用者可以檢視這些檔案的內容：

    # mkdir -p /var/spool/lpd/lp
    # chown daemon:daemon /var/spool/lpd/lp
    # chmod 770 /var/spool/lpd/lp

印表機會定義在 `/etc/printcap`，每台印表機項目所包含的詳細資料有名稱、連接的接頭以及各種其他設定。建立 `/etc/printcap` 使用以下內容：

    lp:\
    	:lp=/dev/unlpt0:\
    	:sh:\
    	:mx#0:\
    	:sd=/var/spool/lpd/lp:\
    	:lf=/var/log/lpd-errs:

| 印表機的名稱。 lpr(1) 會傳送列印工作到 `lp` 印表機，除非有使用 `-P` 來指定其他印表機，所以預的印表機名稱應使用 `lp`。

印表機所連接到裝置。替換此行為正確的連線類型，如此處所示。

連線類型 | 在 `/etc/printcap` 的裝置項目
--- | ---

USB

    :lp=/dev/unlpt0:\

此為不會重設 USB 印表機的裝置，若使用上發生問題，請改使用 `ulpt0`，這個裝置會在每次使用後重設 USB 埠。

並列

    :lp=/dev/lpt0:\

網路

針對支援 LPD 通訊協定的印表機：

    :lp=:rm=network-printer-name:rp=raw:\

針對支援使用埠號 9100 列印的印表機：

    :lp=9100@network-printer-name:\

針對兩者皆支援的印表機，請替換 `network-printer-name` 為網路印表機的 DNS 主機名稱。

序列

    :lp=/dev/cuau0:br=9600:pa=none:\

這些是一般序列印表機連接到主機板序列埠會採用的數值，傳輸速率 (Baud rate) 是 9600 且無同位檢查 (No Parity)。

| 在列印工作開始時不列印首頁。
| 不限制列印工作的最大尺寸。
| 此印表機的緩衝 (Spooling) 目錄路徑，每台印表機會自己使用一個獨立的緩衝 (Spooling) 目錄。
| 回報此印表機的錯誤的日誌檔。

在建立 `/etc/printcap` 之後，使用 chkprintcap(8) 測試印表機是否有錯誤：

    # chkprintcap

在繼續之前修正任何回報的問題。

開啟 `/etc/rc.conf` 中的 lpd(8)：

    lpd_enable="YES"

啟動服務：

    # service lpd start

### 9.5.2. 使用 lpr(1) 列印

Documents are sent to the printer with `lpr`. A file to be printed can be named on the command line or piped into `lpr`. These two commands are equivalent, sending the contents of `doc.txt` to the default printer:

    % lpr doc.txt
    % cat doc.txt | lpr

Printers can be selected with `-P`. To print to a printer called `laser`:

    % lpr -Plaser doc.txt

### 9.5.3. 過濾器

The examples shown so far have sent the contents of a text file directly to the printer. As long as the printer understands the content of those files, output will be printed correctly.

Some printers are not capable of printing plain text, and the input file might not even be plain text.

Filters allow files to be translated or processed. The typical use is to translate one type of input, like plain text, into a form that the printer can understand, like PostScript® or PCL. Filters can also be used to provide additional features, like adding page numbers or highlighting source code to make it easier to read.

The filters discussed here are input filters or text filters. These filters convert the incoming file into different forms. Use su(1) to become `root` before creating the files.

Filters are specified in `/etc/printcap` with the `if=` identifier. To use `/usr/local/libexec/lf2crlf` as a filter, modify `/etc/printcap` like this:

    lp:\
    	:lp=/dev/unlpt0:\
    	:sh:\
    	:mx#0:\
    	:sd=/var/spool/lpd/lp:\
    	:if=/usr/local/libexec/lf2crlf:\
    	:lf=/var/log/lpd-errs:

| `if=` identifies the input filter that will be used on incoming text.

### 提示:

The backslash line continuation characters at the end of the lines in `printcap` entries reveal that an entry for a printer is really just one long line with entries delimited by colon characters. An earlier example can be rewritten as a single less-readable line:

    lp:lp=/dev/unlpt0:sh:mx#0:sd=/var/spool/lpd/lp:if=/usr/local/libexec/lf2crlf:lf=/var/log/lpd-errs:

#### 9.5.3.1. 避免在純文字印表機階梯狀列印

Typical FreeBSD text files contain only a single line feed character at the end of each line. These lines will “stairstep” on a standard printer:

    A printed file looks
                        like the steps of a staircase
                                                     scattered by the wind

A filter can convert the newline characters into carriage returns and newlines. The carriage returns make the printer return to the left after each line. Create `/usr/local/libexec/lf2crlf` with these contents:

    #!/bin/sh
    CR=$'\r'
    /usr/bin/sed -e "s/$/${CR}/g"

Set the permissions and make it executable:

    # chmod 555 /usr/local/libexec/lf2crlf

Modify `/etc/printcap` to use the new filter:

    :if=/usr/local/libexec/lf2crlf:\

Test the filter by printing the same plain text file. The carriage returns will cause each line to start at the left side of the page.

#### 9.5.3.2. 使用 print/enscript 在 PostScript® 印表機美化純文字內容

GNU Enscript converts plain text files into nicely-formatted PostScript® for printing on PostScript® printers. It adds page numbers, wraps long lines, and provides numerous other features to make printed text files easier to read. Depending on the local paper size, install either print/enscript-letter or print/enscript-a4 from the Ports Collection.

Create `/usr/local/libexec/enscript` with these contents:

    #!/bin/sh
    /usr/local/bin/enscript -o -

Set the permissions and make it executable:

    # chmod 555 /usr/local/libexec/enscript

Modify `/etc/printcap` to use the new filter:

    :if=/usr/local/libexec/enscript:\

Test the filter by printing a plain text file.

#### 9.5.3.3. 列印 PostScript® 到 PCL 印表機

Many programs produce PostScript® documents. However, inexpensive printers often only understand plain text or PCL. This filter converts PostScript® files to PCL before sending them to the printer.

Install the Ghostscript PostScript® interpreter, print/ghostscript9, from the Ports Collection.

Create `/usr/local/libexec/ps2pcl` with these contents:

    #!/bin/sh
    /usr/local/bin/gs -dSAFER -dNOPAUSE -dBATCH -q -sDEVICE=ljet4 -sOutputFile=- -

Set the permissions and make it executable:

    # chmod 555 /usr/local/libexec/ps2pcl

PostScript® input sent to this script will be rendered and converted to PCL before being sent on to the printer.

Modify `/etc/printcap` to use this new input filter:

    :if=/usr/local/libexec/ps2pcl:\

Test the filter by sending a small PostScript® program to it:

    % printf "%%\!PS \n /Helvetica findfont 18 scalefont setfont \
    72 432 moveto (PostScript printing successful.) show showpage \004" | lpr

#### 9.5.3.4. 智慧過濾器

A filter that detects the type of input and automatically converts it to the correct format for the printer can be very convenient. The first two characters of a PostScript® file are usually `%!`. A filter can detect those two characters. PostScript® files can be sent on to a PostScript® printer unchanged. Text files can be converted to PostScript® with Enscript as shown earlier. Create `/usr/local/libexec/psif` with these contents:

    #!/bin/sh
    #
    #  psif - Print PostScript or plain text on a PostScript printer
    #
    IFS="" read -r first_line
    first_two_chars=`expr "$first_line" : '\(..\)'`

    case "$first_two_chars" in
    %!)
        # %! : PostScript job, print it.
        echo "$first_line" && cat && exit 0
        exit 2
        ;;
    *)
        # otherwise, format with enscript
        ( echo "$first_line"; cat ) | /usr/local/bin/enscript -o - && exit 0
        exit 2
        ;;
    esac

Set the permissions and make it executable:

    # chmod 555 /usr/local/libexec/psif

Modify `/etc/printcap` to use this new input filter:

    :if=/usr/local/libexec/psif:\

Test the filter by printing PostScript® and plain text files.

#### 9.5.3.5. 其他智慧過濾器

Writing a filter that detects many different types of input and formats them correctly is challenging. print/apsfilter from the Ports Collection is a smart “magic” filter that detects dozens of file types and automatically converts them to the PDL understood by the printer. See http://www.apsfilter.org for more details.

### 9.5.4. 多序列

The entries in `/etc/printcap` are really definitions of queues. There can be more than one queue for a single printer. When combined with filters, multiple queues provide users more control over how their jobs are printed.

As an example, consider a networked PostScript® laser printer in an office. Most users want to print plain text, but a few advanced users want to be able to print PostScript® files directly. Two entries can be created for the same printer in `/etc/printcap`:

    textprinter:\
    	:lp=9100@officelaser:\
    	:sh:\
    	:mx#0:\
    	:sd=/var/spool/lpd/textprinter:\
    	:if=/usr/local/libexec/enscript:\
    	:lf=/var/log/lpd-errs:

    psprinter:\
    	:lp=9100@officelaser:\
    	:sh:\
    	:mx#0:\
    	:sd=/var/spool/lpd/psprinter:\
    	:lf=/var/log/lpd-errs:

Documents sent to `textprinter` will be formatted by the `/usr/local/libexec/enscript` filter shown in an earlier example. Advanced users can print PostScript® files on `psprinter`, where no filtering is done.

This multiple queue technique can be used to provide direct access to all kinds of printer features. A printer with a duplexer could use two queues, one for ordinary single-sided printing, and one with a filter that sends the command sequence to enable double-sided printing and then sends the incoming file.

### 9.5.5. 監視與控制列印

Several utilities are available to monitor print jobs and check and control printer operation.

#### 9.5.5.1. lpq(1)

lpq(1) shows the status of a user's print jobs. Print jobs from other users are not shown.

Show the current user's pending jobs on a single printer:

    % lpq -Plp
    Rank   Owner      Job  Files                                 Total Size
    1st    jsmith     0    (standard input)                      12792 bytes

Show the current user's pending jobs on all printers:

    % lpq -a
    lp:
    Rank   Owner      Job  Files                                 Total Size
    1st    jsmith     1    (standard input)                      27320 bytes

    laser:
    Rank   Owner      Job  Files                                 Total Size
    1st    jsmith     287  (standard input)                      22443 bytes

#### 9.5.5.2. lprm(1)

lprm(1) is used to remove print jobs. Normal users are only allowed to remove their own jobs. `root` can remove any or all jobs.

Remove all pending jobs from a printer:

    # lprm -Plp -
    dfA002smithy dequeued
    cfA002smithy dequeued
    dfA003smithy dequeued
    cfA003smithy dequeued
    dfA004smithy dequeued
    cfA004smithy dequeued

Remove a single job from a printer. lpq(1) is used to find the job number.

    % lpq
    Rank   Owner      Job  Files                                 Total Size
    1st    jsmith     5    (standard input)                      12188 bytes
    % lprm -Plp 5
    dfA005smithy dequeued
    cfA005smithy dequeued

#### 9.5.5.3. lpc(8)

lpc(8) is used to check and modify printer status. `lpc` is followed by a command and an optional printer name. `all` can be used instead of a specific printer name, and the command will be applied to all printers. Normal users can view status with lpc(8). Only `class="username">root` can use commands which modify printer status.

Show the status of all printers:

    % lpc status all
    lp:
    	queuing is enabled
    	printing is enabled
    	1 entry in spool area
    	printer idle
    laser:
    	queuing is enabled
    	printing is enabled
    	1 entry in spool area
    	waiting for laser to come up

Prevent a printer from accepting new jobs, then begin accepting new jobs again:

    # lpc disable lp
    lp:
    	queuing disabled
    # lpc enable lp
    lp:
    	queuing enabled

Stop printing, but continue to accept new jobs. Then begin printing again:

    # lpc stop lp
    lp:
    	printing disabled
    # lpc start lp
    lp:
    	printing enabled
    	daemon started

Restart a printer after some error condition:

    # lpc restart lp
    lp:
    	no daemon to abort
    	printing enabled
    	daemon restarted

Turn the print queue off and disable printing, with a message to explain the problem to users:

    # lpc down lp Repair parts will arrive on Monday
    lp:
    	printer and queuing disabled
    	status message is now: Repair parts will arrive on Monday

Re-enable a printer that is down:

    # lpc up lp
    lp:
    	printing enabled
    	daemon started

See lpc(8) for more commands and options.

### 9.5.6. 分享印表機

Printers are often shared by multiple users in businesses and schools. Additional features are provided to make sharing printers more convenient.

#### 9.5.6.1. 別名

The printer name is set in the first line of the entry in `/etc/printcap`. Additional names, or aliases, can be added after that name. Aliases are separated from the name and each other by vertical bars:

    lp|repairsprinter|salesprinter:\

Aliases can be used in place of the printer name. For example, users in the Sales department print to their printer with

    % lpr -Psalesprinter sales-report.txt

Users in the Repairs department print to their printer with

    % lpr -Prepairsprinter repairs-report.txt

All of the documents print on that single printer. When the Sales department grows enough to need their own printer, the alias can be removed from the shared printer entry and used as the name of a new printer. Users in both departments continue to use the same commands, but the Sales documents are sent to the new printer.

#### 9.5.6.2. 頁首

It can be difficult for users to locate their documents in the stack of pages produced by a busy shared printer. Header pages were created to solve this problem. A header page with the user name and document name is printed before each print job. These pages are also sometimes called banner or separator pages.

Enabling header pages differs depending on whether the printer is connected directly to the computer with a USB, parallel, or serial cable, or is connected remotely over a network.

Header pages on directly-connected printers are enabled by removing the `:sh:\` (Suppress Header) line from the entry in `/etc/printcap`. These header pages only use line feed characters for new lines. Some printers will need the `/usr/share/examples/printing/hpif` filter to prevent stairstepped text. The filter configures PCL printers to print both carriage returns and line feeds when a line feed is received.

Header pages for network printers must be configured on the printer itself. Header page entries in `/etc/printcap` are ignored. Settings are usually available from the printer front panel or a configuration web page accessible with a web browser.

### 9.5.7. 參考文獻

Example files: `/usr/share/examples/printing/`.

The 4.3BSD Line Printer Spooler Manual, `/usr/share/doc/smm/07.lpd/paper.ascii.gz`.

Manual pages: printcap(5), lpd(8), lpr(1), lpc(8), lprm(1), lpq(1).

前一頁 | 上一層 | 下一頁
9.4. 直接列印 | 起始頁 | 9.6. 其他列印系統

本文及其他文件，可由此下載： ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/。

若有 FreeBSD 方面疑問，請先閱讀 FreeBSD 相關文件，如不能解決的話，再洽詢 <questions@FreeBSD.org>。
關於本文件的問題，請洽詢 <doc@FreeBSD.org>。