SUMMARY : print filter

From: Zeev Fisher <>
Date: Mon Sep 30 2002 - 07:35:13 EDT
First , thanks a lot to the following for their wonderful responses :

Larye Parkins
Matt Harris
Lee Elizabeth

What i did at last is adding the following to 
/etc/lp/interface/interface name :

echo $3|egrep -i "gif|pdf|jpeg|bmp"
if [ $? -eq 0 ]; then

The responses :

Check out apsfilter.  Really good lp filtering software.  It can do what
you're looking for. 
You can check for file extension in the printer interface
file (/etc/lp/interfaces/xxx), but that won't catch the cases where the user

cat foo.jpg | lp

To catch these, you can set up a filter (to replace LPCAT=cat in the 
interface) to trap everything that is not PCL or Postscript.  Postscript 
files always start with a percent (%), and PCL files start with ESCAPE 

JPEGs start with 0xFFD8FFE0, GIFs start with "GIF", but you really only 
need to check for '%' or 0x1b, and reject everything else.  If it starts 
with a "legal" file type, just pass the file through, otherwise, send a 
one-page reject note to the printer (appropriate for the printer type) 
and exit.

The interface sends each file listed on the command line to the filter 
one at a time: if it is printing stdin, it also gets filtered.

There is an interface file for each local printer, located in 

Near the top of the file, there is a variable assignment, "LPCAT=cat"

This is used down in the middle of the interface file where the "lp" 
command line is parsed.  If the command line contains a list of file 
names, each file is opened and the contents sent to the printer, 
filtered through the standard filter program (or, if there isn't one, 
the LPCAT command).  If there are no files listed on the command line, 
stdin is redirected through the filter to the printer.

You can write a script to use in place of 'cat' for LPCAT.  This should 
take care of your problem.  A Perl script would probably be the easiest:

while (<>) {
  if ( $state == 0) { # this is the first line of the input
    if ( substr($_,0,1) eq "%" || substr($_,0,1) eq '\033' ) {
                # it's a Postscript or PCL file, pass it to the printer
        $state = 1;
    else { # print a "can't print due to wrong format" message to the 
      system("cat /usr/lib/lp/");
        $state = 2;
  next if $state == 2; # Throw the rest of the file into the dirty bit 
bin (so we don't get a broken pipe)
  print; # If we get here, it passes the test, print each line of the file

/usr/lib/lp/ will be a file containing the postscript 
code for the message:
        "Your file could not be printed because it is not in a format 
recognized by this printer."
This is a state machine because you only need to process the first line: 
if the file passes the test, you just print the whole file without 
further checking. if it doesn't, you print a message instead and exit.

Zeev Fisher - Unix System Administrator
Galileo Technology Ltd - A Marvell Company
Moshav Manof, D.N. Misgav 20184, ISRAEL
Email    -
Tel      -  + 972 4 8225046 ext. 2402
Cell     -  + 972 54 995402
Fax      -  + 972 4 8326420
WWW Page:

This message may contain confidential, proprietary or legally privileged
information. The information is intended only for the use of the individual
or entity named above. If the reader of this message is not the
intended recipient, you are hereby notified that any dissemination, distribution
or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify us
immediately by telephone, or by e-mail and delete the message from your
sunmanagers mailing list
Received on Mon Sep 30 06:45:04 2002

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:42:55 EST