[SCRIPT] Add scripts to shrink pdf

This commit is contained in:
Gregory Trolliet 2021-08-11 11:05:11 +02:00
parent c71ea91aa5
commit 97f76554a5
3 changed files with 107 additions and 1 deletions

View file

@ -17,7 +17,7 @@ svg optimizer svgomg https://lehollandaisvolant.net/tout/tools/svgoptim/
fer ciam https://fer-ge.ch/web/fer-ge fer ciam https://fer-ge.ch/web/fer-ge
inubo https://www.inubo.ch/apps/files/?dir=/&fileid=1238980 inubo https://www.inubo.ch/apps/files/?dir=/&fileid=1238980
instagram rss bridge bibliogram https://bibliogram.pussthecat.org/ instagram rss bridge bibliogram https://bibliogram.pussthecat.org/
ygg torrent https://www.yggtorrent.li/ ygg torrent https://www.yggtorrent.nz/
windows nettoyage https://sebsauvage.net/wiki/doku.php?id=windows windows nettoyage https://sebsauvage.net/wiki/doku.php?id=windows
mail dovecot postfix configuration https://blog.cybiere.fr/post/mail_intro/ mail dovecot postfix configuration https://blog.cybiere.fr/post/mail_intro/
factorio blueprints https://factorioprints.com/ factorio blueprints https://factorioprints.com/
@ -102,3 +102,4 @@ mouse open hardware https://www.ploopy.co/
latex formules math cours https://moonbooks.org/Articles/Formules-math%C3%A9matiques-sous-LaTeX/ latex formules math cours https://moonbooks.org/Articles/Formules-math%C3%A9matiques-sous-LaTeX/
gnu screen cheatsheet https://gist.github.com/jctosta/af918e1618682638aa82 gnu screen cheatsheet https://gist.github.com/jctosta/af918e1618682638aa82
mtg arena card list https://www.magicarenacardlist.com/ mtg arena card list https://www.magicarenacardlist.com/
shrink pdf resize http://www.alfredklomp.com/programming/shrinkpdf/

99
.scripts/shrinkpdf.sh Executable file
View file

@ -0,0 +1,99 @@
#!/bin/sh
# http://www.alfredklomp.com/programming/shrinkpdf
# Licensed under the 3-clause BSD license:
#
# Copyright (c) 2014-2019, Alfred Klomp
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
shrink ()
{
gs \
-q -dNOPAUSE -dBATCH -dSAFER \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.3 \
-dPDFSETTINGS=/screen \
-dEmbedAllFonts=true \
-dSubsetFonts=true \
-dAutoRotatePages=/None \
-dColorImageDownsampleType=/Bicubic \
-dColorImageResolution=$3 \
-dGrayImageDownsampleType=/Bicubic \
-dGrayImageResolution=$3 \
-dMonoImageDownsampleType=/Subsample \
-dMonoImageResolution=$3 \
-sOutputFile="$2" \
"$1"
}
check_smaller ()
{
# If $1 and $2 are regular files, we can compare file sizes to
# see if we succeeded in shrinking. If not, we copy $1 over $2:
if [ ! -f "$1" -o ! -f "$2" ]; then
return 0;
fi
ISIZE="$(echo $(wc -c "$1") | cut -f1 -d\ )"
OSIZE="$(echo $(wc -c "$2") | cut -f1 -d\ )"
if [ "$ISIZE" -lt "$OSIZE" ]; then
echo "Input smaller than output, doing straight copy" >&2
cp "$1" "$2"
fi
}
usage ()
{
echo "Reduces PDF filesize by lossy recompressing with Ghostscript."
echo "Not guaranteed to succeed, but usually works."
echo " Usage: $1 infile [outfile] [resolution_in_dpi]"
}
IFILE="$1"
# Need an input file:
if [ -z "$IFILE" ]; then
usage "$0"
exit 1
fi
# Output filename defaults to "-" (stdout) unless given:
if [ ! -z "$2" ]; then
OFILE="$2"
else
OFILE="-"
fi
# Output resolution defaults to 72 unless given:
if [ ! -z "$3" ]; then
res="$3"
else
res="72"
fi
shrink "$IFILE" "$OFILE" "$res" || exit $?
check_smaller "$IFILE" "$OFILE"

6
.scripts/shrinkpdfauto.sh Executable file
View file

@ -0,0 +1,6 @@
#!/bin/sh
IFILE="$1"
~/.scripts/shrinkpdf.sh "$IFILE" "tmp.pdf" 200
mv "tmp.pdf" "$IFILE"