@@ -13,14 +13,47 @@ set -euo pipefail
1313
1414PDFTOPS=@pdftops@
1515PSTOPS=@pstops@
16+ GREP=@grep@
17+ AWK=@awk@
18+ MKTEMP=@mktemp@
19+ RM=@rm@
1620
1721if [ " $# " -ne 5 ] && [ " $# " -ne 6 ]; then
1822 echo " Usage: $0 job-id user title copies options [file]"
1923 exit
2024fi
2125
26+ copies=" $4 "
27+
2228if [ " $# " -eq 5 ]; then
2329 $PDFTOPS - - | $PSTOPS " $@ "
2430else
25- $PDFTOPS " $6 " - | $PSTOPS " ${@: 1: 5} "
31+ # Generate PS to a temp file so we can inspect it before passing to pstops.
32+ #
33+ # Firefox and GTK print dialogs do not send a CUPS/IPP copies attribute;
34+ # instead they embed /NumCopies in the PDF's viewer preferences dictionary
35+ # (possibly inside compressed object streams, so the raw PDF is not
36+ # grep-able). Poppler's pdftops reads that dictionary and emits
37+ # "<</NumCopies N>> setpagedevice" in the PS output when N > 1. We read
38+ # that value and pass it as the copies count to pstops so the printer
39+ # receives the correct copy count.
40+ ps_tmp=$( $MKTEMP /tmp/ocfps-XXXXXX.ps)
41+ trap ' $RM -f "$ps_tmp"' EXIT
42+
43+ $PDFTOPS " $6 " " $ps_tmp "
44+
45+ echo " [ocfps] ps_tmp size=$( $AWK ' END{print NR}' " $ps_tmp " ) lines, copies_arg=$copies " >&2
46+ $GREP -om3 ' NumCopies [0-9]*' " $ps_tmp " >&2 || echo " [ocfps] NumCopies not found in PS" >&2
47+ $GREP ' %%Pages:' " $ps_tmp " >&2 || echo " [ocfps] %%Pages not found" >&2
48+ cp " $ps_tmp " /tmp/ocfps-debug.ps 2> /dev/null || true
49+
50+ if [ " $copies " -eq 1 ]; then
51+ ps_copies=$( $GREP -om1 ' NumCopies [0-9]*' " $ps_tmp " | $AWK ' {print $2}' ) || true
52+ if [ -n " $ps_copies " ] && [ " $ps_copies " -gt 1 ] 2> /dev/null; then
53+ copies=" $ps_copies "
54+ fi
55+ fi
56+
57+ echo " [ocfps] final copies=$copies " >&2
58+ $PSTOPS " $1 " " $2 " " $3 " " $copies " " $5 " " $ps_tmp "
2659fi
0 commit comments