Skip to content

Instantly share code, notes, and snippets.

@hbostan
Created June 1, 2022 12:08
Show Gist options
  • Save hbostan/b4350c7bd4fb55528ae75abef6c576e6 to your computer and use it in GitHub Desktop.
Save hbostan/b4350c7bd4fb55528ae75abef6c576e6 to your computer and use it in GitHub Desktop.
Make Grayscale pdf
#!/bin/bash
# Uses ghostscript to make a pdf grayscale.
# Helps to see if graphs make sense in grayscale.
COMMAND=gs
COMMAND_OPTS="-sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dCompatibilityLevel=1.4 -dAutoRotatePages=/None -dNOPAUSE -dBATCH"
OUTFILE=grayscale.pdf
COMPRESS=0
usage() { echo -e "Usage:\n\t$0 [-o OUTPUT] [-c LEVEL] INPUT_FILE\n\n\
Options:\n\
\t-o OUTPUT: Grayscale pdf is saved to OUTPUT (Default: grayscale.pdf)\n\
\t-c LEVEL: Enables compression. LEVEL is either '0', '1' or '2' (Default: 0)\n" 1>&2; exit 1;}
if ! command -v $COMMAND &> /dev/null
then
echo "ERROR: \"$COMMAND\" could not be found"
echo "Make sure that GhostScript is installed"
exit
fi
while getopts "o:c:" opt; do
case $opt in
o)
OUTFILE=$OPTARG
;;
c)
COMPRESS=$OPTARG
((COMPRESS == 1 || COMPRESS == 2 || COMPRESS == 0)) || usage
;;
\?)
usage
;;
esac
done
shift $((OPTIND-1))
if [ $# -lt 1 ];
then echo "No input file specified"; exit 1;
fi
COMMAND_OPTS="-sOutputFile=$OUTFILE $COMMAND_OPTS"
if [ $COMPRESS -eq "1" ]
then
COMMAND_OPTS="$COMMAND_OPTS -dPDFSETTINGS=/ebook"
elif [ "$COMPRESS" -eq "2" ]
then
COMMAND_OPTS="$COMMAND_OPTS -dPDFSETTINGS=/screen"
fi
$COMMAND $COMMAND_OPTS $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment