-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress_pdf
executable file
·81 lines (68 loc) · 2.31 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash -e
# Syntax:
# compress_pdf [options] inFile [outFile]
#
# Options:
# See help
# compress_pdf --help
#
# Requires:
# gs
#
# History:
# Apr-2014 Yaswant Pradhan v1.0
usage(){
cat << EOF
NAME
${0##*/}, a gs wrapper to compress PDF files by controlling image
resolutions of the original PDF.
SYNOPSIS
${0##*/} [OPTION] inFile [outFile]
DESCRIPTION
Options:
-h -? --help show this message
-d --dpi image resolution required in the output pdf
-s --size desired output paper size (def: a4)
Paper sizes known to gs are available at
http://ghostscript.com/doc/current/Use.htm#Known_paper_sizes
inFile input pdf file
outFile optional output filename base (def: inFile-{DPI}.pdf)
AUTHOR
Written by Yaswant Pradhan.
EOF
}
# Check gs is avialable on the system
which gs &> /dev/null
[[ $? -ne 0 ]] && echo -e "** ERROR: gs does not exist in path.\n" && exit 1
# -----------------------------------------------------------------------------
# Default settings:
# -----------------------------------------------------------------------------
DPI=300 # Default Image resolution
PAPER=a4 # Default Paper size
# -----------------------------------------------------------------------------
# Parse arguments:
# -----------------------------------------------------------------------------
while [[ $1 == -* ]]; do
case $1 in
-h|--help|-\?) usage; exit 0;;
-d|--dpi) DPI=$2; shift 2;;
-s|--size) PAPER=$2; shift 2;;
--) shift; break;;
-*) echo "invalid option: $1" 1>&2; usage; exit 1;;
esac
done
if [[ $# -lt 1 ]]; then usage; exit 0; fi
inFile=$1 # Input File
[ ! -f "$inFile" ] && echo -e "** ERROR: '$inFile' does not exist.\n" && exit 1
outFile="${2:-$(basename $inFile .pdf)-${DPI}dpi.pdf}"
echo -e "Compressing to file: $outFile ... \c"
# -----------------------------------------------------------------------------
# Shrink PDF
# -----------------------------------------------------------------------------
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
-dDownsampleColorImages=true \
-dColorImageResolution="$DPI" \
-dNOPAUSE -dQUIET -dBATCH -sPAPERSIZE="$PAPER" \
-sOutputFile="$outFile" "$inFile"
st=$?
[[ "$st" -eq 0 ]] && echo -e "done." || exit "$st"