Skip to content

Instantly share code, notes, and snippets.

@ladislas
Created August 22, 2018 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ladislas/01ea893bfeb2d2698c355ac8e0b25c5c to your computer and use it in GitHub Desktop.
Save ladislas/01ea893bfeb2d2698c355ac8e0b25c5c to your computer and use it in GitHub Desktop.
for f in "$@"
do
TMP=$(mktemp)
SIZE_OLD=$(wc -c < "$f")
echo "Optimizing '$f' of size $SIZE_OLD"
/usr/local/bin/gs \
-dNOPAUSE -dBATCH -dSAFER \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/screen \
-dDetectDuplicateImages=true \
-dDownsampleColorImages=true \
-dDownsampleGrayImages=true \
-dDownsampleMonoImages=true \
-dColorImageDownsampleType=/Bicubic \
-dGrayImageDownsampleType=/Bicubic \
-dMonoImageDownsampleType=/Bicubic \
-dColorImageResolution=144 \
-dGrayImageResolution=144 \
-dMonoImageResolution=144 \
-dEmbedAllFonts=false \
-dSubsetFonts=true \
-dConvertCMYKImagesToRGB=true \
-dCompressFonts=true \
-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 "$TMP" "${f}-screen-144.pdf"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment