Skip to content

Instantly share code, notes, and snippets.

@mrunderline
Created September 30, 2020 08:48
Show Gist options
  • Save mrunderline/83735d1b088d94b2c58f7358b5606c4b to your computer and use it in GitHub Desktop.
Save mrunderline/83735d1b088d94b2c58f7358b5606c4b to your computer and use it in GitHub Desktop.
this script will compress pdf, convert to picture, compress again and check the size. so you can use this script if your pdf file contain some secretly content to prevent other to stole it!
source_path='/path/to/booklet'
target_path='/path/to/result'
dpi=200
quality=25
max_size=1536 # in kb
pdf2pic() {
dirname="${i%.*}"
mkdir -p "$dirname"
ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf $1
pdftoppm -png -r $2 output.pdf $dirname/pg
convert -limit memory 1 -limit map 1 -compress jpeg -quality $3 "$dirname/*.png" "$target_path/$1"
ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$target_path/2$1" "$target_path/$1"
mv "$target_path/2$1" "$target_path/$1"
rm -rf $dirname
wc -c "$target_path/$1" | awk '{print $1}'
}
mkdir -p $target_path
cd $source_path
for i in *.pdf; do
echo $i
result_size=$(pdf2pic $i $dpi $quality)
if [[ $result_size -gt $(expr $max_size \* 1024) ]]; then
pdf2pic $i $dpi $(expr $quality - 10)
fi
done
rm output.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment