3
# $Id: pstopdf,v 1.3 2003/02/15 15:21:00 gurubert Exp $
5
# This is a Postscript to PDF filter for CUPS
7
# (C) 2003 Robert Sander <robert.sander@epigenomics.com>
8
# (C) 2008-2012 Till Kamppeter <till.kamppeter@gmail.com>
20
PS2PS_OPTIONS="-dAutoRotatePages=/None -dAutoFilterColorImages=false \
21
-dNOPLATFONTS -dPARANOIDSAFER -dNOINTERPOLATE -sstdout=%stderr"
22
PS2PDF_OPTIONS="-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.3"
23
PS2PDF_OPTIONS="$PS2PDF_OPTIONS $PS2PS_OPTIONS"
24
PS2PDF_OPTIONS="$PS2PDF_OPTIONS -dColorImageFilter=/FlateEncode \
25
-dPDFSETTINGS=/printer \
26
-dColorConversionStrategy=/LeaveColorUnchanged"
28
echo "DEBUG: pstopdf $# args: $@" >&2
29
echo "DEBUG: PPD: $PPD" >&2
31
if [ $# -lt 5 -o $# -gt 6 ]; then
33
echo "ERROR: $0 job-id user title copies options [file]" >&2
38
# Read from given file.
44
trap 'rm -f $tempfiles' 0 1 2 13 15
46
infile=$(mktemp -t pstopdf.XXXXXX)
47
tempfiles="$tempfiles $infile"
51
# Did CUPS already take care of the number of copies? If not, we have to do it
55
if test -z "$numcopies"; then
58
if test "$numcopies" -le "1"; then
59
donumcopies="-dDoNumCopies"
65
eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)Resolution=([0-9.]+(x[0-9.]+)?).*/resolution="${resolution:-\2}"/p')"
66
if test -e "$PPD"; then
67
eval "$(sed -nre 's/^\*DefaultResolution:\s*([0-9.]+(x[0-9.]+)?).*/resolution="${resolution:-\1}"/p' "$PPD")"
69
echo "DEBUG: Resolution: $resolution" >&2
71
if test -n "$resolution"; then
72
# If the resolution is not symmetric, use the lower of the two,
73
# Ghostscript developers recommend to use square resolutions for the
74
# pdfwrite and ps2write output devices.
75
# See http://bugs.ghostscript.com/show_bug.cgi?id=690504
78
eval "$(printf "%s" "$resolution" | sed -nre 's/.*(^|\s)([0-9]+)x([0-9]+).*/xres="\2"; yres="\3"/p')"
79
if test -n "$xres" && test -n "$yres"; then
80
if [ "$xres" -lt "$yres" ]; then
102
eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)(PageSize|PageRegion)=(\S+).*/pagesize="${pagesize:-\3}"/p')"
103
if test -e "$PPD"; then
104
eval "$(sed -nre 's/^\*DefaultPageSize:\s*(\S+).*/pagesize="${pagesize:-\1}"/p' "$PPD")"
106
echo "DEBUG: Page size: $pagesize" >&2
108
eval "$(printf "%s" "$pagesize" | sed -nre 's/^Custom\.([0-9\.]+)x([0-9\.]+)(\S*)$/customw="\1"; customh="\2"; unit="\3"/p')"
110
if test -n "$customw" && test -n "$customh"; then
111
echo "DEBUG: Custom page size: $customw x $customh $unit" >&2
113
if test "$unit" = "in"; then
114
width="$(printf "scale=0; (%s)*(72.0)/(1.00)\n" "$customw" | bc)"
115
height="$(printf "scale=0; (%s)*(72.0)/(1.00)\n" "$customh" | bc)"
116
elif test "$unit" = "cm"; then
117
width="$(printf "scale=0; (%s)*(72.0)/(2.54)\n" "$customw" | bc)"
118
height="$(printf "scale=0; (%s)*(72.0)/(2.54)\n" "$customh" | bc)"
119
elif test "$unit" = "mm"; then
120
width="$(printf "scale=0; (%s)*(72.0)/(25.4)\n" "$customw" | bc)"
121
height="$(printf "scale=0; (%s)*(72.0)/(25.4)\n" "$customh" | bc)"
123
width="$(printf "scale=0; (%s)/(1.00)\n" "$customw" | bc)"
124
height="$(printf "scale=0; (%s)/(1.00)\n" "$customh" | bc)"
127
if test -e "$PPD"; then
128
eval "$(sed -nre 's|^\*HWMargins:\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*|bl_x="\1"; bl_y="\2"; tr_x="\3"; tr_y="\4"|p' "$PPD")"
130
if test -n "$tr_x"; then
131
tr_x="$(printf "scale=8; (%s)-(%s)\n" "$width" "$tr_x" | bc)"
133
if test -n "$tr_y"; then
134
tr_y="$(printf "scale=8; (%s)-(%s)\n" "$height" "$tr_y" | bc)"
137
elif test -n "$pagesize" && test -e "$PPD"; then
138
eval "$(sed -nre 's|^\*PaperDimension\s+'"$pagesize"'(/[^:]+\|):\s*"(\S+)\s+(\S+)".*|width="\2"; height="\3"|p' "$PPD")"
140
eval "$(sed -nre 's|^\*ImageableArea\s+'"$pagesize"'(/[^:]+\|):\s*"(\S+)\s+(\S+)\s+(\S+)\s+(\S+)".*|bl_x="\2"; bl_y="\3"; tr_x="\4"; tr_y="\5"|p' "$PPD")"
143
test -n "$bl_x" || bl_x=0
144
test -n "$bl_y" || bl_y=0
145
test -n "$tr_x" || tr_x=$width
146
test -n "$tr_y" || tr_y=$height
148
echo "DEBUG: Width: $width, height: $height, absolute margins: $bl_x, $bl_y, $tr_x, $tr_y" >&2
150
if test -n "$width" && test -n "$height" && \
151
test -n "$bl_x" && test -n "$bl_y" && \
152
test -n "$tr_x" && test -n "$tr_y"; then
155
margin_r="$(printf "scale=8; (%s)-(%s)\n" "$width" "$tr_x" | bc)"
156
margin_t="$(printf "scale=8; (%s)-(%s)\n" "$height" "$tr_y" | bc)"
158
echo "DEBUG: Relative margins: $margin_l, $margin_b, $margin_r, $margin_t" >&2
160
if test -n "$margin_l" && test -n "$margin_b" && \
161
test -n "$margin_r" && test -n "$margin_t"; then
162
inject_ps="<</.HWMargins[$margin_l $margin_b $margin_r $margin_t] /Margins[0 0]>>setpagedevice"
166
if test -n "$resolution"; then
167
ppd_opts="${ppd_opts:+$ppd_opts }-r$resolution"
169
if test -n "$width"; then
170
ppd_opts="${ppd_opts:+$ppd_opts }-dDEVICEWIDTHPOINTS=$width"
172
if test -n "$height"; then
173
ppd_opts="${ppd_opts:+$ppd_opts }-dDEVICEHEIGHTPOINTS=$height"
175
echo "DEBUG: PPD options: $ppd_opts" >&2
177
# We do not supply the margins to the ps2pdf process, as this breaks
178
# full-bleed printing and also disturbs the printing if PPDs have too
179
# conservative margin definitions.
183
echo "DEBUG: PostScript to be injected: $inject_ps" >&2
184
if test -n "$inject_ps"; then
185
echo "DEBUG: Injecting PostScript: $inject_ps" >&2
187
orig_infile="$infile"
189
infile=$(mktemp -t pstopdf.XXXXXX)
190
tempfiles="$tempfiles $infile"
192
perl -p -e 'if (! $did) { s:(^%!.*)$:\1\n'"$inject_ps"': && $did++; }' "$orig_infile" > "$infile"
197
DRM_MATCH='^%.*Removing the following.*lines is illegal.*Digital Copyright Act'
198
if egrep -q "$DRM_MATCH" "$infile"; then
199
# This PS is DRM-infested. Normalize it with ps2ps first.
200
echo "DEBUG: Normalizing Adobe Reader PostScript with ps2ps" >&2
202
DRMFILTER="$PS2PS $PS2PS_OPTIONS $ppd_opts - -"
207
echo "DEBUG: Running $DRMFILTER | $GS $PS2PDF_OPTIONS $donumcopies $ppd_opts -sOutputFile=- $OPTIONS -c .setpdfwrite -f -" >&2
208
cat "$infile" | $DRMFILTER | $GS $PS2PDF_OPTIONS $donumcopies $ppd_opts -sOutputFile=- $OPTIONS -c .setpdfwrite -f -