There does not appear to be a utility to remove blank pages from PDFs, but we can create a histogram of colours using the convert
command from imagemagick. Blank slides will only have one entry which can be detected with wc
. Once we have a list of non-blank pages we can feed this into pdftk
.
Note that imagemagick numbers pages starting from 0, so we need to adjust for this. We can use a low value in the -density
flag improve performance (though too low seems to result in imagemagick segfaulting).
If we call the following script pdf_rm_blank.sh
, running pdf_rm_blank.sh A
will create A.rm.pdf
from A.pdf
#!/bin/sh
IN="$1"
PAGES=$(pdfinfo $IN.pdf | grep ^Pages: | tr -dc '0-9')
non_blank() {
for i in $(seq 1 $PAGES)
do
if [ $(convert -density 35 "$IN.pdf[$((i-1))]" -define histogram:unique-colors=true -format %c histogram:info:- | wc -l) -ne 1 ]
then
echo $i
#echo $i 1>&2
fi
echo -n . 1>&2
done | tee out.tmp
echo 1>&2
}
set +x
pdftk $IN.pdf cat $(non_blank) output $IN.rm.pdf