summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYigit Sever2021-12-13 11:07:45 +0300
committerYigit Sever2021-12-13 11:07:45 +0300
commit73a45bca95916fcd280f8166f7ce09cc3b2d2a44 (patch)
treed9f1af3206ea1113cb3ca3e0676515cd8f010bc8
parent431077fa973f04ae810a090b7a0b7c0e82ab040e (diff)
downloaddotfiles-73a45bca95916fcd280f8166f7ce09cc3b2d2a44.tar.gz
dotfiles-73a45bca95916fcd280f8166f7ce09cc3b2d2a44.tar.bz2
dotfiles-73a45bca95916fcd280f8166f7ce09cc3b2d2a44.zip
shrinkpdf: tracking
-rwxr-xr-x.local/bin/shrinkpdf101
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#
35shrink ()
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
55check_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
70usage ()
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
77IFILE="$1"
78
79# Need an input file:
80if [ -z "$IFILE" ]; then
81 usage "$0"
82 exit 1
83fi
84
85# Output filename defaults to "-" (stdout) unless given:
86if [ ! -z "$2" ]; then
87 OFILE="$2"
88else
89 OFILE="-"
90fi
91
92# Output resolution defaults to 72 unless given:
93if [ ! -z "$3" ]; then
94 res="$3"
95else
96 res="90"
97fi
98
99shrink "$IFILE" "$OFILE" "$res" || exit $?
100
101check_smaller "$IFILE" "$OFILE"