Handling Arabic and Urdu in PDF documents
What to configure for Arabic and Urdu documents in DocStudio: page direction, locale, fonts, mixed content, and the PDF text extraction limitation to plan around.
Right-to-left documents fail in predictable ways: the layout is not mirrored, a font lacks the script, or Latin names inside Arabic text jump around. This article covers what DocStudio does for each, and one limitation you should plan for.
1. Mirror the layout
{
"schemaVersion": "1.0.0",
"config": { "page": { "direction": "rtl" }, "locale": "ar" },
"body": []
}direction: "rtl" aligns text to the right, runs rows right to left, puts a table's first column on the right and mirrors logical spacing. locale (ar or ur) makes numbers and dates follow the language.
2. Supply a font
DocStudio ships no fonts. Without a font that covers the script, the renderer falls back to the browser default, and the result can be boxes or a mismatched style. Register a font you are licensed to use:
import { readFileSync } from 'node:fs';
import { createFontRegistry } from 'docstudio/render';
const fonts = createFontRegistry();
const problem = fonts.register({
family: 'Noto Naskh Arabic',
weight: 400,
format: 'woff2',
data: new Uint8Array(readFileSync('fonts/noto-naskh-arabic-400.woff2')),
});
if (problem !== undefined) throw new Error(problem.message);Pass fonts to renderDocument for browser printing, and to createPdfService({ fonts }) for server PDF. Faces are embedded as data URIs with a size limit per font, and the service waits for fonts to load before it prints. Urdu usually prefers a Nastaliq style font, so choose the family for your audience.
3. Mixed content
Two cases come up constantly:
- An Arabic note in an English document: put it in a
sectionwithconfig.direction: "rtl". - A Latin name or a number inside Arabic text: bound values are wrapped in an isolating element, so they keep their own reading order.
4. Numbers and dates
Formatting follows the runtime's Intl data. Digit shapes and separators can differ between Node and browser versions, and in the tested runtimes digits come out Latin. If you need Arabic-Indic digits, verify the output in your runtime and, if needed, register a formatter that produces them.
5. The limitation to plan around
The text layer of an Arabic or Urdu PDF is lossy. The page looks right, but copying text, searching and screen-reader access to the extracted text are degraded. If your users search or copy from these PDFs, or accessibility of the extracted text is a requirement, test with your own fonts and viewers before you commit, and consider also providing the content in another format.
Checklist
- Set
directionandlocale. - Register fonts for every script you use, in the renderer and in the PDF service.
- Put mixed-direction content in sections.
- Verify digits in your runtime.
- Test text extraction if it matters to you.
See right-to-left support, the guide and known limitations.
Related: the matching feature page and the quick start.