Skip to content

Instantly share code, notes, and snippets.

@IIPoliII
Last active December 15, 2022 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IIPoliII/dfe302965ef780caec7bf94ef0eac887 to your computer and use it in GitHub Desktop.
Save IIPoliII/dfe302965ef780caec7bf94ef0eac887 to your computer and use it in GitHub Desktop.
GhostScript PDF compression of a folder and all the subfolders with multithreading (using screen)

(Install ghostscript and screen depends on your distro here for debian/ubuntu)

apt install -y screen ghostscript

Firstly change the path where your files are by changning the line files='/home/poli/test

Then you can simply do a chmod +x on both files (they need to be in the same directory) and run

./pdfCompress.sh

To download both scripts run wget https://gist.githubusercontent.com/IIPoliII/dfe302965ef780caec7bf94ef0eac887/raw/6c401c3b95165c4a6e4332e227eb70f63aa00f98/GSCompress.sh && wget https://gist.githubusercontent.com/IIPoliII/dfe302965ef780caec7bf94ef0eac887/raw/6c401c3b95165c4a6e4332e227eb70f63aa00f98/pdfCompress.sh

or simply download both files manually

#!/bin/bash
#Variable for choosing the file
FileInput=$1
FileOutput=$2
#GS compression of the pdf
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dBATCH -dDetectDuplicateImages -dCompressFonts=true -r300 -sOutputFile="$FileOutput" "$FileInput"
mv "$FileOutput" "$FileInput"
#!/bin/bash
# Created the 11.11.2019 using ghostscript
#Where to serach the file
files='/home/poli/test'
#Needed variables
count=1
seconds=0
#Remove the tmp folder in case it still exsits
rm -rf /tmp/GSCompressing
#Map all folders to an array
mapfile -t FoldersList < <(find $files -type f -name '*.pdf' | sed 's%/[^/]*$%/%' | awk '!a[$0]++')
for Folder in "${FoldersList[@]}"
do
#Map all the files of the folder to an array
mapfile -t FilesList < <(find $Folder -maxdepth 1 -type f -name '*.pdf' | sed "s/'/\\\'/")
#Create a temporary directory
mkdir /tmp/GSCompressing
#Loop trought array to compress each files
for File in "${FilesList[@]}"
do
#Put only the file name in the variable
FileName=`echo "$File" | sed 's|.*/||'`
#Launch a screen compressing the PDF's
screen -dmS GSCompress${count} ./GSCompress.sh "$File" "/tmp/GSCompressing/${FileName}"
echo "Compressing the PDF : $File"
let "count++"
done
#Now we wait until the screens are finished so we can assume the pdf are compressed
screenls=$(screen -ls)
#We check if any screen is still on
while [[ $screenls == *GSCompress* ]]
do
screenls=$(screen -ls)
let "seconds++"
echo -ne "Checking if ghostscript is still compressing : $seconds"\\r
sleep 1s
done
seconds=0
rm -rf /tmp/GSCompressing
done
#This echo is to prevent to return on the same line as the \\r does
echo ""
echo "All PDFS were compressed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment