Using System V interface scripts with CUPS printing
CUPS is a powerful and flexible printing system, but sometimes all we need is the simplest things. For example, very often for application printing, all I want is a raw queue. I just want cups to pass my data untouched to some device. That's easy enough with cups:
lpadmin -p myprinter -E -v parallel:/dev/lp
is all that is needed. The "-v" could also go to a network port or a serial port.
But what if I wanted to add an automatic form feed to the end of that, or do some other special processing? I'd have to get into cups ppd files, add a filter - a lot of work for something so simple. You might want to do that if your printer will handle both simple text that you need to mess with, but let's take the simple, text only case first.
It doesn't have to be. Cups can use System V interfaces scripts. We'll create a simple script that just adds a form feed:
#!/bin/bash shift;shift;shift;shift;shift cat $* echo -e "\f\c"
Let's say we called that script "/tmp/myff". We add it to the raw printer like this:
lpadmin -p myprinter -E -i /tmp/myff -v parallel:/dev/lp
YOU CAN ONLY USE SYS V INTERFACES SCRIPTS WITH RAW PRINTERS. No PPD files will be used, no other filters will be processed. This script will be the ONLY thing your data passes through.
The /tmp/myff will be copied to /etc/cups/interfaces. If you now send a job:
date | lp -d myprinter
a form feed will be added.
By the way, that #!/bin/bash isn't optional here. Cups uses execve to run the script, so it needs a binary file or an interpreter line like that.
I threw away everything else here, but the variables passed to the script are the job number, the user, the source of the data (stdin), the number of copies, and finally the actual data file. So for the invocation above, the five variables I shifted away might have been
17 root (stdin) 1 /var/spool/cups/d00017-001
Obviously, there's much more that you can do at this level - emailing, data transformation, whatever you need. However, for complicated tasks, the cups filtering system does have advantages, so this method should be reserved for the simple cases.
You CAN do both - that is, let Cups handle the stuff it wants to handle and have your script handle straight text. It's a bit more work, but the scripts themselves do not change,
To accomplish this, I created "/etc/cups/myfilters". Cups is security conscious (as it should be!) so make sure that directory has the same owner and group and perms as /etc/cups/ppd. Put your script in there, and again make ownership and perms match (-rwxr-xr-x 1 root _lp on my OS X machine).
You then need to modify the ppd file for the printer. In my case, that was "/etc/cups/ppd/hplaser" and I added a new "Filter" line:
*cupsFilter: "application/vnd.cups-raster 50 rastertohp" *cupsFilter: "text/plain 0 /etc/cups/myfilters/TextToPrinter"
The first line was already there; I added the second.
Without that new line, text such as "date | lp -dhplaser" would have been handled by Cups. With this, text jobs go through my filter. This doesn't affect things like printing a web page from a browser or printing from TextEdit, but it will grab things like that command "date | lp -dhplaser".
You'll need to do a "killall -HUP cupsd" (OS X, Linux) or otherwise restart Cups when you initiate these changes; if you need to modify the filter later you can dispense with that and just go ahead and edit it.
In documentation for shell script filters you will see code like this:
[ -n "$6" ] && exec <"$6" cat -
That's because filters can be chained together - the first filter gets passed the file to print in $6 and all others (except the last) are expected to read from stdin and write to stdout. The last filter is the "back-end" which writes to the actual printer hardware (or network port).
See Using Your Own Filters to Print with CUPS for a chaining example.
(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
Inexpensive and informative Apple related e-books:
Photos for Mac: A Take Control Crash Course
Take Control of Upgrading to Mavericks
Take Control of Podcasting on the Mac, Third Edition
Take Control of PDFpen 6
Yosemite Crash Course