2 # originally written by Michael Goffioul; modified & adapted by Kurt
3 # Pfeifle as part of is "Learning Shell Scripting" efforts. (WARNING:
4 # THIS WAS MY FIRST SHELL SCRIPT!)
6 # This script is intended to be used as a CUPS backend, to create
7 # PDFs file on-the-fly. Just create a printer using the device URI
8 # 'pdf:/path/to/dir/. When printing to this printer, a PDF file will
9 # be generated in the directory specified. The file name will either
10 # be "<jobname>-<some-other-info>.pdf" or "unknown-<printtime>.pdf",
11 # depending wether the jobname is empty or not.
13 # Attention: CUPS backends run with root privileges -- and so does
14 # ---------- this script! Use at your own risk!
16 # To use it, simply copy this script to your backend directory, and
17 # create a printer with the correct URI. That's it.
19 # In case you don't know how to create the PDF creating " printer",
20 # here is a reminder (you need the ps2pdf.ppd' in the CUPS default
21 # 'model' directory "/usr/share/cups/model/", and it needs to be
22 # a generic PostScript-PPD such as the distiller.ppd from Adobe)::
24 # "lpdmin -p pdfwriter -v pdf:/home/grml/PDFs -E -m ps2pdf.ppd"
26 # Copyright (C) Michael Goffioul (goffioul@imec.be) 2001
27 # (C) Kurt Pfeifle <kpfeifle@danka.de> 2002
30 # debug off by default; comment in if you desire debugging info....
33 if [ x"${DEBUG}" = x"yes" ]; then
35 LOGFILE="/tmp/pdf.log"
43 PRINTTIME=`date +%b%d-%H%M%S`
45 # first log a few things into the log file....
46 echo " " >> ${LOGFILE}
47 echo "####################################" >> ${LOGFILE}
48 echo "print and log time is ${PRINTTIME}" >> ${LOGFILE}
49 echo arg0 $0 >> ${LOGFILE} # printer
50 echo arg1 $1 >> ${LOGFILE} # job
51 echo arg2 $2 >> ${LOGFILE} # user
52 echo arg3 $3 >> ${LOGFILE} # title
53 echo arg4 $4 >> ${LOGFILE} # options
54 echo arg5 $5 >> ${LOGFILE} # copies
55 echo arg6 $6 >> ${LOGFILE} # <filename>; empty if taken from 'stdin'
56 echo args no. is '$#' = $# >> ${LOGFILE}
57 echo PRINTTIME ${PRINTTIME} >> ${LOGFILE}
58 echo PATH ${PATH} >> ${LOGFILE}
59 echo SOFTWARE ${SOFTWARE} >> ${LOGFILE}
60 echo USER ${USER} >> ${LOGFILE}
61 echo CHARSET ${CHARSET} >> ${LOGFILE}
62 echo LANG ${LANG} >> ${LOGFILE}
63 echo TZ ${TZ} >> ${LOGFILE}
64 echo PPD ${PPD} >> ${LOGFILE}
65 echo CUPS_SERVERROOT ${CUPS_SERVERROOT} >> ${LOGFILE}
66 echo RIP_MAX_CACHE ${RIP_MAX_CACHE} >> ${LOGFILE}
67 echo TMPDIR $TMPDIR >> ${LOGFILE}
68 echo CONTENT_TYPE $CONTENT_TYPE >> ${LOGFILE}
69 echo DEVICE_URI ${DEVICE_URI} >> ${LOGFILE}
70 echo PRINTER ${PRINTER} >> ${LOGFILE}
71 echo CUPS_DATADIR ${CUPS_DATADIR} >> ${LOGFILE}
72 echo CUPS_FONTPATH ${CUPS_FONTPATH} >> ${LOGFILE}
73 echo "Executable: ${PDFBIN}" >> ${LOGFILE}
74 echo "Arguments: | 1: $1 | 2: $2 | 3: $3 | 4: $4 | 5: $5 | 6: $6 |" >> ${LOGFILE}
75 echo " " >> ${LOGFILE}
76 echo "Executable: ${PDFBIN}" >> ${LOGFILE}
77 echo " " >> ${LOGFILE}
79 # case of no argument, *MUST* print available URI to satisfy cupsd's
80 # startup query and test for backend availability
82 if [ ! -x "$PDFBIN" ]; then
85 echo "direct pdf \"Unknown\" \"PDF Writing\""
89 # case of wrong number of arguments
90 if [ $# -ne 5 -a $# -ne 6 ]; then
91 echo "Usage: pdf job-id user title copies options [file]"
95 # get PDF directory from device URI, and check write status
96 PDFDIR=${DEVICE_URI#pdf:}
97 if [ ! -d "${PDFDIR}" -o ! -w "${PDFDIR}" ]; then
98 echo "ERROR: directory ${PDFDIR} not writable"
102 echo "PDF directory: ${PDFDIR}" >> ${LOGFILE}
105 # sanitize the job-title (may contain weird characters if printed fromm
110 # first the spaces.....
111 TITLE=`echo ${TITLE} | tr [:blank:] _`;
113 [ x"${DEBUG}" = x"yes" ] && echo "removed all spaces from original name of jobfile. Name is now ${TITLE}" >> ${LOGFILE};
115 # next the colons....
116 TITLE=`echo ${TITLE} | tr : _`;
117 [ x"${DEBUG}" = x"yes" ] && echo "removed all colons from original name of jobfile. Name is now ${TITLE}" >> ${LOGFILE};
119 # then the slashes....
120 TITLE=`echo ${TITLE} | tr / _`;
121 [ x"${DEBUG}" = x"yes" ] && echo "removed all slashes from original name of jobfile. Name is now ${TITLE}" >> ${LOGFILE};
123 # then the question-marks....
124 TITLE=`echo ${TITLE} | tr ? _`;
125 [ x"${DEBUG}" = x"yes" ] && echo "removed all question marks from original name of jobfile. Name is now ${TITLE}" >> ${LOGFILE};
127 # last the backslashes....
128 TITLE=`echo ${TITLE} | tr "\134" _`;
129 [ x"${DEBUG}" = x"yes" ] && echo "removed all backslashes from original name of jobfile. Name is now ${TITLE}" >> ${LOGFILE};
131 # we should now have a sanitized ${TITLE} to use with less danger of screwing things up....
133 SANITIZED_TITLE=${TITLE}
135 # generate output filename
137 if [ "$3" = "" ]; then
138 OUTPUTFILENAME="${PDFDIR}/unknown-${PRINTTIME}.pdf"
140 # OUTPUTFILENAME="${PDFDIR}/${3//[^[:alnum:]]/_}.pdf"
141 # I changed this to user name, and the printtime to track down who
142 # printed the PDF and when; Samba printing possibly uses 'nobody'...
144 OUTPUTFILENAME="${PDFDIR}/${SANITIZED_TITLE}-${PRINTTIME}-${USER}-${2}.pdf"
145 echo "PDF file: $OUTPUTFILENAME placed in: ${PDFDIR}" >> ${LOGFILE}
148 echo "Output file name: $OUTPUTFILENAME" >> ${LOGFILE}
151 if [ $# -eq 6 ]; then
152 $PDFBIN $6 "$OUTPUTFILENAME"
155 $PDFBIN - "$OUTPUTFILENAME"
161 # modify ownership and permissions on the file
163 # - owns to user specified in argument
164 chmod a+r "$OUTPUTFILENAME"
165 if [ "$2" != "" ]; then
166 chown $2 "$OUTPUTFILENAME"