Skip to content

Instantly share code, notes, and snippets.

@eniehack
Created October 27, 2022 07:31
Show Gist options
  • Save eniehack/71f3ee9b20acc1322dbbd206ef14bb6e to your computer and use it in GitHub Desktop.
Save eniehack/71f3ee9b20acc1322dbbd206ef14bb6e to your computer and use it in GitHub Desktop.
generates raster pdf script
#/bin/sh
set -eu
for exe in "gs" "gm" "pdftoppm" "mktemp" ; do
which "$exe" >/dev/null
if [ $? -ne 0 ]; then
exit 1
fi
done
help() {
echo "
pdf2imgpdf: generates raster pdf
usage: pdf2imgpdf [input file] [output file's prefix]
option:
-h show this message
-o specifies output file's name
" 1>&2
exit 0
}
DIR=$(mktemp -d)
current=$(pwd)
SANITIZED_DIR=$(echo $DIR | sed 's/\//\\&/g' | sed 's/\./\\./g')
while getopts ho: OPTION; do
case $OPTION in
h) help ;;
o) OUTPUTFILE=$OPTARG;;
esac
done
if [ $OUTPUTFILE ] ; then
$OUTPUTFILE="$2-img-optimized.pdf"
fi
pdftoppm -png "$1" "$DIR/$2"
gm convert -page A4+0+0 $(ls "$DIR" | grep -E "$2-[0-9]+.png" | sed "s/^/$SANITIZED_DIR\//") "$DIR/$2-img.pdf"
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/printer \
-sOutputFile="$DIR/$OUTPUTFILE" \
"$DIR/$2-img.pdf"
cp "$DIR/$OUTPUTFILE" "$current/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment