#!/bin/sh # $Id: $ # # This is a PDF to PostScript filter for CUPS # # (C) 2003 Robert Sander # # Released under GPL # # NO WARRANTY AT ALL # set -e PDF2PS=/usr/bin/pdf2ps PS2PDF_OPTIONS="-dLanguageLevel=2" echo "DEBUG: pstopdf argv[$#] = $@" >&2 echo "DEBUG: PPD: $PPD" >&2 if [ $# -lt 5 -o $# -gt 6 ]; then echo "ERROR: $0 job-id user title copies options [file]" >&2 exit 1 fi # Read from given file. if [ -n "$6" ]; then exec <"$6" fi # Apply PPD settings. resolution= eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)Resolution=([0-9.]+(x[0-9.]+)?).*/resolution="${resolution:-\2}"/p')" if test -e "$PPD"; then eval "$(sed -nre 's/^\*DefaultResolution:\s+([0-9.]+(x[0-9.]+)?).*/resolution="${resolution:-\1}"/p' "$PPD")" fi echo "DEBUG: Resolution: $resolution" >&2 pagesize= eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)(media|PageSize)=(\S+).*/pagesize="${pagesize:-\3}"/p')" if test -e "$PPD"; then eval "$(sed -nre 's/^\*DefaultPageSize:\s+(\S+).*/pagesize="${pagesize:-\1}"/p' "$PPD")" fi echo "DEBUG: Page size: $pagesize" >&2 width= height= bl_x= bl_y= tr_x= tr_y= if test -n "$pagesize" && test -e "$PPD"; then eval "$(sed -nre 's|^\*PaperDimension\s+'"$pagesize"'/[^:]+:\s+"(\S+)\s+(\S+)".*|width="\1"; height="\2"|p' "$PPD")" 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")" fi echo "DEBUG: Width: $width, height: $height, absolute margins: $bl_x, $bl_y, $tr_x, $tr_y" >&2 margin_l= margin_b= margin_r= margin_t= if test -n "$width" && test -n "$height" && \ test -n "$bl_x" && test -n "$bl_y" && \ test -n "$tr_x" && test -n "$tr_y"; then margin_l="$bl_x" margin_b="$bl_y" margin_r="$(printf "scale=8; (%s)-(%s)\n" "$width" "$tr_x" | bc)" margin_t="$(printf "scale=8; (%s)-(%s)\n" "$height" "$tr_y" | bc)" fi echo "DEBUG: Relative margins: $margin_l, $margin_b, $margin_r, $margin_t" >&2 if test -n "$margin_l" && test -n "$margin_b" && \ test -n "$margin_r" && test -n "$margin_t"; then inject_ps="<>setpagedevice" fi ppd_opts= if test -n "$resolution"; then ppd_opts="${ppd_opts:+$ppd_opts }-r$resolution" fi if test -n "$width"; then ppd_opts="${ppd_opts:+$ppd_opts }-dDEVICEWIDTHPOINTS=$width" fi if test -n "$height"; then ppd_opts="${ppd_opts:+$ppd_opts }-dDEVICEHEIGHTPOINTS=$height" fi echo "DEBUG: PPD options: $ppd_opts" >&2 # We read the data into a temporary file as Ghostscript needs this to be # able to work with PDF input tempfiles= trap 'rm -f $tempfiles' 0 1 2 13 15 ifile=$(mktemp -t pstopxl.XXXXXX) tempfiles="$tempfiles $ifile" # Allow rendering of PDF files which have non-PDF stuff before and after the # real PDF part, like for example PJL commands. Remove everything before # "%PDF-X.Y" and after "%%EOF" perl -e '$f = join("", <>); $f =~ s/^.*(\%PDF-\d+\.\d+)/$1/s; $f =~ s/(\%\%EOF\r?\n?).*$/$1/s; print $f' > "$ifile" echo "DEBUG: Running $PDF2PS $PDF2PS_OPTIONS $ppd_opts $ifile -" >&2 $PDF2PS $PDF2PS_OPTIONS $ppd_opts "$ifile" -