-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfconv
More file actions
executable file
·298 lines (275 loc) · 7.53 KB
/
pdfconv
File metadata and controls
executable file
·298 lines (275 loc) · 7.53 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/bin/bash
# Function & Usage
# - See help text
#
# Notes
# -
#
# TODO
# -
#
# (c) 2021 Robert Lange (sd2k9@sethdepot.org)
# GNU General Public Licence applies
# ------------------------------------------------------------------------------
# *** Settings ***
# Treat uninitialised variable as error and exit script
set -u
# Test mode: True when defined (only echo commands)
declare TESTMODE
# Program name
readonly PRGNAME=${0##*/}
# Mode, selected from command line
declare MODE=""
# File names to process, remaining command line after mode and options
declare -a FILELIST
# Input file to process
declare INFILE
# ------------------------------------------------------------------------------
# *** Functions ***
# Load shared functions
readonly EXEC_DO_NO_ABORT_ON_ERROR=false
source ${BASH_SOURCE[0]%/*}/bash-functions
# *** Functions ***
function help_text() {
cat<<EOF
Wrapper for pdf conversion tools, to expose a commong interface.
Usage: $PRGNAME [OPTS] mode inputfile(s)
* -h|--help: This screen"
* -V|--version: Print program version and exit"
* -t|--test|--dry-run: Do not execute commands, only echo"
* mode: Operation mode, see below
* inputfile: File to processs
Operation Modes
jpg, jpeg: Convert PDF to JPEG images
images: Extract images from PDF
Requires package poppler-utils
pdf: Combine image, txt, odt and PDF file(s) to PDF
txt, odt: Requires libreoffice
Note: Intermediate files (from mixed conversion)
must be removed manually
txt, text: Extract text from PDF
EOF
}
# Print Versionstring
function versionstring () {
declare -r VER=1.1.0
cat<<EOF
$PRGNAME version $VER
(c) by Robert Lange (sd2k9@sethdepot.org)
Licensed under the GNU General Public License
EOF
}
# ------------------------------------------------------------------------------
# *** Command line parsing
get_options() {
local opt
while [ -n "$*" ] ; do
opt="$1"
shift
case $opt in
-t|--test|--dry-run)
# Test Mode
TESTMODE=1
;;
-V|--version)
# Version output
versionstring
exit 0
;;
-h|--help|'-?')
# Hilfetext
versionstring
help_text
exit 0
;;
jpg|jpeg)
if [[ -n $MODE ]]; then
echo "Multiple mode statements! See help text for usage"
exit 1
fi
MODE="$opt"
;;
pdf)
if [[ -n $MODE ]]; then
echo "Multiple mode statements! See help text for usage"
exit 1
fi
MODE="$opt"
;;
txt|text)
if [[ -n $MODE ]]; then
echo "Multiple mode statements! See help text for usage"
exit 1
fi
MODE="$opt"
;;
images)
if [[ -n $MODE ]]; then
echo "Multiple mode statements! See help text for usage"
exit 1
fi
MODE="$opt"
;;
*)
if [[ -n $MODE ]]; then
# Arguments after mode are expected to be file names
if [[ ! -f "$opt" ]]; then
echo "Error: File \"$opt\" not existing!"
exit 1
fi
# echo "add $opt"
FILELIST+=("$opt")
else
# Unknown Argument
echo "Unknown argument \"$opt\"! See help text for usage"
exit 1
fi
;;
esac
done
}
# *** Call Command Line Parsing and Program execution
# When no options supplied then call help
if [ -z "${1:-}" ]; then
get_options "--help"
else
get_options "$@"
fi
# ------------------------------------------------------------------------------
# *** Now execute actions, or done above
case $MODE in
jpg|jpeg)
for (( i=0; i<${#FILELIST[@]}; i++ ));
do
INFILE="${FILELIST[$i]}"
echo "Converting input PDF file \"$INFILE\" to jpg image file(s)"
exec_tool pdftoppm -jpeg "\"$INFILE\"" "\"${INFILE%.*}\""
echo
done
;;
pdf)
# First check: Only Images? Only PDF files? Or mixed?
declare filetypes
# Starting point is file extension of first file
case ${FILELIST[0]##*.} in
pdf)
filetypes="pdf"
;;
txt|odt)
filetypes="mix"
;;
*)
# Images
filetypes="img"
;;
esac
# Now check other files
for (( i=1; i<${#FILELIST[@]}; i++ ));
do
case ${FILELIST[$i]##*.} in
pdf)
if [[ $filetypes != "pdf" ]]; then
# Reset and done
filetypes="mix"
break
fi
;;
txt|odt)
# Reset and done
filetypes="mix"
break
;;
*)
# Images
if [[ $filetypes != "img" ]]; then
# Reset and done
filetypes="mix"
break
fi
;;
esac
done
# For mixed file list preconversion is needed
if [[ $filetypes == "mix" ]]; then
# Resulting mode is PDF
filetypes=pdf
# Convert Non-PDF files to PDF
for (( i=0; i<${#FILELIST[@]}; i++ ));
do
# Input file name
INFILE="${FILELIST[$i]}"
# Replace suffix with pdf
OUTFILE="${FILELIST[$i]%.*}.pdf"
# Decide by suffix
case ${FILELIST[$i]##*.} in
pdf)
# Do nothing
;;
txt|odt)
echo "Converting input file \"$INFILE\" to PDF file"
exec_tool libreoffice --convert-to pdf "\"$INFILE\""
# Update name in file array
FILELIST[$i]="$OUTFILE"
echo
;;
*)
# Image
# And convert
# Add "--colorspace RGB" to resolve error
# img2pdf ERROR:root:error: Cannot have Palette images with ICC profile
exec_tool img2pdf --output \"${OUTFILE}\" \"${INFILE}\"
# Update name in file array
FILELIST[$i]="$OUTFILE"
;;
esac
done
fi
# Now build the result file list
OUTFILE="${FILELIST[0]%.*}-output.pdf"
# Correctly quote input files
files=
for (( i=0; i<${#FILELIST[@]}; i++ ));
do
files="$files \"${FILELIST[$i]}\""
done
# Execute right command
case $filetypes in
pdf)
exec_tool pdftk $files cat output \"${OUTFILE}\"
;;
img)
exec_tool img2pdf --output \"${OUTFILE}\" $files
;;
*)
echo "Unknown file type \"$filetypes\"! Internal error!"
exit 1
;;
esac
;;
images)
for (( i=0; i<${#FILELIST[@]}; i++ ));
do
INFILE="${FILELIST[$i]}"
echo "Extract images from PDF file \"$INFILE\""
exec_tool pdfimages -all "\"$INFILE\"" "\"${INFILE%.*}-img\""
echo
done
;;
txt|text)
for (( i=0; i<${#FILELIST[@]}; i++ ));
do
INFILE="${FILELIST[$i]}"
echo "Converting input PDF file \"$INFILE\" to text file"
exec_tool pdftotext "\"$INFILE\"" "\"${INFILE%.*}.txt\""
echo
done
;;
*)
# No mode
echo "No mode selected! See help text for usage"
exit 1
;;
esac
# *** Done
echo
echo "Done"