I developed this script in dead time to be able to compress the PDFs that they send me to university and also to be able to send scans to the various institutions that require formats that do not weigh more than 2Mb.
github link: https://github.com/NF02/pdfcomp
#!/bin/sh
#- Function -#
command_exists() {
command -v "$@" >/dev/null 2>&1
}
pdfcomprimer(){
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=$comp -dNOPAUSE \
-dQUIET -dBATCH -sOutputFile=$pdfOut $pdfIn
}
pdfcomprimerPrev() {
echo $pdfIn
if [[ $pdfIn == "" ]]; then
echo "Missing input files, try again!"
exit -1
fi
while [ "$con" != "quit" ]
do
cat << 'EOF'
___________ _____________________________________________________________
| Quality | Description |
|-----------|-------------------------------------------------------------|
| screen | The output file will have up to 72 DPI. |
| ebook | The output file will have up to 150 DPI. |
| preprint | The output file will have up to 300 DPI. |
| printer | The output files will have up to 300 DPI and will be ready |
| | for printing |
| default | It depends on which of the above options is assigned as the |
| | “default”. |
| quit | Close the program |
| | |
-------------------------------------------------------------------------
EOF
echo "Select the degree of compression: "
read con
if [[ $con == "quit" ]]; then
exit 0
fi
if [[ $con == "" ]];then
con="default"
fi
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=$comp -dNOPAUSE \
-dQUIET -dBATCH -sOutputFile=/tmp/prev.pdf $pdfIn
if [[ $con == "screen" ]] || [[ $con == "ebook" ]] || [[ $con == "preprint" ]] || [[ $con == "print" ]] || [[ $con == "default" ]]; then
echo "
____________________ _____________________________________ ___________________
| Source file | output file | Compression level |
------------------------------------------------------------------------------
$(du -sh $pdfIn) $(du -sh /tmp/prev.pdf) $con
==============================================================================
"
else echo "format not found"
fi
done
exit 0
}
if ! command_exists gs; then
echo "ghostscript is not installed"
exit -1
fi
#- -#
comp="/default"
pdfIn=$1
pdfOut=$2
if [[ $1 == "-fl" ]] || [[ $1 == "--format-list" ]];then
cat << 'EOF'
___________ _____________________________________________________________
| Quality | Description |
|-----------|-------------------------------------------------------------|
| screen | The output file will have up to 72 DPI. |
| ebook | The output file will have up to 150 DPI. |
| preprint | The output file will have up to 300 DPI. |
| printer | The output files will have up to 300 DPI and will be ready |
| | for printing |
| default | It depends on which of the above options is assigned as the |
| | “default”. |
| | |
-------------------------------------------------------------------------
EOF
exit 0
fi
if [[ $1 == --help ]] || [[ $1 == -h ]] || [[ $1 == -HELP ]] || [[ $1 == "" ]]; then
cat << 'EOF'
┏━┓╺┳┓┏━╸┏━╸┏━┓┏┳┓┏━┓ ┏━┓ ╺┓
┣━┛ ┃┃┣╸ ┃ ┃ ┃┃┃┃┣━┛ ┃┃┃ ┃
╹ ╺┻┛╹ ┗━╸┗━┛╹ ╹╹ ┗━┛╹╺┻╸
This application allows you to compress PDFs, using ghostscript, this script
is designed to simplify and speed up the use of the tool, making it more
user-friendly and practical.
----------------------------------------------------------------------------
- commands -
----------------------------------------------------------------------------
-h (--help): shows help on screen, with all passable parameters.
-f (--format): allows you to select the compression level
-fl (--format-list): shows the list of possible compression levels.
EOF
exit 0
fi
if [[ $1 == "-f" ]] || [[ $1 == "--format" ]];then
pdfIn=$3
pdfOut=$4
if [[ $2 == "screen" ]] || [[ $2 == "ebook" ]] || [[ $2 == "preprint" ]] ||
[[ $2 == "print" ]] || [[ $con == "default" ]]; then
comp="/$2"
else echo "format not exists" && exit -4
fi
fi
if [[ $1 == "-p" ]] || [[ $1 == "--prev" ]]; then
pdfIn=$2
pdfcomprimerPrev
fi
if [[ $pdfIn == "" ]] || [[ $pdfOut == "" ]]; then
echo "Missing input/output files, try again!"
else if [[ $pdfIn == $pdfOut ]];then
echo "You can't overwrite this file"
else if test -f "$pdfIn"; then
pdfcomprimer
else echo "Input file not exists"
fi
fi
fi