4.13. Printing from lpr Directly To A Device

While the most reliable way to print is to send jobs to a print spooler, sometimes it is desirable to print directly to a printer. This method is supported by the special :direct printcap flag or the lpr -Y command line flag. The following shows the effects of this flag:

    lpr -Y -Phost%port file1 file2 ...
    Eqivalent to:
      ( for i in file1 file2 ... ; do
        $filter $i
      done ) | tcpip_connection( host%port)
    
    lpr -Y -P/dev/lp file1 file2 ...
    Eqivalent to:
      ( for i in file1 file2 ... ; do
        $filter $i
      done ) >>/dev/lp
    
    lpr -Y -P '|/program' file1 file2 ...
    Eqivalent to:
      ( for i in file1 file2 ... ; do
        $filter $i
      done ) | /program

The above examples show how we can use the command line options to send files directly to a printer. You can also create a printcap that will do the same:

    lp:direct:lp=/tmp/a:remote_support=R
    Command:
      lpr -Plp file1 file2 ...
    Equivalent to:
    lpr -P/tmp/a -Y file1 file2 ...
    
    Example:
    h4: {228} % lp -P/tmp/a /tmp/hi
    h4: {229} % cat /tmp/a /tmp/hi
    hi
    h4: {230} % lp -Plp /tmp/hi
    h4: {231} % cat /tmp/a /tmp/hi
    hi
    hi

The lpr -X filter option allows us to specify a user filter on the command line. 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


Execute the following commands to print the /tmp/hi file and observe the results:

    h4: {232} % cp /dev/null /tmp/a
    h4: {233} % lpr -P/tmp/a -X /tmp/pass /tmp/hi
    h4: {234} % cat /tmp/a
    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. We can also use a printcap entry:

    lp:direct:lp=/tmp/a:filter=/tmp/pass
    
    h4: {235} % cp /dev/null /tmp/a
    h4: {236} % lpr -Plp /tmp/hi
    h4: {237} % cat /tmp/a
    LEADER
    hi
    TRAILER