summaryrefslogtreecommitdiff
path: root/pdfdistiller
blob: 3a39692ee2f2dfe53011d247219cdc32c6ed7b6b (plain)
  1. #!/bin/sh
  2. #
  3. # This script is intended to be used as a CUPS backend, to create
  4. # PDF file on-the-fly. Just create a printer using the device uri
  5. # pdf:/path/to/dir/. When printing to this printer, a PDF file
  6. # will be generated in the directory specified. The file name will
  7. # be either "<jobname>.pdf" or "unknown.pdf", depending wether the
  8. # jobname is empty or not.
  9. #
  10. # Usage:
  11. # 1. Copy this script to your backend directory (/usr/lib/cups/backend)
  12. # and name it "pdf".
  13. # 2. Create a printer in CUPS and use the URI "pdf:/tmp".
  14. # 3. Optional: Install either mime-construct or mimeit (part of
  15. # metamail) to have the PDF files emailed instead of dumped to dir.
  16. #
  17. # Copyright (C) Michael Goffioul (goffioul@imec.be) 2001
  18. LOGFILE=/tmp/pdf.log
  19. PDFBIN=`which ps2pdf`
  20. FILENAME=
  21. # this is borrowed from printpdf script for the filename
  22. PRINTTIME=`date +%b%d-%H%M%S`
  23. echo "Executable: $PDFBIN" > $LOGFILE
  24. echo "Arguments: |$1|$2|$3|$4|$5|$6|" >> $LOGFILE
  25. echo $# $PRINTTIME >> $LOGFILE
  26. # case of no argument, prints available URIs
  27. if [ $# -eq 0 ]; then
  28. if [ ! -x "$PDFBIN" ]; then
  29. exit 0
  30. fi
  31. echo "direct pdf \"Unknown\" \"PDF Writing\""
  32. exit 0
  33. fi
  34. # case of wrong number of arguments
  35. if [ $# -ne 5 -a $# -ne 6 ]; then
  36. echo "Usage: pdf job-id user title copies options [file]"
  37. exit 1
  38. fi
  39. # get PDF directory from device URI, and check write status
  40. PDFDIR=${DEVICE_URI#pdf:}
  41. if [ ! -d "$PDFDIR" -o ! -w "$PDFDIR" ]; then
  42. echo "ERROR: directory $PDFDIR not writable"
  43. exit 1
  44. fi
  45. echo "PDF directory: $PDFDIR" >> $LOGFILE
  46. user=$2
  47. # Avoid email to non-user accounts
  48. case "$user" in
  49. nobody|guest) user=;;
  50. esac
  51. # generate output filename
  52. OUTPUTFILENAME=
  53. if [ "$user" = "" ]; then
  54. OUTPUTFILENAME="$PDFDIR/unknown-$PRINTTIME.pdf"
  55. else
  56. # OUTPUTFILENAME="$PDFDIR/${3//[^[:alnum:]]/_}.pdf"
  57. # I changed this to user name, and the printtime to track down who
  58. # printed the PDF and when, samba printing just uses nobody
  59. OUTPUTFILENAME="$PDFDIR/$user-$PRINTTIME.pdf"
  60. echo "PDF file: $OUTPUTFILENAME placed in: $PDFDIR" >> $LOGFILE
  61. fi
  62. echo "Output file name: $OUTPUTFILENAME" >> $LOGFILE
  63. # run ghostscript
  64. if [ $# -eq 6 ]; then
  65. $PDFBIN $6 $OUTPUTFILENAME
  66. #>& /dev/null
  67. else
  68. $PDFBIN - $OUTPUTFILENAME >& /dev/null
  69. fi
  70. # If the printername resolves to a .xml file and XenuxPDF is installed run it
  71. XENUXPDF="/usr/local/XenuxPDF"
  72. CONFIG="$XENUXPDF/$PRINTER.xml"
  73. if [[ -e "$XENUXPDF/xenuxpdf.py" && -e $CONFIG ]]; then
  74. echo "Calling XenuxPDF" >> $LOGFILE
  75. PYTHONBIN=`which python`
  76. $PYTHONBIN "$XENUXPDF/xenuxpdf.py" $CONFIG $OUTPUTFILENAME >> $LOGFILE 2>&1
  77. fi
  78. # modify ownership and permissions on the file
  79. # - world readable
  80. # - owns to user specified in argument
  81. chmod a+r $OUTPUTFILENAME
  82. if [ "$user" != "" ]; then
  83. if [ -x /usr/bin/mime-construct ]; then
  84. echo "Send as pdf to $user using mime-construct" >> $LOGFILE
  85. /usr/bin/mime-construct --to "$user" --subject "PDF print" --type application/pdf --attachment `basename $OUTPUTFILENAME` --file $OUTPUTFILENAME \
  86. && rm $OUTPUTFILENAME
  87. elif [ -x /usr/bin/mimeit ]; then
  88. echo "Send as pdf to $user using mimeit" >> $LOGFILE
  89. cat $OUTPUTFILENAME | /usr/bin/mimeit application/pdf $user "PDF print" \
  90. && rm $OUTPUTFILENAME
  91. else
  92. #The following ensures that PDF has filerights so everybody of fsadmin group can delete it.
  93. chown $user:fsadmin $OUTPUTFILENAME
  94. fi
  95. fi
  96. exit 0