diff options
-rwxr-xr-x | .local/bin/shrinkpdf | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/.local/bin/shrinkpdf b/.local/bin/shrinkpdf new file mode 100755 index 0000000..b9d1f19 --- /dev/null +++ b/.local/bin/shrinkpdf | |||
@@ -0,0 +1,101 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | # http://www.alfredklomp.com/programming/shrinkpdf | ||
4 | # Licensed under the 3-clause BSD license: | ||
5 | # | ||
6 | # Copyright (c) 2014-2019, 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 | # Modified by Vivek Gite to suit my needs | ||
34 | # | ||
35 | shrink () | ||
36 | { | ||
37 | gs \ | ||
38 | -q -dNOPAUSE -dBATCH -dSAFER \ | ||
39 | -sDEVICE=pdfwrite \ | ||
40 | -dCompatibilityLevel=1.3 \ | ||
41 | -dPDFSETTINGS=/screen \ | ||
42 | -dEmbedAllFonts=true \ | ||
43 | -dSubsetFonts=true \ | ||
44 | -dAutoRotatePages=/None \ | ||
45 | -dColorImageDownsampleType=/Bicubic \ | ||
46 | -dColorImageResolution=$3 \ | ||
47 | -dGrayImageDownsampleType=/Bicubic \ | ||
48 | -dGrayImageResolution=$3 \ | ||
49 | -dMonoImageDownsampleType=/Subsample \ | ||
50 | -dMonoImageResolution=$3 \ | ||
51 | -sOutputFile="$2" \ | ||
52 | "$1" | ||
53 | } | ||
54 | |||
55 | check_smaller () | ||
56 | { | ||
57 | # If $1 and $2 are regular files, we can compare file sizes to | ||
58 | # see if we succeeded in shrinking. If not, we copy $1 over $2: | ||
59 | if [ ! -f "$1" -o ! -f "$2" ]; then | ||
60 | return 0; | ||
61 | fi | ||
62 | ISIZE="$(echo $(wc -c "$1") | cut -f1 -d\ )" | ||
63 | OSIZE="$(echo $(wc -c "$2") | cut -f1 -d\ )" | ||
64 | if [ "$ISIZE" -lt "$OSIZE" ]; then | ||
65 | echo "Input smaller than output, doing straight copy" >&2 | ||
66 | cp "$1" "$2" | ||
67 | fi | ||
68 | } | ||
69 | |||
70 | usage () | ||
71 | { | ||
72 | echo "Reduces PDF filesize by lossy recompressing with Ghostscript." | ||
73 | echo "Not guaranteed to succeed, but usually works." | ||
74 | echo " Usage: $1 infile [outfile] [resolution_in_dpi]" | ||
75 | } | ||
76 | |||
77 | IFILE="$1" | ||
78 | |||
79 | # Need an input file: | ||
80 | if [ -z "$IFILE" ]; then | ||
81 | usage "$0" | ||
82 | exit 1 | ||
83 | fi | ||
84 | |||
85 | # Output filename defaults to "-" (stdout) unless given: | ||
86 | if [ ! -z "$2" ]; then | ||
87 | OFILE="$2" | ||
88 | else | ||
89 | OFILE="-" | ||
90 | fi | ||
91 | |||
92 | # Output resolution defaults to 72 unless given: | ||
93 | if [ ! -z "$3" ]; then | ||
94 | res="$3" | ||
95 | else | ||
96 | res="90" | ||
97 | fi | ||
98 | |||
99 | shrink "$IFILE" "$OFILE" "$res" || exit $? | ||
100 | |||
101 | check_smaller "$IFILE" "$OFILE" | ||