-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpdf2rmnotebook.sh
executable file
·274 lines (230 loc) · 6.37 KB
/
pdf2rmnotebook.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash -
#===============================================================================
#
# FILE: ./pdf2rmnotebook.sh
#
# USAGE: ./pdf2rmnotebook.sh [options] file.pdf [...]
#
# DESCRIPTION: Build multi page reMarkable Notebook from PDF Files
#
# OPTIONS: See Usage()
# REQUIREMENTS: drawj2d, pdfinfo
# BUGS: ---
# NOTES: ---
# AUTHOR: Alberto Varesio (JCN), jcn9000@gmail.com
# ORGANIZATION:
# CREATED: 05/26/2021 01:24:45 PM
# REVISION: 1.1 : Multi page PDF conversion
# 1.2.0 : Usage, Verbosity and Version options
# 1.3.0 : Image inclusion
# 2.2.0 : option to create reMarkable Notebook .rmn file
#===============================================================================
set -o nounset # Treat unset variables as an error
Version=2.2.0
NAME=$(basename $0 .sh)
TEMP=$(mktemp -d)
# === Functions ===
Usage() {
cat <<EOD
$NAME: $Version
Usage:
$NAME [options] file.pdf [...]
Create multi-page reMarkable Notebook file from PDF files
* Creates .zip files by default for use with rmapi
* Use -r option to create a reMarkable Notebook .rmn file for use with RCU
Options:
Switches (no arguments required):
-h Display this help and exit
-q Produce fewer messages to stdout
-r Create a reMarkable Notebook .rmn file (default: zip)
-v Produce more messages to stdout
-V Display version information and exit
With arguments:
-n NAME Set the rmn Notebook Display Name (default: Notebook-<yyyymmdd_hhmm.ss>)
Only used with -r option
-o FILE Set the output filename (default: Notebook-<yyyymmdd_hhmm.ss>.zip)
-s SCALE Set the scale value (default: 0.75) - 0.75 is a good value for A4/Letter PDFs
Example:
$NAME -n "My Notebook" -o mynotebook.zip -s 1.0 file.pdf
EOD
}
Cleanup() {
rm -rf ${TEMP}
}
# === Main ===
trap Cleanup EXIT
QVFLAG='-q'
VERBOSE=false
DEBUG=false
IMAGE=false
SCALE=.75
DEFAULT_OUTFILE="Notebook-$(date +%Y%m%d_%H%M.%S)"
OUTFILE=""
EXTENSION=".zip"
DISPLAY_NAME=$DEFAULT_OUTFILE
RMN=false
while getopts "dhin:qrvs:Vo:" opt
do
case $opt in
d)
# Fake some values
DEBUG=true
NAME=pdf2rmnotebook
;;
n)
DISPLAY_NAME=$OPTARG
;;
o)
OUTFILE=$OPTARG
;;
q)
QVFLAG='-q'
VERBOSE=false
;;
r)
RMN=true
EXTENSION=".rmn"
TARARGS="cf"
;;
h)
Usage
exit 0
;;
i)
IMAGE=true
;;
s) # Default is .75 so 1n A$ pdf scales to a rM page
SCALE=$OPTARG
;;
v)
QVFLAG='-v'
VERBOSE=true
;;
V)
echo $NAME: $Version
exit 0
;;
\?)
echo "Invalid option; -$OPTARG" >&2
Usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
Usage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [[ -z "$OUTFILE" ]]
then
OUTFILE=$DEFAULT_OUTFILE
fi
VARLIB=/var/lib/${NAME}
test -d ${VARLIB} || VARLIB=$(dirname $0)/var/lib/${NAME}
test -d ${VARLIB} || { echo "Error - Incorrect Installation: ${VARLIB} not available" ; exit 1 ; }
if [[ $# -le 0 ]]
then
Usage
exit 1
fi
# Prepare HCL file for image inclusion
cat > ${TEMP}/page.hcl <<EOF
pen black 0.1 solid
line 1 1 156 1
line 156 1 156 208
line 156 208 1 208
line 1 208 1 1
moveto 0 0
EOF
NB=${TEMP}/Notebook
mkdir ${NB}
_page=0
UUID_N=$(uuidgen) # UUID for Notebook
mkdir ${NB}/${UUID_N}
cp ${VARLIB}/UUID.pagedata ${NB}/${UUID_N}.pagedata
cp ${VARLIB}/UUID_HEAD.content ${NB}/${UUID_N}.content
for _P in "$@"
do
# test ${_page} -ne 0 && echo , >> ${NB}/${UUID_N}.content
$VERBOSE && echo Working on file: ${_P}
test -f "${_P}" || { echo "${_P}: No such file or directory." ; Usage ; exit 1 ; }
FILETYPE=$(file -b --mime-type "${_P}")
case $FILETYPE in
image/jpeg | image/png)
_NP=1
IMAGE=true
;;
application/pdf )
# Get Pages from file, loop over all of them
read x _NP <<< $(pdfinfo "${_P}" | grep Pages: )
;;
esac
_PP=1
while [[ ${_PP} -le ${_NP} ]]
do
test ${_page} -ne 0 && echo , >> ${NB}/${UUID_N}.content
cp ${TEMP}/page.hcl ${TEMP}/P_${_page}.hcl
case $FILETYPE in
image/jpeg | image/png )
echo "moveto 12 12" >> ${TEMP}/P_${_page}.hcl
echo "image {${_P}} 200 0 0 $SCALE " >> ${TEMP}/P_${_page}.hcl
;;
application/pdf )
# Scale image: Usually PDF is A4/Letter, rM is smaller
echo "image {${_P}} ${_PP} 0 0 $SCALE" >> ${TEMP}/P_${_page}.hcl
;;
esac
UUID_P=$(uuidgen) # Notebook Pages should be named using the UUID from .content file
# rmapi interface to reMarkable cloud renames pages from UUID to page number while up/downloading
# so we need to use the same convention in naming pages: 0.rm 1.rm ... instead of UUIDs
# which are used internally in the rM (see ~/.local/share/remarkable/xochitl/ )
# It is indeed easier to have page numbers instead of random UUIDs referenced elsewhere
drawj2d ${QVFLAG} -T rm -o ${NB}/${UUID_N}/${_page}.rm ${TEMP}/P_${_page}.hcl
cp ${VARLIB}/UUID_PAGE-metadata.json ${NB}/${UUID_N}/${_page}-metadata.json
echo -n \"${UUID_P}\" >> ${NB}/${UUID_N}.content
_PP=$(( $_PP + 1 ))
_page=$(( _page + 1 ))
done
done
cat ${VARLIB}/UUID_TAIL.content >> ${NB}/${UUID_N}.content
# sed in macOS need "backup file" argument
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s/%PAGENUM%/${_page}/" ${NB}/${UUID_N}.content
else
sed -i "s/%PAGENUM%/${_page}/" ${NB}/${UUID_N}.content
fi
#DEBUG cat ${NB}/${UUID_N}.content
(
cd ${NB}
if [ $RMN = true ]; then
if [ $QVFLAG = "-v" ]; then
TARARGS="${TARARGS}v"
fi
CMOTIME=$(date +%s000)
PAGE=1
TYPE="DocumentType"
if [ -z "$DISPLAY_NAME" ]; then
DISPLAY_NAME=$DEFAULT_OUTFILE
fi
cat > "${NB}/${UUID_N}.metadata" <<EOF
{
"createdTime": ${CMOTIME},
"lastModified": ${CMOTIME},
"lastOpened": ${CMOTIME},
"lastOpenedPage": ${PAGE},
"parent": "",
"pinned": false,
"type": "${TYPE}",
"visibleName": "${DISPLAY_NAME}"
}
EOF
tar $TARARGS ${TEMP}/Notebook$EXTENSION ${UUID_N}.* ${UUID_N}/*
else
zip ${QVFLAG} ${TEMP}/Notebook$EXTENSION ${UUID_N}.* ${UUID_N}/*
fi
)
cp ${TEMP}/Notebook$EXTENSION ${OUTFILE}$EXTENSION
echo Output written to $OUTFILE$EXTENSION
#DEBUG find ${TEMP} -ls