Force printer to print in black & white

Anonymous anonymous at easysw.com
Mon Oct 24 01:15:03 PDT 2005


I have found a solution to my problem...don't know if it's the right approach...but for me it works.

I have created a filter in /usr/lib/cups/filter/forcedefaults.

The idea is that I parse the ppd of the printer and search for the default values (like ColorMode in my case). I then just perform a search and replace in the job data and I replace the color settings to Grayscale...then I just pass the print job data to pstops

And I have changed the following line in /etc/cups/mime.convs:

application/postscript  application/vnd.cups-postscript 66      pstops

into:

application/postscript  application/vnd.cups-postscript 66      forcedefaults

Below you can find the code for the forcedefaults filter:

#!/usr/bin/perl
use strict;
use File::Copy;
use POSIX;

# Variable Definitions
my $debug = 1;
my $pstops = "/usr/lib/cups/filter/pstops";
my $spooldir = "/var/spool/cups/forcedefaults/";

my(%options, $cupsjobid, $cupsuser, $cupstitle, $cupscopies,
   $cupsopts, $cupsfilename, $spoolfile);

getparams();

if (not defined($cupsfilename)) {
   print STDERR "Copying STDIN to spoolfile...\n" if $debug;
   $cupsfilename = $spooldir."job".$cupsjobid;
   open (JOB, ">$cupsfilename") || print STDERR "ERROR: Unable to write $cupsfilename\n";
   copy (\*STDIN, \*JOB);
   close (JOB);
}

if ($options{'ColorModel'}) {
   print STDERR "Forcing defaults...\n" if $debug;
   open (SPOOL, ">$spoolfile") || print STDERR "ERROR: Unable to write $spoolfile\n";
   open (JOB, "<$cupsfilename") || print STDERR "ERROR: Unable to write $cupsfilename\n";
   while (<JOB>) {
      if ($options{'ColorModel'} eq "Gray") {
         s/BeginFeature: \*ColorModel CMYK/BeginFeature: \*ColorModel Gray/;
         s/\(cmyk\) RCsetdevicecolor/\(gray\) RCsetdevicecolor/;
      }
      print SPOOL $_;
   }
   close (JOB);
   close (SPOOL);
   $cupsfilename = $spoolfile;
} else {
   print STDERR "No need to Force defaults...\n" if $debug;
}

print STDERR "Passing result to pstops filter...\n" if $debug;
# Now pass the stuff to pstops
system ("$pstops '$cupsjobid' '$cupsuser' '$cupstitle' '$cupscopies' '$cupsopts' '$cupsfilename'") || print STDERR "ERROR: Unable to execute $pstops\n";


#### SUBROUTINES ####

sub getparams {

   if (@ARGV<5 || @ARGV>6) {
      print STDERR "ERROR: forcedefaults job-id user title copies options [file]\n";
      exit(1);
   }

   ($cupsjobid, $cupsuser, $cupstitle, $cupscopies,
    $cupsopts, $cupsfilename) = @ARGV;

   print STDERR "ForceDefaults filter started on printer $ENV{PRINTER} for user $ENV{USER}\n";

   open (PPD, "<$ENV{PPD}") || print STDERR "Unable to read PPDFile : $ENV{PPD}\n";
   # set the options values
   foreach (<PPD>) {
      if (/^\**Default(.*): (.*)/) { $options{$1} = $2; }
   }
   close (PPD);

   if ($debug) {  # for those interested
      my $s = "";
      foreach my $v (sort keys %options) {
         $s .= "$v='$options{$v}',";
      }
      print STDERR "PPD values $ENV{PPD} : '$s'\n";
   }
   $spoolfile = $spooldir."job".$cupsjobid;
}





More information about the cups mailing list