Open source

arabic-pdf-repair: make Arabic text in old PDFs copyable again

A free, open-source library and command-line tool by GenPDF for PDFs whose Arabic text pastes as gibberish such as ÇáÚÑÈíÉ, as boxes or as the wrong letters. It adds the missing map from each glyph to its letter, so the text can be copied, searched and extracted, and the pages look exactly as they did.

Why Arabic text copied from a PDF turns into gibberish

A PDF draws each letter as a shape from a font. To copy or search the text, a reader needs a table in the file, called ToUnicode, that says which letter each shape stands for. Many Arabic PDFs made by older Windows printer drivers, Ghostscript, PDFCreator or Arabic publishing programs have no such table, or a wrong one.

The page looks right, but copied text pastes as Latin letters such as ÇáÚÑÈíÉ, as empty boxes or as Hebrew letters; search finds nothing, and text extraction, screen readers and AI tools read nonsense.

What it fixes

  • Fonts whose glyphs carry Arabic names (afii57xxx, afii62xxx, uniFE91 and the like).
  • Arabic stored in the slots of Latin letters: Windows-1256, ISO-8859-6 and Mac Arabic, the text that copies as ÇáÚÑÈíÉ.
  • Diwan XT fonts from Arabic publishing programs, including their compact (CFF) copies.
  • Persian and Urdu letters, and maps that are only partly right: the correct entries are kept.
  • Encrypted files that open without a password (RC4, AES-128 and AES-256).

How to use it

With Node.js 20 or newer, one command repairs a file and writes old.repaired.pdf next to it, with nothing to install:

npx arabic-pdf-repair old.pdf

In your own code, install it from npm and pass a PDF's bytes to repairTextLayer: it returns the repaired file, or null when there is nothing to repair. The library also runs in the browser and in web workers.

npm install arabic-pdf-repair

Is it safe to run on my files?

  • The pages look exactly the same: only the map from glyphs to letters is added.
  • It makes no network calls and sends no telemetry: your files stay on your computer.
  • It changes a font only when the font or the code page proves its letters, and guesses nothing.
  • The original file is never overwritten.
  • Releases are built on GitHub and published with npm provenance, so anyone can check which source each version came from.

What it does not do

  • No OCR: a scanned page is a picture, with no text to repair. To read scans, use GenPDF's OCR.
  • It does not reorder text: the order of the copied words is decided by your PDF reader.
  • The file is rewritten, so a digital signature on it no longer validates. Keep the signed original.
  • Files that need a password to open are left as they are.

The same repair, in the GenPDF editor

This library is the text-layer repair that the GenPDF editor runs when it opens a file, published on its own. The editor itself is not open source. To change the words of an Arabic PDF, not only repair its text, open it in the GenPDF editor or see how to edit Arabic text in a PDF.

Licence

Free to use, change and share under the Apache 2.0 licence, in personal and commercial projects. Report a problem or suggest a fix on GitHub.