summaryrefslogtreecommitdiff
path: root/pdfdistiller
blob: 351511746fddf878c662894df0d7d56a5e84f0a7 (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. # To use it, simply copy this script to your backend directory, and
  11. # create a printer with the correct URI. That's it.
  12. #
  13. # Copyright (C) Michael Goffioul (goffioul@imec.be) 2001
  14. LOGFILE=/tmp/pdf.log
  15. PDFBIN=`which ps2pdf`
  16. FILENAME=
  17. # this is borrowed from printpdf script for the filename
  18. PRINTTIME=`date +%b%d-%H%M%S`
  19. echo "Executable: $PDFBIN" > $LOGFILE
  20. echo "Arguments: |$1|$2|$3|$4|$5|$6|" >> $LOGFILE
  21. echo $# $PRINTTIME >> $LOGFILE
  22. # case of no argument, prints available URIs
  23. if [ $# -eq 0 ]; then
  24. if [ ! -x "$PDFBIN" ]; then
  25. exit 0
  26. fi
  27. echo "direct pdf \"Unknown\" \"PDF Writing\""
  28. exit 0
  29. fi
  30. # case of wrong number of arguments
  31. if [ $# -ne 5 -a $# -ne 6 ]; then
  32. echo "Usage: pdf job-id user title copies options [file]"
  33. exit 1
  34. fi
  35. # get PDF directory from device URI, and check write status
  36. PDFDIR=${DEVICE_URI#pdf:}
  37. if [ ! -d "$PDFDIR" -o ! -w "$PDFDIR" ]; then
  38. echo "ERROR: directory $PDFDIR not writable"
  39. exit 1
  40. fi
  41. echo "PDF directory: $PDFDIR" >> $LOGFILE
  42. # generate output filename
  43. OUTPUTFILENAME=
  44. if [ "$2" = "" ]; then
  45. OUTPUTFILENAME="$PDFDIR/unknown-$PRINTTIME.pdf"
  46. else
  47. # OUTPUTFILENAME="$PDFDIR/${3//[^[:alnum:]]/_}.pdf"
  48. # I changed this to user name, and the printtime to track down who
  49. # printed the PDF and when, samba printing just uses nobody
  50. OUTPUTFILENAME="$PDFDIR/$2-$PRINTTIME.pdf"
  51. echo "PDF file: $OUTPUTFILENAME placed in: $PDFDIR" >> $LOGFILE
  52. fi
  53. echo "Output file name: $OUTPUTFILENAME" >> $LOGFILE
  54. # run ghostscript
  55. if [ $# -eq 6 ]; then
  56. $PDFBIN $6 $OUTPUTFILENAME
  57. #>& /dev/null
  58. else
  59. $PDFBIN - $OUTPUTFILENAME >& /dev/null
  60. fi
  61. # modify ownership and permissions on the file
  62. # - world readable
  63. # - owns to user specified in argument
  64. chmod a+r $OUTPUTFILENAME
  65. if [ "$2" != "" ]; then
  66. if [ -x /usr/bin/mime-construct ]; then
  67. /usr/bin/mime-construct --to "$2" --subject "PDF print" --type application/pdf --attachment `basename $OUTPUTFILENAME` --file $OUTPUTFILENAME \
  68. && rm $OUTPUTFILENAME
  69. elif [ -x /usr/bin/mimeit ]; then
  70. cat $OUTPUTFILENAME | /usr/bin/mimeit application/pdf $2 "PDF print" \
  71. && rm $OUTPUTFILENAME
  72. else
  73. chown $2 $OUTPUTFILENAME
  74. fi
  75. fi
  76. exit 0