#!/bin/bash ######################################################################## # # Program: booklet-print # # Author: Conor Daly # Version: 0.1.1 # Date: 03 Oct 2002 # # License: GPL # # Purpose: # formats a document for printing in A5 booklet form. # # uses the Xerox colour printer # # takes input from stdin or as a file # # we'll do nothing fancy here, just take the file and stuff it # through psbook and mpage and hand it to the printer # # Bugs: Completely ignores any of the usual print accounting stuff # that comes in from lpr. # ######################################################################### # Set the print queue to use. It must be capable of duplex printing or # you won't get sensible output PRINTER=fanfold PRINTER_COMMAND="lpr -P${PRINTER}" # For debug info, uncomment the "LOGFILE=" line below # Note: debug will disable actual printout... LOGFILE=/var/log/cups/booklet.log # Start the print run... [ $LOGFILE ] && echo >> $LOGFILE [ $LOGFILE ] && echo "________________________________________________" >> $LOGFILE [ $LOGFILE ] && echo -n "Starting print run at: " >> $LOGFILE [ $LOGFILE ] && date >> $LOGFILE [ $LOGFILE ] && echo $0 $* >> $LOGFILE # Get the name of this filter. If it's booklet-right or booklet-left # then print for a non-duplex printer. Gotta print in two passes though FILTERNAME=$0 MPAGE_SWITCH="" if [ `basename $FILTERNAME` == "booklet-left" ]; then MPAGE_SWITCH="-j 2%2" [ $LOGFILE ] && echo "left mode" >> $LOGFILE fi if [ `basename $FILTERNAME` == "booklet-right" ]; then # If your printer outputs reversed, you may need to remove the # '-r' from MPAGE_SWITCH below MPAGE_SWITCH="-r -j 1%2" [ $LOGFILE ] && echo "right mode" >> $LOGFILE fi # pipe or console? if [ -t 0 ]; then # We're in a console here [ $LOGFILE ] && echo "console">> $LOGFILE FILE=$1 PIPE= else # Here, we're in a pipe # We need a temporary file here so something else can carry on [ $LOGFILE ] && echo "pipe">> $LOGFILE FILE=/tmp/booklet-print$$.tmp cat > $FILE PIPE=1 fi # Find out how many pages to print NUM=`mpage -1 $FILE | grep %%Pages: | tail -1 | cut -f2 -d" "` [ $LOGFILE ] && echo "Processing ${NUM} pages from ${FILE}..." >> $LOGFILE # We need an exact multiple of 4 for psbook ODD=`expr $NUM % 4` if [ $ODD -ne 0 ]; then [ $LOGFILE ] && echo "${ODD} odd pages, adjusting..." >> $LOGFILE NUM=`expr $NUM + 4 - $NUM % 4` [ $LOGFILE ] && echo "Processing ${NUM} pages from ${FILE}..." >> $LOGFILE fi # Just see where psbook and mpage are... [ $LOGFILE ] && which psbook >> $LOGFILE [ $LOGFILE ] && which mpage >> $LOGFILE # Now, push $FILE through psbook and mpage and on to the printer [ $LOGFILE ] && psbook -s${NUM} $FILE | mpage -2 $MPAGE_SWITCH -bA4 -m35t-40r-00l-20b -M-8 -t -T > book.ps [ $LOGFILE ] || psbook -s${NUM} $FILE | mpage -2 -$MPAGE_SWITCH bA4 -m00t00r00l00b -M-8 -t -T | ${PRINTER_COMMAND} # If the file came down a pipe, delete the temporary file [ $LOGFILE ] && [ "X${PIPE}" == "X1" ] && echo "Removing $FILE" >> $LOGFILE if [ ! $LOGFILE ]; then [ $PIPE ] && [ $PIPE -eq 1 ] && rm -f $FILE fi