Skip to content

Instantly share code, notes, and snippets.

@aytacworld
Created July 16, 2018 08:21
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 aytacworld/849a3b74009b641e9ad35fc0da2d3ddb to your computer and use it in GitHub Desktop.
Save aytacworld/849a3b74009b641e9ad35fc0da2d3ddb to your computer and use it in GitHub Desktop.
compress-scanned pdf files
#!/bin/bash
if [ -z "${1}" ]; then
echo "please use it as 'compress-pdf <filename>'"
else
FILE=$1
NEW_FILE=${FILE}.new.pdf
echo compressing $FILE
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="${NEW_FILE}" "${FILE}"
ORIG_SIZE=$(ls "$FILE" -s | cut -d " " -f 1)
NEW_SIZE=$(ls "${NEW_FILE}" -s | cut -d " " -f 1)
if [ $ORIG_SIZE -lt $NEW_SIZE ]; then
rm "${NEW_FILE}"
else
mv "${FILE}" "${FILE}.original"
mv "${NEW_FILE}" "${FILE}"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment