4.26. Filters and lpr or Client Side Processing

We have shown how the lpd print spooler can use a filter program to do format conversion. Most of the time users want the print spooler to control the conversion process as it will do the job conversion in the background and allow the user to proceed with other tasks. However, it is possible to request that the lpr application process input jobs with a print filter and send the processed output to the lpd print spooler.

The lpr -X option is used to specify a filter to be used to process files. This filter is invoked once for each input file, with the same options and environment variables as would be used by the lpd print spooler. We will use a simple example to show how this capability could be used in practice. Create the /tmp/pass file with the following contents, and give it executable permissions as shown below:

    #!/bin/sh
    # /tmp/pass file
    echo LEADER
    cat
    echo TRAILER
    exit 0
    
    h4: {238} % chmod a+x /tmp/pass


Edit the printcap file so it has contents indicated below, use checkpc -f to check the printcap, and then use lpc reread to restart the lpd server.

    lp:force_localhost
    lp:server
      :sd=/var/spool/lpd/%P
      :lp=/tmp/lp
Execute the following commands to print the /tmp/hi file and observe the results:
    h4: {239} % lpc stop lp
    Printer: lp@h4
    lp@h4.private: stopped
    Printer: lp2@h4
    lp2@h4.private: stopped
    h4: {240} % lpr -X /tmp/pass /tmp/hi
    h4: {241} % lpq -a
    Printer: lp@h4  (printing disabled)
     Queue: 1 printable job
     Server: no server active
     Rank   Owner/ID                  Class Job Files             Size Time
    1      papowell@h4+659              A   659 /tmp/hi         27 08:04:03
    h4: {242} % cat /var/spool/lpd/lp/*
    LEADER
    hi
    TRAILER


As we see from the example, our filter has processed the input file and added the LEADER and TRAILER strings. In practice, the actual processing of the input job would be far more elaborate, and may do such things as incorporate files or other material available only on the local system.