-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcompress_pdf
More file actions
executable file
·43 lines (32 loc) · 884 Bytes
/
compress_pdf
File metadata and controls
executable file
·43 lines (32 loc) · 884 Bytes
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
#!/usr/bin/env bash
if [[ "$*" =~ -h || "$*" =~ --help ]]; then
echo "Usage: $(basename "$0") pdffile [level]"
echo "Compress a PDF file using ghostscript."
echo " level: 0 (default), 1 (printer), 2 (ebook), 3 (screen)"
exit
fi
declare -i level
level="$2"
level=${level:-1}
levels=(default printer ebook screen)
if [[ "$level" -gt 3 ]]; then
level=3
fi
settings=${levels[$level]}
file="$1"
if [[ ! -f "$file" ]]; then
echo "File not found: $file"
exit 1
fi
newfile="${file%.pdf}-compressed-$settings.pdf"
if [[ -f "$newfile" ]]; then
echo "File already exists: $newfile"
echo "remove? (y/n)"
read -r answer
if [[ "$answer" =~ ^[Yy] ]]; then
rm "$newfile"
else
exit 1
fi
fi
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/"$settings" -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$newfile" "$file"