summaryrefslogtreecommitdiff
path: root/pdfdistiller
blob: c97b7dd7d0080c96acf0d5a0b489cbf4ca77364c (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. #I found out that when the user 'nobody' prints, the script tries to mail the PDF
  49. #to that user - as a local mail. To avoid this, I changed the conditions in the if-statement below.
  50. if [ "$2" = "" -o "$2" = "nobody" ]; then
  51. OUTPUTFILENAME="$PDFDIR/unknown-$PRINTTIME.pdf"
  52. else
  53. # OUTPUTFILENAME="$PDFDIR/${3//[^[:alnum:]]/_}.pdf"
  54. # I changed this to user name, and the printtime to track down who
  55. # printed the PDF and when, samba printing just uses nobody
  56. OUTPUTFILENAME="$PDFDIR/$2-$PRINTTIME.pdf"
  57. echo "PDF file: $OUTPUTFILENAME placed in: $PDFDIR" >> $LOGFILE
  58. fi
  59. echo "Output file name: $OUTPUTFILENAME" >> $LOGFILE
  60. # run ghostscript
  61. if [ $# -eq 6 ]; then
  62. $PDFBIN $6 $OUTPUTFILENAME
  63. #>& /dev/null
  64. else
  65. $PDFBIN - $OUTPUTFILENAME >& /dev/null
  66. fi
  67. # modify ownership and permissions on the file
  68. # - world readable
  69. # - owns to user specified in argument
  70. chmod a+r $OUTPUTFILENAME
  71. #An extra argument in this statement proved to be needed to ensure that after entering
  72. #the previous if-statement (in case user is nobody) this if-statement isn't entered as well..
  73. if [ "$2" != "" -a $2 != "nobody" ]; then
  74. if [ -x /usr/bin/mime-construct ]; then
  75. /usr/bin/mime-construct --to "$2" --subject "PDF print" --type application/pdf --attachment `basename $OUTPUTFILENAME` --file $OUTPUTFILENAME \
  76. && rm $OUTPUTFILENAME
  77. elif [ -x /usr/bin/mimeit ]; then
  78. cat $OUTPUTFILENAME | /usr/bin/mimeit application/pdf $2 "PDF print" \
  79. && rm $OUTPUTFILENAME
  80. else
  81. chown $2 $OUTPUTFILENAME
  82. fi
  83. fi
  84. exit 0