I build Amberword, a small web app that turns a WhatsApp chat export into a PDF. Everything runs in the browser: the chat never leaves the device, and the PDF is drawn with react-pdf in a Web Worker.
Until this week it only handled Latin, Greek and Cyrillic text. Messages in Arabic, Hindi or Chinese came out as empty boxes. WhatsApp is used everywhere, so that was a real gap. Adding fonts sounded like an afternoon of work. It took two days, mostly spent inside react-pdf's text pipeline.
Load only the fonts the chat needs
The fonts are Noto, from Fontsource. All of them together are about 60 MB, which is far too much to download for every chat. Before rendering, the app scans the title, the names and every message with Unicode script regexes (\p{Script=Arabic} and so on) and builds a font stack with only the scripts it finds. react-pdf downloads every family in the stack, so the stack has to be short.
Chinese, Japanese and Korean fonts are split into many small subset files. The app keeps a map from character to subset and downloads only the subsets whose characters appear in the chat. A test chat with 15 scripts in two styles downloads 11.7 MB. A Latin-only chat downloads none of these fonts.
Bug 1: the first letter of a line disappeared
In Noto Arabic, a letter with dots can become two glyphs: the base and the dots. When @react-pdf/textkit cut a run of text, it counted from the last glyph of the group instead of the first. At the start of a line the base letter vanished and its dots floated over the next one: "شكرا" became "كرا". The same happened to Bengali, Oriya and Thai vowels drawn in two parts.
The fix is a small patch applied while Vite builds the bundle. Each patched spot must match exactly once: if react-pdf changes, the build stops instead of shipping broken Arabic.
Bug 2: marks drawn on the wrong letter
react-pdf scaled glyph offsets by size / 1000 a second time. Those offsets were already in points. Marks positioned by the font (Arabic dots, Indic and Thai vowel signs above and below) ended up almost unshifted, on the neighbouring letter. I copied the glyph-drawing function and corrected that one line.
Bug 3: right-to-left shaping in the wrong order
react-pdf always shapes text left to right, then reverses right-to-left runs. With the Arabic font the mark positions were computed for the wrong order. Now right-to-left runs are shaped right to left, then put back in the order react-pdf expects to reverse.
Bug 4: letters lost at the end of a message
react-pdf finds where each glyph belongs in the text by adding up the characters of each glyph. fontkit splits some vowels into two glyphs (Khmer ើ, Bengali ো, Thai ำ), adds a dotted circle ◌ and drops invisible characters. The totals stopped matching, and the last glyphs of a run were silently dropped. In Thai, "น้ำใจ" lost its final letter. Now each glyph's character list is rebuilt against the real text: same total, each character exactly once.
Bug 5: Burmese in typing order
fontkit has no shaping engine for Burmese. Letters stayed in the order they are typed, not the order they are displayed: ေ and ြ go before the consonant. I reorder them the way HarfBuzz's Myanmar shaper does, then turn on the font's rules for marks above and below.
Smaller things
- Hyphens. When react-pdf breaks inside a word, it adds a hyphen in the run's font. Most of these fonts don't have one, so it printed a box. It now uses the font's space glyph. A build script checks that every font has one.
- Line breaks. textkit split words wherever the font changed. In Chinese the full-width comma "," comes from a different subset than the letters, so a line could start with a comma. A font change inside a word is no longer a break point.
- Chinese and Japanese. In mixed chats each message gets the glyph shapes of its own language: Japanese, Korean, or Simplified or Traditional Chinese.
- Direction. Each paragraph's direction comes from its first strong letter (Unicode rule P2). Arabic, Hebrew, Syriac and Thaana messages are drawn right to left.
- What I gave up on. Urdu in Nastaliq: fontkit doesn't build its diagonal ligatures, so Urdu is set in Naskh. Mongolian and a few other rare scripts still show a warning before paying.
Testing it
I can't read most of these scripts, so "looks right" wasn't a test. The check that caught Bug 4 renders a line and asserts that every word comes out with all of its glyphs. It fails without the fix. The suite now has 2,881 tests, plus 16 for the scripts.
If you've hit any of these in react-pdf, the patches are small and I'm happy to share details in the comments. The app is at https://amberword.com. You can try it with a sample chat, without an account.
Top comments (0)