-
Notifications
You must be signed in to change notification settings - Fork 39
/
pdf-extract.sh
executable file
·56 lines (49 loc) · 1.21 KB
/
pdf-extract.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
#!/bin/bash
# exit on error
set -e
any2img() {
convert -density 150 "$1" -quality 100 "$2" &>/dev/null
}
pdf2img() {
echo "Splitting single pdf file by pages (tiff)"
stapler burst "$1"
base=${1%.*}
for i in "${base}_"*.pdf
do
out=pg${i#"$base"} # will result in 'pg_123.pdf'
out=${out%.*}.tiff # replace extension
echo "$out"
# any2img "$i" "$out"
convert -density 300 "$i" -compress lzw "$out"
rm -f "$i"
done
}
djvu2img() {
echo "Splitting single djvu file by pages (tiff)"
pages=`djvused -e "n" "$1"`
for (( i=1; i<=$pages; i++ ))
do
num=$(printf "%03d" "$i")
out="pg_$num.tiff"
echo " $out"
ddjvu -page=$i -format=tiff "$1" "$out"
done
}
path=$(realpath "$1")
filename=$(basename "$path")
extension=${filename##*.}
basename=${filename%.*} # filename without extension
# create directory for extracted images
mkdir -p "$basename"
cp "$path" "$basename"
cd "$basename"
if [[ "$extension" == "pdf" ]]; then
pdf2img "$filename"
rm -f "$filename"
elif [[ "$extension" == "djvu" ]]; then
djvu2img "$filename"
rm -f "$filename"
else
echo "Supported file types: pdf, djvu"
exit 1
fi