16
PSTOPDF=/usr/bin/ps2pdf13
17
OPTIONS="-r150 -dAutoRotatePages=/None -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dNOPLATFONTS"
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
-dPDFSETTINGS=/printer"
19
echo "INFO: pstopdf argv[$#] = $@" >&2
23
echo "DEBUG: pstopdf $# args: $@" >&2
24
echo "DEBUG: PPD: $PPD" >&2
21
26
if [ $# -lt 5 -o $# -gt 6 ]; then
29
outfile=$(mktemp "${TMPDIR:-/tmp}/$jobid.pstopdf.out.XXXXXX")
30
trap 'rm -f "$outfile"' 0 1 2 13 15
34
$PSTOPDF $OPTIONS "$infile" "$outfile" >&2
33
# Read from given file.
39
trap 'rm -f $tempfiles' 0 1 2 13 15
41
infile=$(mktemp -t pstopdf.XXXXXX)
42
tempfiles="$tempfiles $infile"
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")"
53
echo "DEBUG: Resolution: $resolution" >&2
65
# Apply a paper size to the conversion process only if the "fitplot" or
66
# "fit-to-page" option is supplied
67
if echo "$5" | egrep -q '\b(fitplot|fit-to-page)\b'; then
69
eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)(media|PageSize)=(\S+).*/pagesize="${pagesize:-\3}"/p')"
70
if test -e "$PPD"; then
71
eval "$(sed -nre 's/^\*DefaultPageSize:\s+(\S+).*/pagesize="${pagesize:-\1}"/p' "$PPD")"
73
echo "DEBUG: Page size: $pagesize" >&2
75
if test -n "$pagesize" && test -e "$PPD"; then
76
eval "$(sed -nre 's|^\*PaperDimension\s+'"$pagesize"'/[^:]+:\s+"(\S+)\s+(\S+)".*|width="\1"; height="\2"|p' "$PPD")"
78
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")"
80
echo "DEBUG: Width: $width, height: $height, absolute margins: $bl_x, $bl_y, $tr_x, $tr_y" >&2
82
if test -n "$width" && test -n "$height" && \
83
test -n "$bl_x" && test -n "$bl_y" && \
84
test -n "$tr_x" && test -n "$tr_y"; then
87
margin_r="$(printf "scale=8; (%s)-(%s)\n" "$width" "$tr_x" | bc)"
88
margin_t="$(printf "scale=8; (%s)-(%s)\n" "$height" "$tr_y" | bc)"
90
echo "DEBUG: Relative margins: $margin_l, $margin_b, $margin_r, $margin_t" >&2
92
if test -n "$margin_l" && test -n "$margin_b" && \
93
test -n "$margin_r" && test -n "$margin_t"; then
94
inject_ps="<</.HWMargins[$margin_l $margin_b $margin_r $margin_t] /Margins[0 0]>>setpagedevice"
99
if test -n "$resolution"; then
100
ppd_opts="${ppd_opts:+$ppd_opts }-r$resolution"
102
if test -n "$width"; then
103
ppd_opts="${ppd_opts:+$ppd_opts }-dDEVICEWIDTHPOINTS=$width"
105
if test -n "$height"; then
106
ppd_opts="${ppd_opts:+$ppd_opts }-dDEVICEHEIGHTPOINTS=$height"
108
echo "DEBUG: PPD options: $ppd_opts" >&2
110
# We do not supply the margins to the ps2pdf process, as this breaks
111
# full-bleed printing and also disturbs the printing if PPDs have too
112
# conservative margin definitions.
116
echo "DEBUG: PostScript to be injected: $inject_ps" >&2
117
if test -n "$inject_ps"; then
118
echo "DEBUG: Injecting PostScript: $inject_ps" >&2
120
orig_infile="$infile"
122
infile=$(mktemp -t pstopdf.XXXXXX)
123
tempfiles="$tempfiles $infile"
125
perl -p -e 'if (! $did) { s:(^%!.*)$:\1\n'"$inject_ps"': && $did++; }' "$orig_infile" > "$infile"
130
DRM_MATCH='^%.*Removing the following.*lines is illegal.*Digital Copyright Act'
131
if egrep -q "$DRM_MATCH" "$infile"; then
132
# This PS is DRM-infested. Normalize it with ps2ps first.
133
echo "DEBUG: Normalizing Adobe Reader PostScript with ps2ps" >&2
135
DRMFILTER="$PS2PS $PS2PS_OPTIONS $ppd_opts - -"
140
echo "DEBUG: Running $DRMFILTER | $PS2PDF $PS2PDF_OPTIONS $ppd_opts - -" >&2
141
cat "$infile" | $DRMFILTER | $PS2PDF $PS2PDF_OPTIONS $ppd_opts - -