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:

Output
החברה מוחזקת בידי 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:

LibraryTitleMixed sentence
pdfplumberReversedReversed
pdfminer.sixReversedReversed
pypdfCorrectPieces in the wrong order
PyMuPDFCorrectPieces 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:

Output
. בחברה שני דירקטורים ושלושה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:

Shell
pip install pdfplumber python-bidi
Python
import 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))
Output
נסח חברה
מספר חברה 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:

Output
החברה מוחזקת בידי  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:

Shell
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:

JSON
{
  "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

  1. Extract one line you can read.
  2. If it is back to front, your tool returns visual order: add the bidi step.
  3. 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.
  4. Check a line with a date or an amount last. Numbers are where the mistakes hide.

Questions

Why is Hebrew text reversed when I extract it from a PDF?

The PDF stores each line's letters in the order they are drawn on the page from left to right. Hebrew is read from right to left, so text returned as stored is back to front.

How do I fix reversed Hebrew text in Python?

Extract each line with pdfplumber, then call get_display from the python-bidi package on it. That applies the Unicode bidirectional rules, which also keep numbers and English words the right way round.

Why not just reverse the string?

Reversing fixes the Hebrew letters but also reverses every number and English word in the line, so 2019 becomes 9102. The bidirectional algorithm reverses only the right-to-left runs.

Does this also apply to Arabic?

The ordering problem is the same, but Arabic has a second one: its letters join, and joined pairs are often extracted in the wrong order. See the guide to Arabic PDFs.

Try it on your own documents

A free plan every month, no credit card. See the Hebrew PDF text.

Start free