PDFs mit Ghostscript verkleinern

Ghostscript ist ein Interpreter für PostScript und PDFs. Mit ihm lassen sich Postscript und PDF Dateien konvertieren und verkleinern.
Installation
Installation für Linux
Bei mir war Ghostscript unter Linux bereits vorinstalliert. Ansonsten installiert ihr alle notwendigen Programme mit folgendem Aufruf:
apt install ghostscript
Installation für Windows
Für Windows werden Installationsprogramme bereitgestellt, sodass alle Programme wie gewohnt installiert werden können.Die Installationsanleitung findet ihr hier: https://www.ghostscript.com/doc/9.50/Install.htm
PDFs verkleinern (Grundversion)
Bei manchen PDFs hat mir die “Standardkonvertierung” geholfen. Hierzu öffnen wir die Kommandozeile und navigieren in den Ordner, wo die großen PDF-Dateien liegen:
cd my-folder
ps2pdf input.pdf output.pdf
Dem Programm ps2pdf
übergeben wir mit input.pdf
das große Dokument. Das Ergebnis wird
in output.pdf
gespeichert. Bei meinen lokalen Tests wurden manche PDFs kleiner, andere wiederherum
garnicht (bis auf wenige Bytes). Bei den Testdaten kamen PDFs von Webseiten zum Einsatz und
gescannte Dokumente.

Umgewandelte PDFs mit ps2pdf
PDFs verkleinern (Profiversion)
Mittels diverser Recherchen kam ich auf die Webseite von Alfred Klomp . Er hat ein sehr tolles Shrink-Script geschrieben, um PDFs zu verkleinern. Dazu werden diverse Umwandlungen durchgeführt aber auch die Qualität des PDFs reduziert.
#!/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"
Das Script kann lokal als Datei gespeichert und mit 1-3 Parametern aufgerufen werden. Der dritte Parameter bestimmt die DPI. Indem die DPI reduziert wird, können wir eine Datei beliebig verkleinern:
chmod +x shrink.sh
./shrink.sh input.pdf output.pdf
./shrink.sh input.pdf output_50.pdf 50
./shrink.sh input.pdf output_100.pdf 100
./shrink.sh input.pdf output_200.pdf 200
Ich habe die selben Dateien, wie oben bei ps2pdf, mit verschiedenen DPI-Zahlen verkleinert:

shrinkpdf mit verschiedenen Dokumenten und DPIs
Wie ihr sehen könnt, sind die Dateigrößen teilweise sehr unterschiedlich. 2_in.pdf konnte hier beispielsweise garnicht verkleinert werden.Das Script prüft vorher die Dateigröße. Falls das Ausgangsdokument größer ist als das Eingangsdokument, dann wird die Eingangsdatei kopiert. Ihr könnt das Script also sicher in eurem Ablauf nutzen und die Ausgangsdatei sicher in das Zielverzeichnis verschieben. Größer als das Eingangsdokument wird die Datei nicht.
Achtet darauf, dass das Script eine verlustbehaftete Kompression durchführt. Die Qualität der Ausgangsdokumente kann je nach Inhalt und Kompression stark variieren.
Fazit
Wie ihr sehen könnt, gibt es zwei Wege, um ein PDF zu verkleinern. ps2pdf
bietet eine schnelle und
einfache Lösung, um ein PDF zu verkleinern. Allerdings ist diese Umwandlung ohne Parameter stark
beschränkt.
Das Script ist erheblich umfangreicher und bietet die Möglichkeit, die Qualität des Dokuments zu bestimmen. Die Dateigröße wird geprüft, sodass das Ausgangsdokument niemals größer ist als das Original. Mit dem Skript sind stärkere Kompressionen möglich, allerdings sollte immer auf die Qualität geachtet werden.
Bildnachweis: Pixabay.com
Weitere Quellen und Informationen: