Cleaned up the bash a bit to avoid any problems from unexpected spaces or characters, hopefully that's okay!

Nick Bradshaw 2024-01-10 16:01:47 -05:00
parent 6bdff8709c
commit e304d6c115

@ -11,28 +11,31 @@ This wiki page is a repository of example [pre-consume scripts](https://docs.pap
set -e -o pipefail
export LC_ALL=C
#IN="$1"
IN="$DOCUMENT_WORKING_PATH"
PAGES=$(pdfinfo "$IN" | grep -a ^Pages: | tr -dc '0-9')
THRESHOLD=0.002
#IN="$1"
IN="${DOCUMENT_WORKING_PATH}"
PAGES=$(pdfinfo "${IN}" | grep -a "^Pages:" | tr -dc '0-9')
non_blank() {
for i in $(seq 1 $PAGES) ; do
PERCENT=$(gs -o - -dFirstPage=${i} -dLastPage=${i} -sDEVICE=inkcov "${IN}" | grep CMYK | nawk 'BEGIN { sum=0; } {sum += $1 + $2 + $3 + $4;} END { printf "%.5f\n", sum } ')
if awk "BEGIN { exit !($PERCENT > $THRESHOLD) }"; then
echo $i
for (( i="1"; i<="${PAGES}"; i++ )); do
PERCENT=$(gs -o - -dFirstPage="${i}" -dLastPage="${i}" -sDEVICE=inkcov "${IN}" | grep CMYK | nawk 'BEGIN { sum=0; } {sum += $1 + $2 + $3 + $4;} END { printf "%.5f\n", sum } ')
if awk "BEGIN { exit !(${PERCENT} > ${THRESHOLD}) }"; then
echo "${i}"
else
>&2 echo Color-sum is $PERCENT: will remove blank page $i of $IN
>&2 echo "Color-sum is ${PERCENT}: will remove blank page ${i} of ${IN}"
fi
done
done
}
NON_BLANK=$(non_blank)
NON_BLANK="$(non_blank)"
NON_BLANK="$(tr '\n' ' ' <<<"${NON_BLANK}")"
NON_BLANK="${NON_BLANK% }"
if [ -n "$NON_BLANK" ]; then
NON_BLANK=$(echo $NON_BLANK | tr ' ' ",")
qpdf "$IN" --replace-input --pages . $NON_BLANK --
if [ -n "${NON_BLANK}" ]; then
NON_BLANK=$(echo "${NON_BLANK}" | tr ' ' ",")
qpdf "${IN}" --replace-input --pages . "${NON_BLANK}" --
fi
```