#!/bin/bash # CUPS PDF writer # # Copyright (C) 2009, Christopher J. Cox # Copyright (C) 2009, Sterling Commerce, Inc. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # # This program tries really hard to always be successful... even when # it isn't. Certain things have NOT been well tested, for example, # I have not tested a ps2pdf failure condition yet. # # # If you run the cups daemon as something other than root, this program # has to be able to write as that user (e.g. lp) into PDFDIR. If # PDFDIR is NOT set below, then PDFDIR defaults to a per-user directory # inside the user's home directory. The filename written defaults to # the -.pdf. If PDFDIR is set, # then it defaults to --.pdf. # # # Set PDFDIR is you want all results written to a common directory. # How a user gets to their PDF output in that directory is left # up to the administrator. Otherwise, output goes to the user's # home directory under pdfdirname. #PDFDIR="/var/spool/pdf" # Directory created in user's home dir if PDFDIR not set. # pdfdirname="CUPS-PDF" # Add an extra debug logger line or two with this set # pdf_writer_debug=1 # Find the ps2pdf program (sometimes people override the default) # ps2pdf_exe=`/usr/bin/which ps2pdf` # Date stamp for semi-unique file naming and when run info. # date_stamp=`/bin/date +%Y%m%d%H%M%S` # What uid am I running as? Assume 0 is root and can do chown/chmod. # Note: if running as root but without an effective pdf output directory # that root cannot operate in... NFS for example, then hardcode uid # here to something other than 0. It is only used for checking if root # or not. # uid=`/usr/bin/id -u` # CUPS will pass these arguments to us. # cups_jobnum="$1" cups_user="$2" cups_title="$3" cups_copies="$4" cups_options="$5" cups_file="$6" # # General note, I always return 0 on exit, to make sure the cups printer # doesn't go down. # if [ "${cups_jobnum}" = "" ]; then if [ -x "${ps2pdf_exe}" ]; then echo "network pdf-writer:/var/spool/pdf \"unknown\" \"pdf writer\" " rc=0 else echo "Error: $0 - ps2pdf is not available!" logger "pdf-writer: ERROR: ps2pdf is not available!" rc=1 exit 0 fi fi if [ "${pdf_writer_debug}" ]; then /bin/logger "pdf-writer started: job:[${cups_jobnum}] user:[${cups_user}] title:[${cups_title}] copies:[${cups_copies}] options:[${cups_options}] file:[${cups_file}]" fi # Check number of command line arguments # if [ $# -ne 5 -a $# -ne 6 ]; then echo "Usage: $0 jobnum user title copies options [file]" /bin/logger "pdf-writer: CRITICAL: Printer stopped !" rc=2 exit 0 fi # get directory from device URI # pdfpath=${DEVICE_URI#pdf-writer:} # Check for cups_user # if [ ! "${cups_user}" ]; then /bin/logger "pdf-writer: ERROR: no user!" rc=3 exit 0 fi if [ ! "${PDFDIR}" ]; then # Check for cups_users home dir # homedir=`/usr/bin/getent passwd ${cups_user} | cut -f6 -d:` if [ ! "${homedir}" ]; then /bin/logger "pdf-writer: ERROR: No home directory for user, ${cups_user}!" rc=4 exit 0 fi pdfdir="${homedir}/${pdfdirname}" pdfuser="" else pdfdir="${PDFDIR}" pdfuser="${cups_user}" fi # Check for writable CUPS-PDF directory # if [ ! -w "${pdfdir}" ]; then /bin/logger "pdf-writer(${cups_jobnum}): ERROR: PDF dir: ${pdfdir} not writable!" rc=5 exit 0 fi # Create filename for pdf output. # pdffilename="${pdfdir}/${pdfuser}${pdfuser:+-}${cups_title}${cups_title:+-}${date_stamp}.pdf" if [ ${uid} -ne 0 ]; then # No umask, so the user can remove the pdf file this creates ideally. # umask 0 fi # Create PDF file. # if [ $# -eq 6 ]; then pdferr=`${ps2pdf_exe} $6 "${pdffilename}"` else pdferr=`${ps2pdf_exe} - "${pdffilename}"` fi if [ $? -ne 0 ]; then /bin/logger "pdf-writer(${cups_jobnum}): ERROR: ${ps2pdf_exe}, message was: ${pdferr}" rc=6 exit 0 else #Change ownership if possible # if [ ${uid} -eq 0 ]; then /bin/chown "${cups_user}" "${pdffilename}" /bin/chmod u+rw "${pdffilename}" fi fi exit 0