Print Redirection

 I've got a java application (Openbravo POS) that allows the user to configure a printer.  The printer can either be a POS line printer (like the Epson TM-T99iv), a javapos printer or a cups printer.

When the application prints via the inbuilt printer driver directly to the serial/usb port - I get the correct output.  When the application prints via cups, I get a 2.5 cm blank void down the left hand side of the page.  OB Pos has an option to fix this that seems to work under windows which is to adjust the x,y start position but I have not been able to make this work under Linux.

Having tested the cups printer driver, I am sure that the problem is not in cups but rather in how the application formats the print job when sending to cups and I suspect it may be to do with the java graphics2D co-ord system and how it maps to different dpi's.  However, I wanted to create a work around to allow me to print from the application to a file and then send the file using cups to the printer.

Here are the steps to implement a solution:

  1. Create a raw printer in cups and configure the serial port as required
  2. Configure the application to print to the inbuilt printer 'driver' and send the output to a file
  3. Create a script to monitor the file created in 2 and if it changes (mod date) then send this via lpr to the raw printer

Here is the script:


# A lpr wrapper to monitor a file and to send it to cups if the odifed date changes
# parameter 1 is the name of the file to watch
# parameter 2 is the name of the printer to print to (if blank print to the default
# check printer names with "lpstat -a"

if [ $# -lt 1 ]; then
        echo "Usage: $0 file_to_monitor [printer_name]"
        exit 1

OLDMODDATE=$(stat -c %y $1)
while true
   MODDATE=$(stat -c %y $1)
   if [ "$MODDATE" != "$OLDMODDATE" ]
      lpr -P $2 $1
      echo "file printed $MODDATE"
It's basic and could do with some more work - but it seems to work.  I run this with nohup.