Friday, June 08, 2007

Setting up a PDF printer in CUPS (in my case in Slackware)

1. Create a sub-directory in CUPS directory sturcture
Code:
mkdir /usr/lib/cups/pdf
Put the following script into the newly created directory, e.g
Code:
vim /usr/lib/cups/pdf/ps2pdf.cups
i

Then highlight the following script with the mouse, and middle-click into the terminal session with vim open.
Code:
#!/bin/sh
# Convert PostScript to PDF.
umask 002
OPTIONS=""
while true
do
case "$1" in
-*) OPTIONS="$OPTIONS $1" ;;
*) break ;;
esac
shift
done

if [ $# -lt 1 -o $# -gt 2 ]; then
echo "Usage: `basename $0` [options...] input.ps [output.pdf]" 1>&2
exit 1
fi

infile=$1;

if [ $# -eq 1 ]
then
outfile=$1
else
outfile=$2
fi

# Doing an initial 'save' helps keep fonts from being flushed between pages.
exec gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
-sOutputFile=$outfile $OPTIONS -c save pop -f $infile
exec chmod a+r $outfile
After the middle-click, press ESC, :wq to save the file, followed by a
Code:
chmod 755 /usr/lib/cups/pdf/ps2pdf.cups
to make it executable.


2. Create the backend

The next bit of our PDF printer driver goes into /usr/lib/cups/backend
The back-end directory holds several binary executables that take care of varied printing methods, e.g. parallel, usb, ...

We're now going to add a shell-script that handles pdf-files!
Code:
vim /usr/lib/cups/backend/pdf
i
As before, copy & paste time
Code:
#!/bin/sh
#
umask 002
PDFBIN=/usr/lib/cups/pdf/ps2pdf.cups
FILENAME=
# filename of the PDF File
PRINTTIME=`date +%Y-%m-%d_%H.%M.%S`
# no argument, prints available URIs
if [ $# -eq 0 ]; then
if [ ! -x "$PDFBIN" ]; then
exit 0
fi
echo "direct pdf \"Unknown\" \"PDF Creator\""
exit 0
fi
# case of wrong number of arguments
if [ $# -ne 5 -a $# -ne 6 ]; then
echo "Usage: pdf job-id user title copies options [file]"
exit 1
fi
# get PDF directory from device URI, and check write status
PDFDIR=${DEVICE_URI#pdf:}
if [ ! -d "$PDFDIR" -o ! -w "$PDFDIR" ]; then
echo "ERROR: directory $PDFDIR not writable"
exit 1
fi
# generate output filename
OUTPUTFILENAME=
if [ "$3" = "" ]; then
OUTPUTFILENAME="$PDFDIR/unknown.pdf"
else
if [ "$2" != "" ]; then
OUTPUTFILENAME="$PDFDIR/$2-$PRINTTIME.pdf"
else
OUTPUTFILENAME="$PDFDIR/$PRINTTIME.pdf"
fi
echo "PDF file: $OUTPUTFILENAME placed in: $PDFDIR" >> $LOGFILE
fi
# run ghostscript
if [ $# -eq 6 ]; then
$PDFBIN $6 $OUTPUTFILENAME >& /dev/null
else
$PDFBIN - $OUTPUTFILENAME >& /dev/null
fi

exit 0

And as before, this script needs to be made executable, too.
Code:
chmod 755 /usr/lib/cups/backend/pdf


3. Download and install distiller.ppd

The printer definition file for distiller is freely available from the adobe-website, if you have a windows-installation handy follow steps 1 to 5 of the instructions...
If you don't: there's some other sites on the web that hold just the ppd rather than an installer. Google is your friend.

Once you have downloaded it add it to your CUPS' printer-model
directory:
cp distiller.ppd /usr/share/cups/model/distiller.ppd

and then, the second to last step:

4. Create the printer object

To be able to use all the pre-requisits that held us up ....
Code:
lpadmin -p PDF -v pdf:/directory/of/your/choice/ -E -P /usr/share/cups/model/distiller.ppd


5. Re-start CUPS

Now that all the set-up and installation is done, we need to let the CUPS daemon know about the new printer, too.

To achieve that we restart the daemon.

Code:
/etc/rc.d/rc.cups restart

And if all went well we should now have a printer-object that will create PDFs for us.

No comments: