Skip to content

Instantly share code, notes, and snippets.

@nilesh-akhade
Created September 6, 2022 07:47
Show Gist options
  • Save nilesh-akhade/17cff4395545a9092bdeefb8afb7acb9 to your computer and use it in GitHub Desktop.
Save nilesh-akhade/17cff4395545a9092bdeefb8afb7acb9 to your computer and use it in GitHub Desktop.
Reduces file size of PDF files using ghostscript and pdfcpu
#!/bin/bash
infile=$1
if [[ -z $2 ]]; then
expsz_kb=120
else
expsz_kb=$2
fi
expsz=$((expsz_kb*1000))
file_size=$(stat -c%s "$infile")
echo "# Reducing PDF file size"
echo "Current Size (bytes): $file_size"
echo "Expected Size(bytes): $expsz"
echo "--------------------------------"
pdfcpu optimize $infile
# TODO: Check if Done
file_size=$(stat -c%s "$infile")
res=150
outfile=${1::-4}-compressed.pdf
while [ $file_size -gt $expsz ]
do
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dBATCH -dColorImageResolution=$res -sOutputFile=$outfile $infile
pdfcpu optimize $outfile
file_size=$(stat -c%s "$outfile")
res=$((res-10))
echo res=$res
echo fs=$file_size
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment