-
Notifications
You must be signed in to change notification settings - Fork 5
/
djvu2pdf
executable file
·214 lines (167 loc) · 5.35 KB
/
djvu2pdf
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
#!/bin/sh
# (c) Christoph Sieghart <[email protected]> 2007 - 2011
# Source: https://0x2a.at/site/projects/djvu2pdf/
# djvu2pdf
# Copyright: 2007-2009 Christoph Sieghart <[email protected]>
# The home page of djvu2pdf is at:
# http://www.0x2a.at/s/projects/djvu2pdf
# The entire code base may be distributed under the terms of the GNU General
# Public License (GPL), which appears immediately below. Alternatively, all
# of the source code as any code derived from that code may instead be
# distributed under the GNU Lesser General Public License (LGPL), at the
# choice of the distributor. The complete text of the LGPL appears at the
# bottom of this file.
# See /usr/share/common-licenses/(GPL|LGPL)
VERSION=0.9.2
# Flags for commandline options
S_FLAG=0
C_FLAG=0
trap 'if [ $S_FLAG -eq 1 ]; then clean_temp_dir; clean_cursor; fi' 0 1 2
#*****************************************************************************
# - Functions
#*****************************************************************************
check_print_error() {
if [ $? -ne 0 ]
then
echo "Error: $0: $1"
exit 1
fi
}
print_version() {
echo "djvu2pdf $VERSION - Christoph Sieghart <[email protected]>"
}
print_help() {
print_version
echo -e "\n Usage: ./djvu2pdf filename.djvu [filename.djvu] ...\n"
echo " -h Prints this help"
echo " -v Prints the version number"
echo " -s Show status messages (A little bit slower - every page"
echo " gets dumped on its own and later recombined"
echo -e " -c Don't use terminal escape sequences to move cursor \n"
exit 0
}
print_quiet() {
if [ $S_FLAG -eq 1 ]; then
echo -en $1
fi
}
move_cursor() {
if [ $S_FLAG -eq 1 ]; then
if [ $C_FLAG -eq 1 ]; then
echo -ne "\n"
return
else
# cursor magic
echo -en "\033[35D"
fi
fi
}
clean_cursor() {
# if the cursor 'magic' messes anything up
if [ $S_FLAG -eq 1 ]; then
if [ $C_FLAG -eq 0 ]; then
tput sgr0
fi
fi
}
clean_temp_files() {
rm -rf "$TEMP"/* >/dev/null 2>&1
}
clean_temp_dir() {
rm -rf "$TEMP" >/dev/null 2>&1
}
#*****************************************************************************
# - Programm checks
#*****************************************************************************
# MacOS and BSD compatibility
which seq >/dev/null 2>&1
if [ $? -eq 0 ]; then
SEQ=seq
else
SEQ=jot
fi
for i in "ddjvu djvulibre" "gs ghostscript"; do
BINARY=$(echo $i | awk '{print $1'})
PACKAGE=$(echo $i | awk '{print $2'})
which $BINARY >/dev/null 2>&1
check_print_error "$BINARY not found. Install $PACKAGE."
done
#*****************************************************************************
# - Commandline options
#*****************************************************************************
while getopts hvcs opt
do
case "$opt" in
v) print_version
exit 0;;
h) print_help;;
c) C_FLAG=1;;
s) S_FLAG=1;;
\?) exit 1;;
esac
done
shift `expr $OPTIND - 1`
if [ $C_FLAG -eq 1 ]; then
if [ $S_FLAG -eq 0 ]; then
echo -e "Error: $0: The option -c only makes sense with -s"
exit 1
fi
fi
if [ -z "$1" ]; then
echo -e "Error: $0: No file specified for conversion"
exit 1
fi
#*****************************************************************************
# - Main loop
#*****************************************************************************
while [ $# -gt 0 ]; do
if [ ! -f "$1" ]; then
echo -e "Error: $0: File '$1' not found"
exit 1
fi
FILENAME=$1
FILEBASE=`basename "$FILENAME" .djvu`
OUTPUTFILE="${FILEBASE}.pdf"
if [ $S_FLAG -eq 0 ]; then
ddjvu -format=pdf "${FILENAME}" "${OUTPUTFILE}" 2> /dev/null
else
#*****************************************************************************
# - Temporary Files
#*****************************************************************************
TEMP="${TMPDIR:=/tmp}/djvu2pdf.$$"
mkdir $TEMP
check_print_error "Creating temporary directory failed!"
# Child processes should use our temporary directory
TMPDIR=$TEMP
PAGES=`djvudump "$FILENAME" | grep pages | awk '{print $8;}'`
ZEROCOUNT=$(expr `echo $PAGES | wc -m` - 1)
COUNT=1
TENS=1
print_quiet "Converting $FILENAME to $OUTPUTFILE\n"
#
# We dump every page and print the status message
#
for COUNT in `$SEQ 1 $PAGES`;
do
if [ `expr $COUNT / $TENS` -gt 0 ]
then
TENS=`expr $TENS \* 10`
ZEROCOUNT=`expr $ZEROCOUNT - 1`
ZEROS=$(for i in `$SEQ 1 $ZEROCOUNT`; do echo -n 0; done)
fi
ddjvu -format=pdf -page $COUNT "$FILENAME" "$TEMP/$FILEBASE.${ZEROS}$COUNT.pdf" 2> /dev/null
print_quiet "Page $COUNT/$PAGES dumped"
move_cursor
done
clean_cursor
#
# The pages get combined into one big happy .pdf
#
print_quiet "\nDumping finished - writing $OUTPUTFILE\n"
gs -dSAFER -dQUIET -dBATCH -sDEVICE=pdfwrite -sOutputFile="$OUTPUTFILE" -DNOPAUSE "$TEMP/$FILEBASE".*.pdf
check_print_error "Error in creating pdf file"
clean_temp_files
fi
shift
done
exit 0