Skip to content

Instantly share code, notes, and snippets.

@scriptum
Created November 25, 2014 14:20
Show Gist options
  • Save scriptum/340f3ef2682f6eb6362a to your computer and use it in GitHub Desktop.
Save scriptum/340f3ef2682f6eb6362a to your computer and use it in GitHub Desktop.
PDF optimization script
for f in "$@"
do
TMP=$(mktemp)
SIZE_OLD=$(wc -c < "$f")
echo "Optimizing '$f' of size $SIZE_OLD"
/usr/bin/gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/ebook \
-dNOPAUSE \
-dBATCH \
-dQUIET \
-sOutputFile="$TMP" \
"$f"
SIZE_NEW=$(wc -c < "$TMP")
if [ $(echo "$SIZE_NEW > $SIZE_OLD" | bc) = "1" ]
then
echo "New size ($SIZE_NEW) is bigger, skipping"
rm "$TMP"
else
echo "New size: $SIZE_NEW"
mv "$f" "$f".bak
mv "$TMP" "$f"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment