Created
September 6, 2022 07:47
-
-
Save nilesh-akhade/17cff4395545a9092bdeefb8afb7acb9 to your computer and use it in GitHub Desktop.
Reduces file size of PDF files using ghostscript and pdfcpu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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