Last active
September 14, 2022 21:30
-
-
Save kemelzaidan/331ab82c533f87962895ceeb0c21b0f0 to your computer and use it in GitHub Desktop.
A simple bash script to shrink PDFs based on ghostscript cli tool
This file contains hidden or 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 | |
## This program is free software: you can redistribute it and/or modify | |
## it under the terms of the GNU General Public License version 3 as published | |
## by the Free Software Foundation. | |
## | |
## This program is distributed in the hope that it will be useful, | |
## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
## GNU General Public License for more details. | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <https://www.gnu.org/licenses/> | |
# Check if system has ghostscript tool | |
if ! command -v gs &> /dev/null | |
then | |
echo "ghostscript could not be found" | |
echo "Please, install it and run this script again" | |
exit 1 | |
fi | |
# Check if at least one argument was passed | |
if [ -z "$1" ] || [ "$1" == "--help" ] | |
then | |
echo -e "Use: $0 INPUT_PDF OUTPUT_PDF(OPTIONAL)\n" | |
echo "In case an output file is omitted, the software will generate | |
an output file appending ¨compressed¨ to the input file name" | |
exit | |
fi | |
# check if the first argument is a PDF file | |
MIME=$(file -b --mime-type $1) | |
if [ "$MIME" != "application/pdf" ] | |
then | |
echo "Input is not a valid PDF file" | |
exit | |
fi | |
# Choose the quality output | |
CHOICE=0 | |
VALID=false | |
function pick_quality() { | |
echo -e "\nWhat is your desired output?\n" | |
echo " 1: screen: 72 dpi" | |
echo " 2: ebook: 150 dpi" | |
echo " 3: prepress: 300 dpi" | |
echo -e "\nEnter number 1 to 3 below:" | |
read CHOICE | |
case $CHOICE in | |
1) QUALITY="screen" | |
VALID=true ;; | |
2) QUALITY="ebook" | |
VALID=true;; | |
3) QUALITY="prepress" | |
VALID=true;; | |
*) echo -e "\e[31;1m \nYou typed an invalid number. Choose again: \e[m" | |
sleep 1 ;; | |
esac | |
} | |
# Run pick_quality function until getting a valid input | |
while [ "$VALID" = false ] | |
do | |
pick_quality | |
done | |
# Get the output file name | |
FILE_DIR=$(dirname $1) | |
FULL_FILE=$(basename $1) | |
FILE_NAME=${FULL_FILE%.*} | |
EXTENSION=${FULL_FILE##*.} | |
OUTPUT="" | |
if [ -n "$2" ] | |
then | |
OUTPUT="$2" | |
else | |
OUTPUT="${FILE_DIR}/${FILE_NAME}_compressed.${EXTENSION}" | |
fi | |
# Compressing the PDF | |
echo -e "\nCompressing..." | |
(gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/${QUALITY} -dNOPAUSE -dQUIET -dBATCH -sOutputFile=${OUTPUT} $1) & | |
wait | |
echo -e '\e[1A\e[KDone!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment