-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
paper_store_dense.sh
executable file
·173 lines (147 loc) · 5.74 KB
/
paper_store_dense.sh
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env bash
# MIT License
#
# Copyright (c) 2018 Maxim Biro <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
set -euo pipefail
INPUT="$1"
if [ -z "$INPUT" ]; then
echo "Error: No input file was specified. Please specify an input file as the first argument."
exit 1
fi
if [ ! -f "$INPUT" ]; then
echo "Error: The input file \"$INPUT\" doesn't exist."
exit 1
fi
if ! command -v pdflatex >/dev/null 2>&1; then
echo "Error: Program \"pdflatex\" is not installed (Debian/Ubuntu: texlive-latex-base)."
exit 1
fi
if ! command -v pdfunite >/dev/null 2>&1; then
echo "Error: Program \"pdfunite\" is not installed (Debian/Ubuntu: poppler-utils)."
exit 1
fi
if ! command -v qrencode >/dev/null 2>&1; then
echo "Error: Program \"qrencode\" is not installed (Debian/Ubuntu: qrencode)."
exit 1
fi
if ! command -v zbarimg >/dev/null 2>&1; then
echo "Error: Program \"zbarimg\" is not installed (Debian/Ubuntu: zbar-tools)."
exit 1
fi
TMP_DIR="$(mktemp -d)"
echo "Taking base64 of \"$INPUT\" file"
base64 "$INPUT" > "${TMP_DIR}/full_base64.txt"
OLD_PWD="${PWD}"
cd "$TMP_DIR"
cat <<EOF > page.tex.in
\documentclass[12pt,@@papersize@@]{article}
\usepackage[left=1cm,right=1cm,top=2cm,bottom=2cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{fancyhdr}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\chead{\fontfamily{pcr}\selectfont \detokenize{@@header@@}}
\cfoot{\fontfamily{pcr}\selectfont \detokenize{@@footer@@}}
\begin{document}
\thispagestyle{fancy}
\fontfamily{pcr}\selectfont
\begin{center}
\begin{minipage}{0.49\linewidth}
\includegraphics[width=\linewidth]{qr1.png}
\begin{center}QR1\end{center}
\end{minipage}
\hfill
\begin{minipage}{0.49\linewidth}
\IfFileExists{qr2.png}{\includegraphics[width=\linewidth]{qr2.png}\begin{center}QR2\end{center}}{}
\end{minipage}
\end{center}
\vfill
\begin{center}
\begin{minipage}{0.49\linewidth}
\IfFileExists{qr3.png}{\includegraphics[width=\linewidth]{qr3.png}\begin{center}QR3\end{center}}{}
\end{minipage}
\hfill
\begin{minipage}{0.49\linewidth}
\IfFileExists{qr4.png}{\includegraphics[width=\linewidth]{qr4.png}\begin{center}QR4\end{center}}{}
\end{minipage}
\end{center}
\end{document}
EOF
INPUT_BASENAME="$(basename "$INPUT")"
# You can store only so much data in a QR code and some QR readers
# have trouble with QR codes containing too much data. 12 lines of
# base64 works fine with most readers though, so we split the file
# into QR codes containing 12 line pieces.
echo "Splitting the base64 file into 12-line files"
split -l 12 -a 4 -d --additional-suffix=.txt full_base64.txt
total=0
for f in x*; do
total=$((total + 1))
done
echo "Split in $total of 12-lines files"
i=1
file_num=1
page=1
total_pages=$((total / 4))
if [[ $((total % 4)) != 0 ]]; then
total_pages=$((total_pages + 1))
fi
for f in x*; do
echo "Creating QR code image for file #$i"
head -c -1 "$f" | qrencode -o "qr-$f.png"
cp "qr-$f.png" qr$file_num.png
file_num=$((file_num + 1))
if [[ $file_num == 5 ]]; then
echo "Creating a pdf page with QR code images"
sed "s/@@papersize@@/${PAPER_STORE_PAPER_SIZE:-a4paper}/g" page.tex.in | sed "s/@@header@@/$INPUT_BASENAME/g" | sed "s#@@footer@@#Page $page/$total_pages#g" > page.tex
pdflatex page.tex >/dev/null 2>&1
cp page.pdf "page-$(printf "%04d" $page).pdf"
rm -f qr1.png qr2.png qr3.png qr4.png
file_num=1
page=$((page + 1))
fi
i=$((i + 1))
done
if [[ $(((i-1) % 4)) != 0 ]]; then
echo "Creating a pdf page with QR code images"
sed "s/@@papersize@@/${PAPER_STORE_PAPER_SIZE:-a4paper}/g" page.tex.in | sed "s/@@header@@/$INPUT_BASENAME/g" | sed "s#@@footer@@#Page $page/$total_pages#g" > page.tex
pdflatex page.tex >/dev/null 2>&1
cp page.pdf "page-$(printf "%04d" $page).pdf"
rm -f qr1.png qr2.png qr3.png qr4.png
fi
echo "Combining all the pages into a single pdf"
pdfunite $(find . -name 'page-*.pdf' | sort) pages.pdf
echo "Using a QR code reader on the pdf file to make sure we get back the initial base64 file correctly"
zbarimg --raw -Sdisable -Sqrcode.enable pages.pdf > read_base64.txt
# We sort them because we have multiple QR codes on a page and we can't
# control the order zbarimg scans them in
sort read_base64.txt > read_base64_sorted.txt
sort full_base64.txt > full_base64_sorted.txt
if ! diff -u read_base64_sorted.txt full_base64_sorted.txt; then
echo "Error: Weren't able to reconstruct original file when scanning the created pdf with a QR scanner."
rm -rf "${PWD}"
exit 1
fi
echo "Success: the read QR codes matched the initial base64 file"
echo "To reconstruct the file, scan the QR codes, concatenate the results into a single file and decode it as base64"
mv pages.pdf "${OLD_PWD}/${INPUT_BASENAME}.pdf"
rm -rf "${PWD}"