aboutsummaryrefslogtreecommitdiffstats
path: root/cli/pdf-compress.sh
blob: 9f68c4cf76a758f00d149e53b1ef5eaaab43bbaa (plain)
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/sh
#
# Shrink the size of PDF files by adjust its quality using `gs` (GhostScript).
#
# Aaron LI
# 2013/09/18
#

case "$1" in
    -[hH]*|--[hH]*)
        printf "usage:\n"
        printf "    `basename $0` in=<input.pdf> out=<output.pdf> quality=<screen|ebook|printer|prepress> imgdpi=<img_dpi>\n"
        exit 1
        ;;
esac

getopt_keyval() {
    until [ -z "$1" ]
    do
        key=${1%%=*}                    # extract key
        val=${1#*=}                     # extract value
        keyval="${key}=\"${val}\""
        echo "## getopt: eval '${keyval}'"
        eval ${keyval}
        shift                           # shift, process next one
    done
}
getopt_keyval "$@"

if [ -z "${in}" ] || [ -z "${out}" ]; then
    printf "Error: 'in' or 'out' not specified\n"
    exit 2
fi
quality=${quality:-ebook}
imgdpi=${imgdpi:-120}

printf "# in: ${in}
# out: ${out}
# quality: ${quality}
# imgdpi: ${imgdpi}\n"

gs  -dNOPAUSE -dBATCH -dSAFER \
    -sDEVICE=pdfwrite \
    -dCompatibilityLevel=1.4 \
    -dPDFSETTINGS="/${quality}" \
    -dEmbedAllFonts=true \
    -dSubsetFonts=true \
    -dColorImageDownsampleType=/Bicubic \
    -dColorImageResolution=${imgdpi} \
    -dGrayImageDownsampleType=/Bicubic \
    -dGrayImageResolution=${imgdpi} \
    -dMonoImageDownsampleType=/Bicubic \
    -dMonoImageResolution=${imgdpi} \
    -sOutputFile=${out} \
    ${in}