HACKER Q&A
📣 GirkovArpa

How to OCR a PDF and preserve whitespace?


I have some rather large PDFs that need to be transcribed, but every service I try has some minor but deal-breaking flaw.

Either they don't support PDFs this large (hundreds of pages), are just really bad at English OCR, or, most commonly, don't preserve whitespace correctly.

The number one problem is whitespace when it comes to multiple columns (similar to newspapers). Either not putting any spaces between words, or when there are multiple columns of text, putting rows in the wrong order. If it was just a single page, this would still be useful, since I could fix it myself. But I have over 1000 pages.

I tried so many free services and trials that I just got charged for forgetting to cancel one (thanks to smallpdf.com for refunding my $12). Is OCR technology just not there yet when it comes to multiple-column pages? Yet, this does not seem to be an issue with newspapers.com, based on my experience using their text search feature. I would like to know what OCR software they are using.


  👤 eigenvalue Accepted Answer ✓
I’ve found that the built in OCR in the iPhone is just way better and more accurate than everything else out there. I’m talking about how if you have an image in your camera roll on the iPhone, you can select the text and copy it out. I had the idea to simply expose that service better so it could be applied to PDFs and made my first ever iPhone app that does this. It can easily handle hundreds of pages too. Only problem is that it’s an iPhone app so it can’t easily be included in a programmatic fashion, but if you’re only trying to convert a reasonable number of documents it’s worth a try— the accuracy is really good:

https://apps.apple.com/us/app/super-pdf-ocr/id6479674248


👤 sargstuff
Not sure if a multi-step is ok, but convert pdf to image format such as png, use AI to recognize 'tabular blocks', convert pdf to 'text format' with tabular blocks as embeddable image to preserve spacing.

https://stackoverflow.com/questions/3203790/parsing-pdf-file...

https://excalibur-py.readthedocs.io/en/master/

https://ledgerbox.io/blog/extract-tables-with-tesseract-ocr

https://www.johnsnowlabs.com/extract-tabular-data-from-pdf-i...

bit more in-depth review : https://dev.to/upsilon_it/how-to-extract-tabular-data-from-p...


👤 tacostakohashi
Did you try textract? https://aws.amazon.com/textract/

In my experience it works amazingly well with columns / tabulated content.


👤 bdowling
Many of the free or cheap OCR services are based on the free, open-source Tesseract OCR.

https://github.com/tesseract-ocr/tesseract/

Those services usually do not expose all of the options. If you’re handy with shell scripts or Python, you can probably get better performance by hand-tuning options for your particular images. For example, if I recall there are page segmentation options to tell Tesseract to expect multi-column text. That alone might get you better performance than the automatic mode.


👤 constantinum
Do give LLMWhisperer[1] a try. It does a good job preserving the layout for the most part — but one cannot escape PDF hell.

Try LLMwhisperer Playground[2] with your documents; there is no need for any setup.

Extracting multi-column layout example - https://imgur.com/roYmv0I

[1] https://llmwhisperer.unstract.com/ [2] https://pg.llmwhisperer.unstract.com/


👤 cpach
Can you use ImageMagick to split the columns into single files…? E.g. if the columns are the same on every page, you can feed the coordinates to ImageMagick. Then do OCR on each of those files.

👤 cyanydeez
Tabula works for tables.

But if you think about space and font widtgs, youll realize nontrivial. Fonts are often variable sizes and table alignments are often left, right or center aligned.

A more general tool ive used is PaddleOCR


👤 sandreas
Your use case seems very specific. I personally am very happy with ocrmypdf[1], which is free and puts an invisible text layer into the pdf. However, since it is free, I'm pretty sure it cannot compete with the commercial solutions you tried.

There also is an older version PDF XChange viewer, that has the ability to do the same thing, although it is presented as "viewer".

1: https://github.com/ocrmypdf/OCRmyPDF/