I needed to remove a bad OCR and reduce the size of my PDF as well; I came up with the following script using ocrmypdf
and ghostscript
.
#!/usr/bin/sh
TEMP_FILE="$(mktemp --suffix=.pdf)" &&
ghostscript -q -dNOPAUSE -dBATCH -dSAFER -dPDFA=2 -dPDFACompatibilityPolicy=1 -dSimulateOverprint=true -sDEVICE=pdfwrite -dCompatibilityLevel=1.3 -dPDFSETTINGS=/ebook -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dFILTERTEXT -dImageResolution=300 -sOutputFile="$TEMP_FILE" "$1" &&
ocrmypdf "$TEMP_FILE" "$2" &&
rm "$TEMP_FILE"
The long ghostscript
line removes the text layer and makes various space-saving changes to the first argument $1
, saving them to a temporary file. We then add OCR with ocrmypdf
(which is an excellent tool), and output to the path given by the second argument $2
.