Why Hebrew text from a PDF comes out backwards, and how to fix it
Updated · Tested with pdfplumber 0.11.11, python-bidi 0.6.12, pypdf 7.2.0, PyMuPDF 2.2, Poppler 25.03
In short
Many PDFs store Hebrew in the order it is drawn, left to right, not the order it is read. A tool that returns the text as stored gives every line reversed. Extract the lines with pdfplumber and pass each one through python-bidi's get_display to get the reading order back, with numbers and English names in the right place.
You extract the text of a Hebrew PDF and get הרבח חסנ where the page says נסח חברה. Every letter is there, in exactly the opposite order. The examples use hebrew-extract.pdf, a short document with Hebrew labels, a company number, and one sentence with an English name in it.
Why it happens
Hebrew is written from right to left, but a PDF does not know that. It stores shapes and where to draw them. Many programs write a line's shapes in the order they sit on the page from left to right, because that is the simplest way to draw them. This is called visual order. The order in which a person reads the letters is logical order, and it is what every text file, database and search index expects.
A tool that returns the shapes as stored gives you each Hebrew line back to front. It gets worse when a line mixes directions. Take this sentence from the sample:
החברה מוחזקת בידי Harbor Holdings Ltd מאז שנת 2019. בחברה שני דירקטורים ושלושהThe Hebrew runs right to left, while Harbor Holdings Ltd and 2019 run left to right inside it. Reversing the whole line fixes the Hebrew and ruins the English and the number. Putting it right needs the Unicode bidirectional algorithm, the same rules a browser uses to display mixed text.
What four Python libraries return
For the title of the sample and for the mixed sentence:
| Library | Title | Mixed sentence |
|---|---|---|
| pdfplumber | Reversed | Reversed |
| pdfminer.six | Reversed | Reversed |
| pypdf | Correct | Pieces in the wrong order |
| PyMuPDF | Correct | Pieces in the wrong order |
pypdf and PyMuPDF try to repair right-to-left text themselves. Plain lines come out right, but the mixed sentence comes back like this from PyMuPDF, with the year and the name in the wrong places:
. בחברה שני דירקטורים ושלושה2019 מאז שנתHarbor Holdings Ltd החברה מוחזקת בידיA half-repaired line is harder to fix than a fully reversed one, because you can no longer tell what was done to it.
The fix: pdfplumber plus python-bidi
pdfplumber returns each line exactly as stored, and the python-bidi package converts between visual and logical order. Together they give the right text:
pip install pdfplumber python-bidiimport pdfplumber
from bidi.algorithm import get_display
with pdfplumber.open("hebrew-extract.pdf") as pdf:
lines = pdf.pages[0].extract_text().split("\n")
for line in (lines[0], lines[2], lines[8]):
print(get_display(line))נסח חברה
מספר חברה 51-482913-6
החברה מוחזקת בידי Harbor Holdings Ltd מאז שנת 2019. בחברה שני דירקטורים ושלושהThe title is the right way round, the company number is intact, and the English name and the year sit where they belong. get_display is meant for turning logical text into visual text for a screen; applied to a visual line it does the reverse, which is why this works.
Apply it line by line, never to a whole page at once, and only to files that store visual order. If your PDF already stores logical order, which some do, the same call would reverse correct text. Test one line you can read before you run a whole folder.
From the command line
pdftotext, from the Poppler tools, reorders Hebrew itself and gets the title and the labels right. In the mixed sentence it loses the spaces around the English name and the year:
החברה מוחזקת בידי Harbor Holdings Ltdמאז שנת .2019בחברה שני דירקטורים ושלושהIt also wraps Hebrew runs in invisible direction marks, which you should strip before you compare or store the text.
With 100sands
100sands puts Hebrew in reading order itself, with no parameter to set and no bidi step afterwards:
curl https://api.100sands.com/v1/text \
-H "Authorization: Bearer $SANDS_API_KEY" \
-F [email protected]The response has this shape, with the title, a label and its value, and the mixed sentence in reading order:
{
"pages": 1,
"text": "נסח חברה\n...\nמספר חברה\t51-482913-6\n...\nהחברה מוחזקת בידי Harbor Holdings Ltd מאז שנת 2019. בחברה שני דירקטורים ושלושה\n..."
}Numbers and the English name keep their own direction inside the Hebrew line, and there are no invisible direction marks to strip. One case does go wrong: an amount followed by an English word, such as 1,234.56 USD, can come back as USD 1,234.56, so check lines like that (step 4 below). Where the sample sets a label and its value in two columns, they are separated by a tab, as in any language, and the label comes first because it is read first. The same holds for Arabic, Persian and Urdu. It reads PDFs and HTML files that contain text; scanned Hebrew pages are not read.
Scanned Hebrew documents
All of this is about PDFs that contain text. A scanned Hebrew page has no stored order to get wrong, because it has no text at all; OCR reads it and writes logical text. That has its own pitfalls, covered in Hebrew OCR.
A quick test for your own files
- Extract one line you can read.
- If it is back to front, your tool returns visual order: add the bidi step.
- If plain lines are right but lines with numbers or English are scrambled, your tool is half-repairing: switch to one that returns visual order and repair it yourself.
- Check a line with a date or an amount last. Numbers are where the mistakes hide.