summaryrefslogtreecommitdiff
path: root/pdfdistiller
blob: 7c4044ae2fff8cb12f93efba1bbee9d2fce64d49 (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. # generate output filename
  47. OUTPUTFILENAME=
  48. if [ "$2" = "" ]; then
  49. OUTPUTFILENAME="$PDFDIR/unknown-$PRINTTIME.pdf"
  50. else
  51. # OUTPUTFILENAME="$PDFDIR/${3//[^[:alnum:]]/_}.pdf"
  52. # I changed this to user name, and the printtime to track down who
  53. # printed the PDF and when, samba printing just uses nobody
  54. OUTPUTFILENAME="$PDFDIR/$2-$PRINTTIME.pdf"
  55. echo "PDF file: $OUTPUTFILENAME placed in: $PDFDIR" >> $LOGFILE
  56. fi
  57. echo "Output file name: $OUTPUTFILENAME" >> $LOGFILE
  58. # run ghostscript
  59. if [ $# -eq 6 ]; then
  60. $PDFBIN $6 $OUTPUTFILENAME
  61. #>& /dev/null
  62. else
  63. $PDFBIN - $OUTPUTFILENAME >& /dev/null
  64. fi
  65. # modify ownership and permissions on the file
  66. # - world readable
  67. # - owns to user specified in argument
  68. chmod a+r $OUTPUTFILENAME
  69. if [ "$2" != "" ]; then
  70. if [ -x /usr/bin/mime-construct ]; then
  71. /usr/bin/mime-construct --to "$2" --subject "PDF print" --type application/pdf --attachment `basename $OUTPUTFILENAME` --file $OUTPUTFILENAME \
  72. && rm $OUTPUTFILENAME
  73. elif [ -x /usr/bin/mimeit ]; then
  74. cat $OUTPUTFILENAME | /usr/bin/mimeit application/pdf $2 "PDF print" \
  75. && rm $OUTPUTFILENAME
  76. else
  77. chown $2 $OUTPUTFILENAME
  78. fi
  79. fi
  80. exit 0