summaryrefslogtreecommitdiff
path: root/pdfdistiller
blob: 9a3298fd5a454f6307f0afa4e00e20cdd903744b (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. # modify ownership and permissions on the file
  71. # - world readable
  72. # - owns to user specified in argument
  73. chmod a+r $OUTPUTFILENAME
  74. if [ "$user" != "" ]; then
  75. if [ -x /usr/bin/mime-construct ]; then
  76. echo "Send as pdf to $user using mime-construct" >> $LOGFILE
  77. /usr/bin/mime-construct --to "$user" --subject "PDF print" --type application/pdf --attachment `basename $OUTPUTFILENAME` --file $OUTPUTFILENAME \
  78. && rm $OUTPUTFILENAME
  79. elif [ -x /usr/bin/mimeit ]; then
  80. echo "Send as pdf to $user using mimeit" >> $LOGFILE
  81. cat $OUTPUTFILENAME | /usr/bin/mimeit application/pdf $user "PDF print" \
  82. && rm $OUTPUTFILENAME
  83. else
  84. chown $user $OUTPUTFILENAME
  85. fi
  86. fi
  87. exit 0