-
Notifications
You must be signed in to change notification settings - Fork 2
/
mkpdf.sh
executable file
·76 lines (66 loc) · 1.64 KB
/
mkpdf.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
#!/bin/bash
#Prerequisites: base64, sed, convert (ImageMagick)
PNGHEAD="data:image\/png;base64,"
JPEGHEAD="data:image\/jpeg;base64,"
COUNTER=1
SKIPEXTRACT=0
# Get filename
if [ -z "$1" ]; then
echo "Usage: $0 input"
echo "Where \"input\" is the downloaded PDF page archive file name"
exit 1
fi
if [ ! -f "$1" ]; then
echo "Error: File $1 does not exist!"
exit 1
fi
echo "Processing file: $1"
# Base file name without extension/spaces
basename=${1/.imgpack/}
#basename=${basename// /_}
dirname="$basename"_pages
if [ -d "$dirname" ]; then
read -p "It seems \"${1}\" already has been extracted to \"${dirname}\". Use existing files? (y/n) " yn
case $yn in
[yY] ) SKIPEXTRACT=1
;;
[nN] ) SKIPEXTRACT=0
echo "Purging existing files."
rm -f "$dirname"/*.jpg
;;
* ) echo Invalid response
;;
esac
else
mkdir -p "$dirname"
fi
cd "$dirname"
if [ $SKIPEXTRACT == 0 ]; then
echo -n "Extracting pages: "
while read line
do
echo -n "$COUNTER, "
padcounter=$(printf "%04d" $COUNTER)
if echo "$line" | grep -q $PNGHEAD; then
imgdata=$(echo "$line" | sed -e "s/^$PNGHEAD//")
echo "$imgdata" | base64 -di - > $padcounter.png
convert $padcounter.png $padcounter.jpg
rm $padcounter.png
fi
if echo "$line" | grep -q $JPEGHEAD; then
imgdata=$(echo "$line" | sed -e "s/^$JPEGHEAD//")
echo "$imgdata" | base64 -di - > $padcounter.jpg
fi
let "COUNTER++"
done < ../"$1"
echo "done."
fi
# Use ImageMagick to make PDF if available
if command -v convert > /dev/null; then
echo -n "Generating PDF ..."
convert $(ls -1v) "../$basename.pdf"
echo " completed!"
else
echo "ImageMagick not installed, skipping PDF generation."
fi
cd ..