home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / cups / examples / pstopdf < prev    next >
Encoding:
Text File  |  2009-06-18  |  3.9 KB  |  138 lines

  1. #!/bin/sh
  2.  
  3. # $Id: pstopdf,v 1.3 2003/02/15 15:21:00 gurubert Exp $
  4. #
  5. # This is a Postscript to PDF filter for CUPS
  6. #
  7. # (C) 2003 Robert Sander <robert.sander@epigenomics.com>
  8. #
  9. # Released under GPL
  10. #
  11. # NO WARRANTY AT ALL
  12. #
  13.  
  14. set -e
  15.  
  16. PS2PS=/usr/bin/ps2ps
  17. PS2PDF=/usr/bin/ps2pdf13
  18. PS2PS_OPTIONS="-dAutoRotatePages=/None -dAutoFilterColorImages=false \
  19.                -dNOPLATFONTS -dPARANOIDSAFER -sstdout=%stderr"
  20. PS2PDF_OPTIONS="$PS2PS_OPTIONS -dColorImageFilter=/FlateEncode \
  21.                -dDoNumCopies -dPDFSETTINGS=/printer"
  22.  
  23. echo "DEBUG: pstopdf $# args: $@" >&2
  24. echo "DEBUG: PPD: $PPD" >&2
  25.  
  26. if [ $# -lt 5 -o $# -gt 6 ]; then
  27.  
  28.   echo "ERROR: $0 job-id user title copies options [file]" >&2
  29.   exit 1
  30.  
  31. fi
  32.  
  33. # Read from given file.
  34. if [ -n "$6" ]; then
  35.   exec <"$6"
  36. fi
  37.  
  38. tempfiles=
  39. trap 'rm -f $tempfiles' 0 1 2 13 15
  40.  
  41. infile=$(mktemp -t pstopdf.XXXXXX)
  42. tempfiles="$tempfiles $infile"
  43.  
  44. cat >"$infile"
  45.  
  46. # Apply PPD settings.
  47.  
  48. resolution=
  49. eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)Resolution=([0-9.]+(x[0-9.]+)?).*/resolution="${resolution:-\2}"/p')"
  50. if test -e "$PPD"; then
  51.   eval "$(sed -nre 's/^\*DefaultResolution:\s*([0-9.]+(x[0-9.]+)?).*/resolution="${resolution:-\1}"/p' "$PPD")"
  52. fi
  53. echo "DEBUG: Resolution: $resolution" >&2
  54.  
  55. width=
  56. height=
  57. bl_x=
  58. bl_y=
  59. tr_x=
  60. tr_y=
  61. margin_l=
  62. margin_b=
  63. margin_r=
  64. margin_t=
  65. pagesize=
  66. eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)(media|PageSize)=(\S+).*/pagesize="${pagesize:-\3}"/p')"
  67. if test -e "$PPD"; then
  68.     eval "$(sed -nre 's/^\*DefaultPageSize:\s*(\S+).*/pagesize="${pagesize:-\1}"/p' "$PPD")"
  69. fi
  70. echo "DEBUG: Page size: $pagesize" >&2
  71.  
  72. if test -n "$pagesize" && test -e "$PPD"; then
  73.     eval "$(sed -nre 's|^\*PaperDimension\s+'"$pagesize"'/[^:]+:\s*"(\S+)\s+(\S+)".*|width="\1"; height="\2"|p' "$PPD")"
  74.  
  75.     eval "$(sed -nre 's|^\*ImageableArea\s+'"$pagesize"'/[^:]+:\s*"(\S+)\s+(\S+)\s+(\S+)\s+(\S+)".*|bl_x="\1"; bl_y="\2"; tr_x="\3"; tr_y="\4"|p' "$PPD")"
  76. fi
  77. echo "DEBUG: Width: $width, height: $height, absolute margins: $bl_x, $bl_y, $tr_x, $tr_y" >&2
  78.  
  79. if test -n "$width" && test -n "$height" && \
  80.     test -n "$bl_x" && test -n "$bl_y" && \
  81.     test -n "$tr_x" && test -n "$tr_y"; then
  82.     margin_l="$bl_x"
  83.     margin_b="$bl_y"
  84.     margin_r="$(printf "scale=8; (%s)-(%s)\n" "$width" "$tr_x" | bc)"
  85.     margin_t="$(printf "scale=8; (%s)-(%s)\n" "$height" "$tr_y" | bc)"
  86. fi
  87. echo "DEBUG: Relative margins: $margin_l, $margin_b, $margin_r, $margin_t" >&2
  88.  
  89. if test -n "$margin_l" && test -n "$margin_b" && \
  90.     test -n "$margin_r" && test -n "$margin_t"; then
  91.     inject_ps="<</.HWMargins[$margin_l $margin_b $margin_r $margin_t] /Margins[0 0]>>setpagedevice"
  92. fi
  93.  
  94. ppd_opts=
  95. if test -n "$resolution"; then
  96.   ppd_opts="${ppd_opts:+$ppd_opts }-r$resolution"
  97. fi
  98. if test -n "$width"; then
  99.   ppd_opts="${ppd_opts:+$ppd_opts }-dDEVICEWIDTHPOINTS=$width"
  100. fi
  101. if test -n "$height"; then
  102.   ppd_opts="${ppd_opts:+$ppd_opts }-dDEVICEHEIGHTPOINTS=$height"
  103. fi
  104. echo "DEBUG: PPD options: $ppd_opts" >&2
  105.  
  106. # We do not supply the margins to the ps2pdf process, as this breaks
  107. # full-bleed printing and also disturbs the printing if PPDs have too
  108. # conservative margin definitions.
  109. inject_ps=
  110.  
  111. # Injection
  112. echo "DEBUG: PostScript to be injected: $inject_ps" >&2
  113. if test -n "$inject_ps"; then
  114.   echo "DEBUG: Injecting PostScript: $inject_ps" >&2
  115.  
  116.   orig_infile="$infile"
  117.  
  118.   infile=$(mktemp -t pstopdf.XXXXXX)
  119.   tempfiles="$tempfiles $infile"
  120.  
  121.   perl -p -e 'if (! $did) { s:(^%!.*)$:\1\n'"$inject_ps"': && $did++; }' "$orig_infile" > "$infile"
  122. fi
  123.  
  124. # DRM
  125.  
  126. DRM_MATCH='^%.*Removing the following.*lines is illegal.*Digital Copyright Act'
  127. if egrep -q "$DRM_MATCH" "$infile"; then
  128.   # This PS is DRM-infested. Normalize it with ps2ps first.
  129.   echo "DEBUG: Normalizing Adobe Reader PostScript with ps2ps" >&2
  130.  
  131.   DRMFILTER="$PS2PS $PS2PS_OPTIONS $ppd_opts - -"
  132. else
  133.   DRMFILTER=cat
  134. fi
  135.  
  136. echo "DEBUG: Running $DRMFILTER | $PS2PDF $PS2PDF_OPTIONS $ppd_opts - -" >&2
  137. cat "$infile" | $DRMFILTER | $PS2PDF $PS2PDF_OPTIONS $ppd_opts - -
  138.