TPTruPDF

@trupdf/ocr

The TruPDF OCR engine — WASM, runs fully in-browser. Lazy-loaded (the engine core + language packs stay out of the main viewer bundle), per-page progress, self-hostable language packs.

OCREngine

import { OCREngine } from '@trupdf/viewer';
 
const ocr = new OCREngine({
  languages: ['eng', 'fra'], // default ['eng']
  onProgress: ({ page, status, progress }) => { /* 0..1 */ },
  // Self-hosted / offline asset overrides:
  langPath: '/static/ocr-langs/',
  workerPath: '/static/ocr-worker.js',
  corePath: '/static/ocr-core.wasm.js',
});
 
await ocr.init(); // loads the worker + language data
const result = await ocr.recognize(canvas, 1); // one rendered page
await ocr.destroy(); // terminates the worker
  • recognize(image, page) accepts HTMLCanvasElement, HTMLImageElement, or Blob. Pair it with ExportEngine.exportImage to rasterise a page first.
  • Concurrent recognize calls are serialised internally — safe to fire per page in a loop.
  • init() is idempotent; recognize auto-inits on first call.

Result shape

interface OCRPageResult {
  page: number;
  text: string; // joined
  confidence: number; // average across all words, 0–100
  lines: readonly OCRLine[];
  width: number;  // page width in CSS px at the supplied scale
  height: number;
  durationMs: number;
}
 
interface OCRLine {
  text: string;
  confidence: number;
  box: { x: number; y: number; width: number; height: number }; // pixel space, top-left origin
  words: readonly OCRWord[];
}
 
interface OCRWord {
  text: string;
  confidence: number;
  box: OCRBox;
}

Languages

The typed OCRLanguage union covers the common codes: eng, hin, fra, deu, spa, ita, por, rus, jpn, kor, chi_sim, chi_tra, ara, heb, tha, vie, tur. Pass multiple codes to recognise mixed-language documents. The engine's language-pack format covers ~100 languages — point langPath at your own hosted pack directory to serve additional ones offline.

License gate

new OCREngine() calls gate.require('ocr'). Trial tokens cannot OCR.