-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf2png
63 lines (54 loc) · 1.35 KB
/
pdf2png
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
#!/bin/sh
force=0
trim=1
while getopts ":ft" opt; do
case $opt in
f)
force=1
;;
t)
trim=0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
shift $((OPTIND -1))
echo "$@"
for pdf in "$@"
do
dpi=600
echo "Converting $pdf with DPI = ${dpi}"
png_path="${pdf%%.pdf}.png"
if [ -f "$png_path" ] && [ $force -eq 0 ]; then
echo "File $png_path already exists. Use -f to overwrite."
else
if [ $force -eq 1 ] && [ -f "$png_path" ]; then
echo "Overwriting $png_path due to -f flag."
fi
gs \
-o "$png_path" \
-q \
-sDEVICE=png16m \
-r1200 \
"$pdf" \
-dGraphicsAlphaBits=20 \
-dTextAlphaBits=20
if [ $? -eq 0 ]; then
echo "Conversion successful: $png_path"
if [ $trim -eq 1 ]; then
# Remove whitespace from the converted PNG image
convert "$png_path" -trim "$png_path"
if [ $? -eq 0 ]; then
echo "Whitespace removal successful: $png_path"
else
echo "Whitespace removal failed for $png_path"
fi
fi
else
echo "Conversion failed for $pdf"
fi
fi
done