修訂 | bb29204c46d1fb42fec5e0411c3742fd30d0b15f (tree) |
---|---|
時間 | 2019-04-15 17:23:34 |
作者 | Lorenzo Isella <lorenzo.isella@gmai...> |
Commiter | Lorenzo Isella |
A script to shrink a pdf file.
@@ -0,0 +1,99 @@ | ||
1 | +#!/bin/sh | |
2 | + | |
3 | +# http://www.alfredklomp.com/programming/shrinkpdf | |
4 | +# Licensed under the 3-clause BSD license: | |
5 | +# | |
6 | +# Copyright (c) 2014, Alfred Klomp | |
7 | +# All rights reserved. | |
8 | +# | |
9 | +# Redistribution and use in source and binary forms, with or without | |
10 | +# modification, are permitted provided that the following conditions are met: | |
11 | +# 1. Redistributions of source code must retain the above copyright notice, | |
12 | +# this list of conditions and the following disclaimer. | |
13 | +# 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | +# this list of conditions and the following disclaimer in the documentation | |
15 | +# and/or other materials provided with the distribution. | |
16 | +# 3. Neither the name of the copyright holder nor the names of its contributors | |
17 | +# may be used to endorse or promote products derived from this software | |
18 | +# without specific prior written permission. | |
19 | +# | |
20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
21 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
24 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
25 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
26 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
27 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
28 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
29 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
30 | +# POSSIBILITY OF SUCH DAMAGE. | |
31 | + | |
32 | + | |
33 | +shrink () | |
34 | +{ | |
35 | + gs \ | |
36 | + -q -dNOPAUSE -dBATCH -dSAFER \ | |
37 | + -sDEVICE=pdfwrite \ | |
38 | + -dCompatibilityLevel=1.3 \ | |
39 | + -dPDFSETTINGS=/screen \ | |
40 | + -dEmbedAllFonts=true \ | |
41 | + -dSubsetFonts=true \ | |
42 | + -dAutoRotatePages=/None \ | |
43 | + -dColorImageDownsampleType=/Bicubic \ | |
44 | + -dColorImageResolution=$3 \ | |
45 | + -dGrayImageDownsampleType=/Bicubic \ | |
46 | + -dGrayImageResolution=$3 \ | |
47 | + -dMonoImageDownsampleType=/Bicubic \ | |
48 | + -dMonoImageResolution=$3 \ | |
49 | + -sOutputFile="$2" \ | |
50 | + "$1" | |
51 | +} | |
52 | + | |
53 | +check_smaller () | |
54 | +{ | |
55 | + # If $1 and $2 are regular files, we can compare file sizes to | |
56 | + # see if we succeeded in shrinking. If not, we copy $1 over $2: | |
57 | + if [ ! -f "$1" -o ! -f "$2" ]; then | |
58 | + return 0; | |
59 | + fi | |
60 | + ISIZE="$(echo $(wc -c "$1") | cut -f1 -d\ )" | |
61 | + OSIZE="$(echo $(wc -c "$2") | cut -f1 -d\ )" | |
62 | + if [ "$ISIZE" -lt "$OSIZE" ]; then | |
63 | + echo "Input smaller than output, doing straight copy" >&2 | |
64 | + cp "$1" "$2" | |
65 | + fi | |
66 | +} | |
67 | + | |
68 | +usage () | |
69 | +{ | |
70 | + echo "Reduces PDF filesize by lossy recompressing with Ghostscript." | |
71 | + echo "Not guaranteed to succeed, but usually works." | |
72 | + echo " Usage: $1 infile [outfile] [resolution_in_dpi]" | |
73 | +} | |
74 | + | |
75 | +IFILE="$1" | |
76 | + | |
77 | +# Need an input file: | |
78 | +if [ -z "$IFILE" ]; then | |
79 | + usage "$0" | |
80 | + exit 1 | |
81 | +fi | |
82 | + | |
83 | +# Output filename defaults to "-" (stdout) unless given: | |
84 | +if [ ! -z "$2" ]; then | |
85 | + OFILE="$2" | |
86 | +else | |
87 | + OFILE="-" | |
88 | +fi | |
89 | + | |
90 | +# Output resolution defaults to 72 unless given: | |
91 | +if [ ! -z "$3" ]; then | |
92 | + res="$3" | |
93 | +else | |
94 | + res="72" | |
95 | +fi | |
96 | + | |
97 | +shrink "$IFILE" "$OFILE" "$res" || exit $? | |
98 | + | |
99 | +check_smaller "$IFILE" "$OFILE" |