OCR
Recognise scanned pages and add an invisible, searchable text layer in the browser with NexPDF for the web.
License module
OCR needs the ocr module (Enterprise tier).
ocr(options) recognises the text of scanned pages and adds it as an invisible text layer, so the
PDF becomes searchable and its text can be extracted. The page images are not changed. In the
browser the built-in engine runs inside the page, so scans never leave the device.
const doc = await nex.PdfDocument.fromFile(scanFile);
const report = doc.ocr({ dpi: 300 });
console.log(report.engine, report.pages, report.words, report.confidence);
const searchable = doc.toBlob();Options
| Option | Default | Meaning |
|---|---|---|
dpi | 300 | Resolution the pages are rendered at for recognition |
pages | every page | 0-based page indices |
force | false | Also process pages that already have text |
languages | por+eng+spa | Language codes joined with +. The built-in engine reads Latin-script text in any language |
ocr() uses the built-in engine (engine: 'builtin'). Tesseract is not available in the
browser. To use loaded neural models, call doc.raw.ocr(optionsJson) with engine auto or
neural (see Other scripts); it returns the same report as JSON.
The report
| Field | Meaning |
|---|---|
engine | The engine that ran |
pages | Pages recognised |
skipped | Pages skipped because they already had text |
words | Words recognised |
confidence | Average word confidence, 0 to 1 |
text | The recognised text, one string per page |
Other scripts: neural models
The built-in engine reads printed Latin-script text (English, Portuguese, Spanish, French, German
and others) with no models. For Chinese, Japanese, Korean, Arabic, Persian, Urdu, Cyrillic,
Devanagari, Thai, Greek and other scripts, load neural OCR models at run time: PaddleOCR
detection and recognition models in ONNX. No model is bundled; the page fetches the ones it needs.
The standard module nexpdf_bg.wasm contains the neural engine; the
lighter module is built without it, so loading a model
fails there.
You need one text detection model, which serves every language, and one recognition model per script with its character dictionary (UTF-8 text, one symbol per line). See the Python OCR guide for a ready-made set of models.
const bytes = async (url: string) => new Uint8Array(await (await fetch(url)).arrayBuffer());
nex.wasm.ocrLoadModel('{"kind":"detection"}', await bytes('/models/det.onnx'), null);
nex.wasm.ocrLoadModel('{"kind":"recognition","languages":"ar+fa+ur","name":"arabic"}',
await bytes('/models/arabic/rec.onnx'), await (await fetch('/models/arabic/dict.txt')).text());
console.log(JSON.parse(nex.wasm.ocrModelsJson())); // kind, name, languages, symbols, size
const report = JSON.parse(doc.raw.ocr(JSON.stringify({ engine: 'auto', languages: 'ara' })));
console.log(report.engine); // "neural (arabic)"
nex.wasm.ocrUnloadModels('ar'); // or null for every modelModels are loaded once per page load and used by every document. ocrLoadModel options:
| Option | Meaning |
|---|---|
kind | detection, recognition or classification (required) |
languages | Languages a recognition model reads, joined with +, as ISO 639-1 (ar) or Tesseract (ara) codes |
name | Name in listings; loading the same kind and name again replaces the model |
input_height | Recognition input height when the model leaves it open (default 48) |
max_side | Detection: longest side, in pixels, of the page image the detector sees (default 1536) |
An optional classification model is the PaddleOCR cls text-direction model (0 or 180
degrees, input 3×48×192). It takes no dictionary (null). Without it, upright and upside-down
lines are told apart by the recogniser's confidence both ways.
nex.wasm.ocrLoadModel('{"kind":"classification"}', await bytes('/models/cls.onnx'), null);The neural engine reads slanted lines as rotated boxes (the text layer follows the slant). Arabic word spaces are restored from the gaps in the ink, presentation forms are mapped to plain letters and the text is stored in logical order. Vertical CJK columns are read right to left, in the report and in the text layer, and text extraction reads vertical text as columns from right to left. Models are large: mobile models are a few MB, server models much more.
Orientation and skew
Pages scanned sideways, upside down or slightly skewed are corrected: the orientation (0, 90, 180 or 270 degrees) and the skew are detected, the page is read upright, and the text layer is mapped back onto the original page. The built-in engine corrects skew up to 20 degrees, the neural engine up to 15 degrees either way (besides quarter turns). The page images are not changed.
The built-in engine also reads ß, œ, æ, ÿ, ì, Ò, Ù and the Catalan middle dot, and ignores scanner specks.
Limits
- Curved lines are read only when the curve is gentle.
- With the neural engine, skew beyond 15 degrees is not corrected.
- The OCR text layer uses glyph-less fonts, so convert to PDF/A after OCR when the file must be PDF/A.
Large documents
ocr() is synchronous and renders every page at dpi, so on the main thread it blocks the page
until it finishes. For long scans, load the engine in a Web Worker and limit pages to what the
user needs.