--- /dev/null
+#!/bin/sh
+# Extract PBMs from PDF and create new PDF from them.
+set -e
+n_pages=$(pdfinfo "$1" | grep -e '^Pages:' | awk '{ print $NF }')
+i=1
+while [ "$i" -le "${n_pages}" ]
+do
+ i_formatted=$( printf "%010d" $i)
+ prefix="pdfimages_export_${i_formatted}"
+ echo "extracting page $i as ${prefix}"
+ pdfimages "$1" -l $i -f $i "${prefix}"
+ rm *.ppm
+ source=$(ls -1 "${prefix}"* | head -1)
+ # negate to invert colours, rotate to, well, rotate
+ convert "${source}" -negate -rotate 270 "pdfimages_export_converted_${i_formatted}".pdf
+ rm *.pbm
+ i=$((i + 1))
+done
+pdfunite "pdfimages_export_converted_"* "converted_$1"
+rm pdfimages_export*