forked from jbenet/compress-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress-pdf
executable file
·64 lines (52 loc) · 1.48 KB
/
compress-pdf
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
#!/bin/sh
USAGE="Usage: compress-pdf [<in-pdf>] [<out-pdf>]
Compress PDF files using gs and compress."
usage() {
echo "$USAGE"
exit 0
}
die() {
echo >&2 "error: $@"
exit 1
}
# help option
case "$1" in
-h|--h|--help) usage ;;
esac
# check for dependencies
type gs &>/dev/null || die "dependency not found: gs
It is contained in the package: ghostscript
One of these may work for you:
brew install ghostscript
sudo apt-get install ghostscript"
# arguments
stream=0
infile=$1
outfile=$2
# are we streaming? if so use temp files.
if [ $# -eq 0 ]; then
date=$(date +"%Y-%m-%dT%H:%M:%SZ")
infile=$(mktemp "/tmp/compress-pdf.in.$date.XXXXXX.pdf")
outfile=$(mktemp "/tmp/compress-pdf.out.$date.XXXXXX.pdf")
stream=1
echo >&2 "Awaiting pdf from stdin (stream mode)..."
echo >&2 "If you meant to compress a file, use: $0 <file>"
cat - >"$infile"
fi
# only one filename provided, default output filename.
if [ $# -eq 1 ]; then
outfile=$(echo "$infile" | sed 's/\(.*\.\)\([^.]*\)/\1compressed.\2/')
fi
# if file does not exist, show usage.
if [ ! -f "$infile" ]; then
die "'$infile' does not exist or is not a pdf file"
fi
# actually do the compression now \o/
echo >&2 "compressing $infile to $outfile..."
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$outfile" "$infile" >/dev/null
# if streaming, output the outfile, then remove the tmp files
if [ $stream -eq 1 ]; then
cat "$outfile"
rm "$infile"
rm "$outfile"
fi