-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_pdf.sh
executable file
·65 lines (43 loc) · 1.7 KB
/
process_pdf.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
#!/bin/sh
echo "Running pdf2epubEX on $1..."
epuboutputfilename=$(basename "$1" .pdf)-150dpi-jpg.epub
# Patch out weird sed bug when modifying CSS
sh -c \
"sed -i \"s#sed -i 's/;unicode-bidi:bidi-override//g' base.min.css#cp base.min.css /tmp/base.min.css.bk \&\& sed -i -e 's/;unicode-bidi:bidi-override//g' /tmp/base.min.css.bk \&\& cat /tmp/base.min.css.bk >base.min.css || echo failed sed here#\" \"/usr/bin/pdf2epubEX\""
# Patch out DPI and format prompts
sed -ri '/echo "(======================|Caution:|- if you chose |If you want)/d; /read -p "Format of the images/d; /if \[ "\$imgformat" != "svg" \]/{N;N;d;}' /usr/bin/pdf2epubEX
pdf2epubEX $1
echo "Preparing to modify EPUB..."
mkdir -p bookmod
cd bookmod
mv ../$epuboutputfilename .
unzip $epuboutputfilename >/dev/null
rm $epuboutputfilename
echo "Extracting ppm images..."
mkdir -p ppm-out
pdftoppm -verbose ../$1 ppm-out
mv ppm-out-* ppm-out/
mkdir -p jpegimgs
echo "Converting ppm images to jpeg..."
for i in $(seq 1 `ls ppm-out | wc -l`); do
page=$(printf "%06d" $i)
# Define the input file names
ppm="ppm-out/ppm-out-$page.ppm"
# Define the output file name
jpeg_file="jpegimgs/pg$i.jpeg"
# Execute ImageMagick convert command
convert $ppm $jpeg_file
done
echo "Converting cover image to png..."
convert jpegimgs/pg1.jpeg OEBPS/cover.png
rm -rf ppm-out
echo "Copying jpeg images to OEBPS..."
cat OEBPS/content.opf
for img in jpegimgs/*.jpeg; do
cp $img OEBPS/bg`printf "%x\n" $(echo $img | sed "s/jpegimgs\/pg//; s/.jpeg//")`.jpg
done
echo "Zipping up..."
rm -rf jpegimgs
zip -0Xq ./test.epub mimetype && zip -Xr9Dq test.epub * -x mimetype -x ./test.epub && mv test.epub ../$epuboutputfilename
cd ..
rm -rf bookmod