OCR
The TruPDF OCR engine — WASM, runs fully in-browser. ~100 languages. Lazy-loaded (~6 MB compressed) so unused sessions pay zero.
Recognize
import { OCREngine } from '@trupdf/viewer';
const ocr = new OCREngine({
languages: ['eng', 'fra'], // first is primary, rest are fallbacks
onProgress: ({ page, status, progress }) => console.log(page, status, progress),
});
await ocr.init(); // boots the worker + loads language models (idempotent)
const result = await ocr.recognize(canvas, 1); // (image, 1-indexed page)
for (const line of result.lines) {
console.log(line.text, line.confidence, line.box);
}
await ocr.destroy(); // terminates the worker, frees WASM memoryInputs accepted: HTMLCanvasElement, HTMLImageElement, Blob.
Rasterise a page first (e.g. via ExportEngine.exportImage) before
calling recognize. Concurrent recognize calls are serialised
internally, so it is safe to fire one per page in a loop.
OCRPageResult
| Field | Meaning |
|---|---|
page | The page number you passed in |
text | Full recognised text (trimmed) |
confidence | Average confidence across all words (0–100) |
lines | OCRLine[] — { text, confidence, box, words } per line |
width | Source image width in pixels |
height | Source image height in pixels |
durationMs | Wall-clock recognition time |
Line and word box values are pixel-space bounding boxes
({ x, y, width, height }, top-left origin) so you can paint
highlights straight onto the source canvas.
Languages
The OCRLanguage type covers the common codes:
eng (English), fra (French), deu (German), spa (Spanish),
ita (Italian), por (Portuguese), rus (Russian), chi_sim
(Chinese, simplified), chi_tra (traditional), jpn (Japanese),
kor (Korean), ara (Arabic), heb (Hebrew), hin (Hindi),
tha (Thai), vie (Vietnamese), tur (Turkish)
Language models are fetched on demand — only the languages you list
are downloaded, on first init().
Self-hosting the engine assets
Air-gapped or CDN-restricted deployments can serve the OCR engine's worker and core assets — plus the language models — from their own origin and point the engine at them:
const ocr = new OCREngine({
workerPath: '/static/ocr-worker.js',
corePath: '/static/ocr-core.wasm.js',
langPath: '/static/lang/',
});workerPath and corePath override where the engine's worker script
and WASM core load from; langPath is the URL prefix for the
per-language model files. Serve the files yourself and every OCR
request stays inside your network.
From the viewer
When aiConfig is set with OCR fallback, the AI panel auto-OCRs
scanned pages before the AI pipeline runs. You can also surface OCR
as its own toolbar action and re-flow recognised text as a
selectable text layer (standard mode only).
License gating
OCREngine calls gate.require('ocr') in its constructor. Trial
tokens cannot OCR.