NEW ANSWER
My PDFsak can now simulate Adobe Clearscan (using potrace).
OLD ANSWER
Having ImageMagick, potrace and pdftools (disclaimer: I am the author of pdftools). Assuming you have an input PDF file called input.pdf
:
- Convert every page in a BMP file using ImageMagick:
mkdir bmp
magick convert -density 300 input.pdf -quality 100 ./bmp/input.bmp
- (optional) pre-process image files:
mkdir bmpproc
for /r %%i in (./bmp/*.bmp) do mkbitmap ./bmp/%%~ni.bmp --output ./bmpproc/%%~ni.pbm
- Rasterize every BMP images and save the output in a PDF:
mkdir pdf
for /r %%i in (./bmpproc/*.pbm) do potrace ./bmpproc/%%~ni.pbm -b pdf --output ./pdf/%%~ni.pdf
- Merge the output PDF files together:
pdftools --input-dir .\pdf --output merged.pdf --fitpaper
EDIT: You can obtain a version of your PDF vectorized and OCRed. Thus the only difference with Adobe ClearScan is that the fonts are not embedded (but the text is saved as a "Path" object nevertheless
- Using ImageMagick, convert your pdf to bmp images (one per page)
magick convert -monochrome -density 300 -alpha off input.pdf ./bmp/input.bmp
- Using potrace, postprocess your bmp, save to pbm files, and generate a vectorized version of your pdf
for /r %%i in (./bmp/*.bmp) do mkbitmap ./bmp/%%~ni.bmp --output ./bmpproc/%%~ni.pbm
for /r %%i in (./bmpproc/*.pbm) do potrace ./bmpproc/%%~ni.pbm -b pdf --output ./vectorized/%%~ni.pdf
- Using TesserAct, OCR your pdf file
for /r %%i in (./bmpproc/*.pbm) do tesseract ./bmpproc/%%~ni.pbm ./ocred/%%~ni pdf
- Using pdftools, merge together the pages of the single files for the ocr-ed and the vectorized version:
python -m pdftools --input-dir ocred --overwrite --fitpaper --output ocred.pdf --natural-sort
python -m pdftools --input-dir vectorized --overwrite --fitpaper --output vectorized.pdf --natural-sort
- Now we need to remove the image layer from the ocr-ed file (because we will use the vectorized image). Using qPDF, generate an uncompressed version of your ocr-ed file:
qpdf --qdf --object-streams=disable ocr.pdf ocr-uncompressed.pdf
- Remove whatever is not an image from your ocr-uncompressed.pdf file with the following python 3 script
with open("ocr-uncompressed.pdf", "rb") as fileh:
filedata = fileh.read()
streams = filedata.split("endstream".encode("utf8"))
outdata = bytearray()
for rawstream in streams[:-1]:
before, stream = rawstream.split("stream".encode("utf8"))
outdata += before + "stream".encode("utf8")
if not stream.startswith(bytearray.fromhex("0aff")):
outdata += stream
outdata += "endstream".encode("utf8")
outdata += streams[-1]
with open("ocr-no-img.pdf", "wb") as fileh:
fileh.write(outdata)
- Now using pdftk, overlay the ocr-ed version (with no images) to the vectorized version:
pdftk vectorized.pdf multibackground ocr-no-image.pdf output mergedlayer.pdf
and you are done!