-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprinter
executable file
·86 lines (72 loc) · 2.19 KB
/
printer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
# This quick script provides a way to print paper-efficient, double-sided
# booklets from PDFs on my cheap printer.
# Dependencies:
# lp, pdfjam, pdftk, mupdf, krop
set -euo pipefail
IFS=$'\n\t'
LP_ARGS=(\
-o "TonerSaveMode=OFF" \
-o "Resolution=600dpi" \
-o "PageSize=A4" \
-o "media=a4" \
-o "fit-to-page" \
)
# Exit if given printer is unknown. Check `lpstat -e` for available printers
(lpoptions -d ${PRINTER:-undefined} 2>&1 > /dev/null) || \
(echo "Printer \"${PRINTER:-undefined}\" is unknown." 1>&2; exit 1)
# Make a temporary directory for this run, and make sure it gets deleted
TMP=$(mktemp -d "${TMPDIR:-/tmp/}$(basename $0).XXXXX")
function finalize {
rm -rf "${TMP}"
}
trap finalize EXIT
# Start processing
INPUT="$1"
OUTPUT="$INPUT"
# PDFs are cropped before use using a GUI, to get rid of the margins
INPUT="${OUTPUT}"
OUTPUT="$(mktemp -p "${TMP}" "XXX.pdf")"
krop --output "${OUTPUT}" "${INPUT}"
# This crops the whitespace away automatically
#echo "Cropping whitespace..." 1>&2
#INPUT="${OUTPUT}"
#OUTPUT="$(mktemp -p "${TMP}" "XXX.pdf")"
#pdfcrop \
# --hires \
# --noclip \
# --margins '17' \
# "${INPUT}" "${OUTPUT}"
# This scales it back to A4 size
#echo "Scaling back to A4 size..." 1>&2
#INPUT="${OUTPUT}"
#OUTPUT="$(mktemp -p "${TMP}" "XXX.pdf")"
#pdfjam \
# --paper 'a4paper' \
# --fitpaper 'true' \
# --outfile "${OUTPUT}" \
# -- "${INPUT}" -
# Puts it in booklet form
echo "Making booklet..." 1>&2
INPUT="${OUTPUT}"
OUTPUT="$(mktemp -p "${TMP}" "XXX.pdf")"
pdfjam \
--booklet 'true' \
--signature '4' \
--landscape \
--delta '2.2cm 0cm' \
--outfile "${OUTPUT}" \
-- "${INPUT}" -
echo "Showing preview..." 1>&2
mupdf "${OUTPUT}"
if read -p "Continue?"; then
# Print odd pages
ODD="$(mktemp -p "${TMP}" "XXX.odd.pdf")"
pdftk "${OUTPUT}" cat odd output "${ODD}"
lp -d "$PRINTER" "${LP_ARGS[@]}" "${ODD}"
read -p "Wait for printing to finish, flip and reinsert the pages, then press enter to continue."
# Print even pages
EVEN="$(mktemp -p "${TMP}" "XXX.even.pdf")"
pdftk "${OUTPUT}" cat even output "${EVEN}"
lp -d "$PRINTER" "${LP_ARGS[@]}" -o outputorder=reverse "${EVEN}"
fi