Skip to content

Commit f095308

Browse files
committed
fix copies
1 parent 7ec5d25 commit f095308

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

modules/printhost/cups.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ let
5454
ocfpsFilter = pkgs.replaceVars ./scripts/ocfps {
5555
pdftops = "${pkgs.poppler-utils}/bin/pdftops";
5656
pstops = "${pkgs.cups}/lib/cups/filter/pstops";
57+
grep = "${pkgs.gnugrep}/bin/grep";
58+
awk = "${pkgs.gawk}/bin/awk";
59+
mktemp = "${pkgs.coreutils}/bin/mktemp";
60+
rm = "${pkgs.coreutils}/bin/rm";
5761
};
5862

5963
# Shell wrapper so the filter runs under the Nix-store bash rather than

modules/printhost/scripts/ocfps

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,47 @@ set -euo pipefail
1313

1414
PDFTOPS=@pdftops@
1515
PSTOPS=@pstops@
16+
GREP=@grep@
17+
AWK=@awk@
18+
MKTEMP=@mktemp@
19+
RM=@rm@
1620

1721
if [ "$#" -ne 5 ] && [ "$#" -ne 6 ]; then
1822
echo "Usage: $0 job-id user title copies options [file]"
1923
exit
2024
fi
2125

26+
copies="$4"
27+
2228
if [ "$#" -eq 5 ]; then
2329
$PDFTOPS - - | $PSTOPS "$@"
2430
else
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"
2659
fi

0 commit comments

Comments
 (0)