Writings

Why Nepali government PDFs render as garbage, and how to fix it

· 5 min read

A lot of what the Nepali government publishes ends up as PDFs: notices, reports, court rulings. PDFs are great for reading, and they look the same on every device.

But more and more of the work around documents is done by AI agents now, and what agents understand is text, ideally clean Markdown. So I wanted to know how ready Nepal's documents are for that, digitally.

Not very, it turns out. Copy text out of one and you get garbage. नेपाल सरकार, “Government of Nepal”, comes out as g]kfn ;/sf/. महालेखापरीक्षकको, the Auditor General's office, comes out as महालेखापरीçकको. In others, लेखापरीक्षण keeps a hole where क्ष belongs, or a vowel sign lands on the wrong side of its consonant.

These look like one problem, but they're three different bugs, often in the same file. Here's what each one is, and how Likhit fixes it.

how a PDF stores text

A PDF doesn't really store text. It stores which shapes, called glyphs, to draw from which font, and where on the page (Bast and Korzen explain this well). To let you copy text, the PDF also carries a lookup table, the ToUnicode character map, that says which character each glyph stands for.

Most of these government PDFs embed their font as a subset, only the glyphs the document uses, with each glyph identified by its number in the font. When copying works, it's because that lookup table is right. In these documents, it usually isn't.

bug 1: broken character maps

Documents set in Kalimati or Lohit Devanagari, the standard Unicode fonts in Nepali offices, carry a ToUnicode map that's simply wrong. That's where महालेखापरीçकको comes from: some glyphs are mapped to Latin characters instead of Devanagari.

The fix starts from something the PDF still has: the font itself. An embedded font has its own character map, which links characters to glyphs, and a GSUB table, which describes how letters combine into conjuncts and half forms. Likhit reads the embedded font, reverses its character map to get glyph-to-character, works out each conjunct from the GSUB rules, and writes a corrected ToUnicode map back into the PDF before extracting the text.

Often the tool that made the PDF has stripped those tables too. Then Likhit recovers the mapping from a reference copy of the font, and the two fonts needed different approaches:

  • Lohit. Every Lohit subset in our documents descends from one release, Lohit Devanagari 2.5.3, and keeps that release's glyph order. So a glyph's number still means the same glyph, and one table derived from the original font decodes every subset. Across 150 documents, that table accounts for all 8,093,837 Lohit glyphs.
  • Kalimati. Two versions of Kalimati are in circulation, and they number their glyphs differently: glyph 106 is a different letter in each. A table keyed on glyph numbers would decode half the documents confidently and wrongly. So Likhit matches each glyph by its outline instead, its exact drawing, coordinate for coordinate. A glyph is recognised whatever number it was given, and a different drawing is never mistaken for it.

One rule holds throughout: if Likhit doesn't recognise a glyph, it keeps whatever the PDF said. An unknown glyph falls back to the old result, never to a new, confident error.

bug 2: Devanagari is drawn in a different order than it's written

Even with every glyph identified, the order can be wrong. In Devanagari, the vowel sign ि is written after its consonant but drawn before it: कि is क then ि in text, but ि comes first on the page. The repha, the small र् drawn above a letter, works the other way round. PDFs store glyphs in the order they're drawn, so extracted text comes out in drawing order. Microsoft's Devanagari font guide shows how much reordering normally goes on between text and what you see.

Likhit first maps these glyphs to temporary markers instead of real characters. Then a reordering pass moves each ि after the consonant cluster it belongs to, including clusters joined by a virama like त्त, and moves each repha back in front of the cluster it sits on.

The tricky part is the rakar, the stroke under त in त्र. A rakar is virama then र, and the reverse order is a repha, which spells a different word: प्रति against पर्ति, मन्त्रालय against मन्तर्ालय. Likhit checks every reordering for this and refuses any change that would turn a rakar into a repha. Getting it right is the difference between स्टर्ीट and स्ट्रीट.

bug 3: legacy keyboard fonts

The oldest documents use fonts like Preeti, Kantipur and PCS Nepali. They come from before Unicode, and they draw Devanagari shapes on ordinary English keyboard keys: the key for g draws न. The PDF honestly reports what's stored, English letters, which is why नेपाल सरकार comes out as g]kfn ;/sf/.

Converting these back is a known problem, and Likhit builds on the maps from an existing open-source converter, npttf2utf, plus layouts it doesn't ship. The hard part is choosing which map to apply, because the font's name can't be trusted. Some fonts named Himalb actually carry Preeti bytes, and reading them with the wrong map caused 1,438 corruptions across 13 annual reports.

So Likhit picks the map by reading the content. It tries every map, scores how much real Nepali each one produces, and only accepts a clear winner. Ties are decided by evidence, never by which map happens to come first in a list. A 303-character passage that tied all six maps used to be read as Preeti, turning ;_Vof into स)ख्या, when the right map gives संख्या.

why not OCR or an LLM

I tried the obvious shortcuts first. OCR throws away a text layer that's almost right and guesses it back from pixels, and classic OCR struggles with Devanagari: one study of Tesseract on Nepali got 69% accuracy. Language models read fluently, which is the problem: they hallucinate when they OCR, more so in scripts with less training data, and in a court ruling a confident wrong name is worse than an obvious error.

Both work well for a handful of documents, where you can read the output and fix it by hand. At hundreds of thousands of pages, nobody can check every one. That needs something deterministic: the same document gives the same text every time, and when it's wrong, it's wrong in a way you can find and fix. OCR is still in Likhit, but only for pages that really are scanned images.

where it ended up

Likhit is an open-source plugin for Microsoft's MarkItDown, on PyPI. On our public benchmark of real government documents, 13 of 16 now convert without any OCR at all.

It started as something I personally wanted to solve, and it ended up helping at work too. At Jawafdehi it's now what we use to transcribe the documents we work with.

It's open source, and anyone is welcome to use it or help make it better. If you have a Nepali PDF that comes out wrong, open an issue with it, or read the contributing guide and send a fix. Every odd font someone reports makes it work for everyone else.

book a chat