← all projects

khpdf (អក្សរ)

2026 · active

Clean Khmer text out of PDFs that copy-paste mangles. A Rust tool and library that gives back correct Khmer Unicode as Markdown or JSON, in the terminal or the browser.

RustWebAssemblyPython#khmer#pdf#rust

khpdf reads the fonts inside a PDF and gives back correct Khmer Unicode as Markdown or JSON — the text other tools mangle on the way out. It is a small Rust command-line tool and library, and it also runs entirely in your browser.

អក្សរ means "letters, script": what most PDF tools lose on the way out.

Same page, two tools

From a real document Standard extraction (pdftotext) khpdf
World Bank 2016 (Word) ការររៀបចំត្រកបខ័ណឌត្ររប់ត្ររង ការរៀបចំក្របខ័ណ្ឌគ្រប់គ្រង
UNDP 2024 (InDesign) កាារអភិិវឌ្ឍឍមនុុស្សស ការអភិវឌ្ឍមនុស្ស
ILO manual (Limon font) esckþIEføgGMNrKuN សេចក្តីថ្លែងអំណរគុណ
ADB 2014 (stripped font) ប្រពេសពនេះ អាតទញប្រពោជន៍ ប្រទេសនេះ អាចទាញប្រយោជន៍

Every row is real output from a public document. Each problem khpdf fixes was found in a real Cambodian PDF and has a regression test.

Try it

pip install khmerpdf
khpdf report.pdf                      # Markdown in the terminal
khpdf report.pdf -o report.md         # save it; the format follows the file name
khpdf report.pdf -o report.json       # structured JSON
khpdf docs/ -o out/ -r                # a whole folder, subfolders too
khpdf report.pdf --pages 1-3,5        # only some pages

From Python:

import khpdf

text = khpdf.extract("report.pdf")                     # plain text
md = khpdf.extract("report.pdf", format="markdown")    # headings, lists, tables
doc = khpdf.extract_document("report.pdf")             # pages, blocks, fonts, warnings

How it works

khpdf needs a PDF with a text layer — one made on a computer, not a scan. If you can select the text in a viewer, even if it pastes as garbage, khpdf can read it.

  1. Read the fonts, not the clipboard: it decodes each font's own encoding — Khmer OS Unicode, InDesign subsets, even stripped CMaps — instead of trusting the mangled bytes copy-paste hands you.
  2. Recover broken CMaps: when a PDF's character map is damaged or missing, khpdf reconstructs the mapping back to real Unicode.
  3. Decode legacy fonts: 1990s–2012 Limon-family fonts are transliterated from their ASCII-hack layout into proper Khmer Unicode.
  4. Rebuild the structure: headings, paragraphs, numbered lists and tables come back as Markdown; JSON adds per-page fonts, warnings, and every word's bounding box.