Skip to content

A fast, helpful, and open-source document parser

feat(wasm): add WebAssembly bindings for browser PDF parsing

Clelia (Astra) Bertelli4220eef08d3f
diff --git a/Cargo.lock b/Cargo.lock
index bbd3eda..0434c0b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -347,6 +347,16 @@ dependencies = [
"memchr",
]
+[[package]]
+name = "console_error_panic_hook"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
+dependencies = [
+ "cfg-if",
+ "wasm-bindgen",
+]
+
[[package]]
name = "constant_time_eq"
version = "0.3.1"
@@ -1198,6 +1208,7 @@ dependencies = [
"tempfile",
"tesseract-rs",
"tokio",
+ "web-time",
]
[[package]]
@@ -1249,6 +1260,20 @@ dependencies = [
"tokio",
]
+[[package]]
+name = "liteparse-wasm"
+version = "2.0.0"
+dependencies = [
+ "console_error_panic_hook",
+ "js-sys",
+ "liteparse",
+ "serde",
+ "serde-wasm-bindgen",
+ "serde_json",
+ "wasm-bindgen",
+ "wasm-bindgen-futures",
+]
+
[[package]]
name = "lock_api"
version = "0.4.14"
@@ -1893,7 +1918,6 @@ checksum = "62e0021ea2c22aed41653bc7e1419abb2c97e038ff2c33d0e1309e49a97deec0"
dependencies = [
"base64",
"bytes",
- "futures-channel",
"futures-core",
"futures-util",
"http",
@@ -2120,6 +2144,17 @@ dependencies = [
"serde_derive",
]
+[[package]]
+name = "serde-wasm-bindgen"
+version = "0.6.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8302e169f0eddcc139c70f139d19d6467353af16f9fce27e8c30158036a1e16b"
+dependencies = [
+ "js-sys",
+ "serde",
+ "wasm-bindgen",
+]
+
[[package]]
name = "serde_core"
version = "1.0.228"
diff --git a/Cargo.toml b/Cargo.toml
index 8fd8f78..32b4e71 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,6 +6,7 @@ members = [
"crates/liteparse",
"crates/liteparse-napi",
"crates/liteparse-python",
+ "crates/liteparse-wasm",
]
[workspace.package]
diff --git a/crates/WASM_PLAN.md b/crates/WASM_PLAN.md
new file mode 100644
index 0000000..fceec19
--- /dev/null
+++ b/crates/WASM_PLAN.md
@@ -0,0 +1,451 @@
+# LiteParse WASM/Browser Support — Implementation Plan
+
+This document describes how to add browser/WASM support to LiteParse, producing an npm package that runs entirely in the browser with no native dependencies.
+
+## Goal
+
+```js
+import init, { LiteParse } from '@llamaindex/liteparse-wasm';
+
+await init(); // load .wasm binary
+
+const parser = new LiteParse({ ocrEnabled: false });
+const result = await parser.parse(pdfBytes); // Uint8Array input
+console.log(result.text);
+```
+
+Users install via npm, import in browser JS/TS, and parse PDFs from `Uint8Array` input. No file paths, no CLI, no LibreOffice/ImageMagick.
+
+## Architecture Overview
+
+```
+┌─────────────────────────────────────────────────┐
+│ Browser JS │
+│ │
+│ @llamaindex/liteparse-wasm (npm package) │
+│ ┌───────────────────────────────────────────┐ │
+│ │ JS glue (from wasm-pack) │ │
+│ │ - init() loads .wasm │ │
+│ │ - LiteParse class wrapper │ │
+│ │ - Optional: tesseract.js OCR callback │ │
+│ └────────────────┬──────────────────────────┘ │
+│ │ wasm-bindgen │
+│ ┌────────────────▼──────────────────────────┐ │
+│ │ liteparse-wasm.wasm │ │
+│ │ - liteparse-wasm crate (Rust) │ │
+│ │ - liteparse core (extract, project, ocr) │ │
+│ │ - pdfium (statically linked .a) │ │
+│ └───────────────────────────────────────────┘ │
+└─────────────────────────────────────────────────┘
+```
+
+PDFium is statically linked into the single `.wasm` binary — one `init()` call, one module.
+
+## Prerequisites
+
+- PDFium WASM build must produce a static archive (`libpdfium.a`) instead of a standalone `.wasm` module. **This change has been made** in `pdfium-binaries` — the `em++` linking step in `steps/06-build.sh` was removed and `steps/07-stage.sh` now stages the `.a` file directly. Once this is built and published as a new release, the `pdfium-sys` crate can consume it.
+
+---
+
+## Changes Required
+
+### 1. `crates/pdfium-sys/build.rs` — Static linking for WASM
+
+The build script currently always links dynamically (`rustc-link-lib=dylib=pdfium`). For wasm32 targets, it needs to link statically and skip the dylib copy step.
+
+**Changes:**
+- When `CARGO_CFG_TARGET_ARCH == "wasm32"`:
+ - Emit `cargo:rustc-link-lib=static=pdfium` instead of `dylib=pdfium`
+ - Skip `copy_dylib_to_target_deps()` (no dynamic loading in WASM)
+ - Skip `fix_dylib_install_name()` (macOS-only concern)
+- The auto-download logic already maps `wasm32-unknown-unknown` to `pdfium-wasm` — this should continue to work once the new release with the `.a` file is published
+
+**Key code location:** `crates/pdfium-sys/build.rs` lines 27-35 (linking), line 50 (dylib copy)
+
+### 2. `crates/liteparse/` — Feature-gate and WASM compatibility
+
+#### 2a. Feature-gate non-WASM modules
+
+Add `#[cfg(not(target_arch = "wasm32"))]` gates to modules that can't work in the browser:
+
+In `src/lib.rs`:
+```rust
+pub mod config;
+pub mod extract;
+pub mod ocr;
+pub mod ocr_merge;
+pub mod output;
+pub mod parser;
+pub mod projection;
+pub mod types;
+
+#[cfg(not(target_arch = "wasm32"))]
+pub mod conversion;
+#[cfg(not(target_arch = "wasm32"))]
+pub mod render;
+```
+
+- **`conversion`** — requires filesystem + external processes (LibreOffice, ImageMagick). No WASM equivalent.
+- **`render`** — uses `image::save()` to write files. Screenshot functionality doesn't make sense in the browser context. The page *rendering* logic used by `ocr_merge` (bitmap rendering via pdfium) is separate and should still work.
+
+#### 2b. Make `OcrEngine` trait async
+
+The current trait is sync:
+```rust
+pub trait OcrEngine {
+ fn name(&self) -> &str;
+ fn recognize(&self, image_data: &[u8], width: u32, height: u32, options: &OcrOptions)
+ -> Result<Vec<OcrResult>, Box<dyn std::error::Error>>;
+}
+```
+
+Change to async:
+```rust
+pub trait OcrEngine: Send + Sync {
+ fn name(&self) -> &str;
+ async fn recognize(&self, image_data: &[u8], width: u32, height: u32, options: &OcrOptions)
+ -> Result<Vec<OcrResult>, Box<dyn std::error::Error>>;
+}
+```
+
+> **Note:** Async traits are stable in Rust 2024 edition (which this project uses). However, `dyn OcrEngine` with async methods requires `async-trait` or manual boxing. Evaluate whether to use `async-trait` crate or `Box<dyn Future>` return types. An alternative is:
+> ```rust
+> fn recognize(&self, ...) -> Pin<Box<dyn Future<Output = Result<...>> + Send + '_>>;
+> ```
+
+**Affected files:**
+- `src/ocr/mod.rs` — trait definition
+- `src/ocr/tesseract.rs` — `TesseractOcrEngine` impl (wrap sync tesseract-rs call in async)
+- `src/ocr/http_simple.rs` — `HttpOcrEngine` impl (convert from `reqwest::blocking` to `reqwest` async)
+- `src/ocr_merge.rs` — calls `ocr_engine.recognize()`, needs `.await`
+- `src/parser.rs` — calls `ocr_and_merge_pages_from_input()`, already async
+- `crates/liteparse-napi/src/lib.rs` — already async, should just work
+- `crates/liteparse-python/src/lib.rs` — uses `tokio::runtime::Runtime::block_on()`, should just work
+
+#### 2c. Replace `reqwest::blocking` with async `reqwest`
+
+In `src/ocr/http_simple.rs`:
+- Change `reqwest::blocking::Client` to `reqwest::Client`
+- Make `recognize()` async (follows from 2b)
+- The reqwest async client works both natively (via tokio) and in WASM (via browser `fetch`)
+
+Update `Cargo.toml` dependencies:
+```toml
+reqwest = { version = "0.13", default-features = false, features = ["json", "multipart", "rustls"] }
+# Remove: "blocking" feature
+# For WASM, reqwest automatically uses browser fetch — no extra features needed
+```
+
+> **Note:** `reqwest`'s `multipart` feature may not be available on WASM targets. Check this — if not, the HTTP OCR engine may need to be `#[cfg(not(target_arch = "wasm32"))]` gated, or use a simpler request format for WASM.
+
+#### 2d. Replace `web_time::Instant`
+
+`web_time::Instant` panics on `wasm32-unknown-unknown`. Use the [`web-time`](https://docs.rs/web-time) crate as a drop-in replacement:
+
+```toml
+# Cargo.toml
+web-time = "1"
+```
+
+```rust
+// In parser.rs, replace:
+use web_time::Instant;
+// With:
+use web_time::Instant;
+```
+
+`web-time` uses `performance.now()` in WASM and delegates to `web_time::Instant` on native — zero-cost on non-WASM targets.
+
+#### 2e. Conditional dependencies in `Cargo.toml`
+
+```toml
+[dependencies]
+# Always available
+anyhow = "1"
+image = { version = "0.25", default-features = false, features = ["png"] }
+pdfium = { ... }
+pdfium-sys = { ... }
+reqwest = { version = "0.13", default-features = false, features = ["json", "multipart", "rustls"] }
+serde = { version = "1", features = ["derive"] }
+serde_json = "1"
+web-time = "1"
+
+# Native only
+clap = { version = "4", features = ["derive"], optional = true }
+infer = { version = "0.19", optional = true }
+tempfile = { version = "3", optional = true }
+tokio = { version = "1", features = ["full"], optional = true }
+tesseract-rs = { version = "0.2", features = ["build-tesseract"], optional = true }
+
+[features]
+default = ["native"]
+native = ["dep:clap", "dep:infer", "dep:tempfile", "dep:tokio", "dep:tesseract-rs"]
+tesseract = ["dep:tesseract-rs"]
+```
+
+> **Alternative:** Instead of a `native` feature, you could use `#[cfg(not(target_arch = "wasm32"))]` everywhere since the target arch is always known at compile time. The feature approach is more explicit; the cfg approach is less boilerplate. Choose one and be consistent.
+
+### 3. New crate: `crates/liteparse-wasm/`
+
+New wasm-bindgen binding crate, following the same pattern as `liteparse-napi` and `liteparse-python`.
+
+#### `Cargo.toml`
+```toml
+[package]
+name = "liteparse-wasm"
+version = "2.0.0"
+edition.workspace = true
+license.workspace = true
+
+[lib]
+crate-type = ["cdylib"]
+
+[dependencies]
+liteparse = { path = "../liteparse", default-features = false }
+pdfium-sys = { path = "../pdfium-sys" }
+pdfium = { path = "../pdfium" }
+wasm-bindgen = "0.2"
+wasm-bindgen-futures = "0.4"
+serde = { version = "1", features = ["derive"] }
+serde-wasm-bindgen = "0.6"
+js-sys = "0.3"
+web-sys = "0.3"
+```
+
+#### `src/lib.rs`
+
+Exports via `#[wasm_bindgen]`:
+
+```rust
+use wasm_bindgen::prelude::*;
+
+#[wasm_bindgen]
+pub struct LiteParse { /* wraps liteparse::parser::LiteParse */ }
+
+#[wasm_bindgen]
+impl LiteParse {
+ #[wasm_bindgen(constructor)]
+ pub fn new(config: JsValue) -> Result<LiteParse, JsError> { ... }
+
+ /// Parse a PDF from raw bytes. Returns a Promise<ParseResult>.
+ pub async fn parse(&self, data: &[u8]) -> Result<JsValue, JsError> { ... }
+
+ /// Format a parse result as JSON or text string.
+ pub fn format(&self, result: JsValue) -> Result<String, JsError> { ... }
+}
+```
+
+**Type conversion:** Use `serde-wasm-bindgen` to convert between Rust structs and JS objects, or define `#[wasm_bindgen]` structs with explicit getters (similar to the napi types pattern). The serde approach is simpler; explicit structs give better TypeScript types.
+
+**OCR callback bridge:** To support user-provided OCR (e.g. tesseract.js), accept a JS function in the config:
+
+```rust
+#[wasm_bindgen]
+extern "C" {
+ pub type JsOcrEngine;
+
+ #[wasm_bindgen(structural, method, catch)]
+ pub async fn recognize(
+ this: &JsOcrEngine,
+ image_data: &[u8],
+ width: u32,
+ height: u32,
+ language: &str,
+ ) -> Result<JsValue, JsValue>;
+}
+```
+
+This lets the JS side provide any OCR implementation:
+```js
+const parser = new LiteParse({
+ ocrEngine: {
+ async recognize(imageData, width, height, language) {
+ // Use tesseract.js, a remote API, or anything else
+ const worker = await Tesseract.createWorker(language);
+ const { data } = await worker.recognize(imageData);
+ return data.words.map(w => ({
+ text: w.text,
+ bbox: [w.bbox.x0, w.bbox.y0, w.bbox.x1, w.bbox.y1],
+ confidence: w.confidence,
+ }));
+ }
+ }
+});
+```
+
+The Rust side wraps `JsOcrEngine` into an `OcrEngine` impl that deserializes the JS return value into `Vec<OcrResult>`.
+
+#### Add to workspace
+
+In root `Cargo.toml`:
+```toml
+[workspace]
+members = [
+ "crates/pdfium-sys",
+ "crates/pdfium",
+ "crates/liteparse",
+ "crates/liteparse-napi",
+ "crates/liteparse-python",
+ "crates/liteparse-wasm",
+]
+```
+
+### 4. New package: `packages/wasm/`
+
+The npm package that users install. Built with `wasm-pack`.
+
+#### Directory structure
+```
+packages/wasm/
+├── package.json
+├── src/
+│ └── lib.ts # Optional TS wrapper for ergonomics
+├── scripts/
+│ └── build.sh # wasm-pack build script
+└── README.md
+```
+
+#### `package.json`
+```json
+{
+ "name": "@llamaindex/liteparse-wasm",
+ "version": "2.0.0",
+ "description": "LiteParse PDF parser for the browser (WASM)",
+ "type": "module",
+ "main": "./pkg/liteparse_wasm.js",
+ "types": "./pkg/liteparse_wasm.d.ts",
+ "files": ["pkg/", "README.md", "LICENSE"],
+ "scripts": {
+ "build": "wasm-pack build ../../crates/liteparse-wasm --target web --out-dir ../../packages/wasm/pkg"
+ },
+ "keywords": ["pdf", "parser", "wasm", "browser"],
+ "license": "Apache-2.0"
+}
+```
+
+#### Build command
+```bash
+wasm-pack build crates/liteparse-wasm --target web --out-dir packages/wasm/pkg
+```
+
+This produces:
+- `pkg/liteparse_wasm_bg.wasm` — the WASM binary (includes pdfium statically linked)
+- `pkg/liteparse_wasm.js` — JS glue with `init()` + class exports
+- `pkg/liteparse_wasm.d.ts` — TypeScript type definitions
+
+#### Optional: TS wrapper (`src/lib.ts`)
+
+A thin TypeScript wrapper can provide nicer ergonomics (auto-init, better types) similar to `packages/node/src/lib.ts`. This is optional — `wasm-pack` already generates usable JS + `.d.ts` files.
+
+### 5. CI / GitHub Actions
+
+Add a WASM build job to the existing CI. Key steps:
+
+```yaml
+- name: Install wasm-pack
+ run: cargo install wasm-pack
+
+- name: Build WASM
+ run: wasm-pack build crates/liteparse-wasm --target web --out-dir packages/wasm/pkg
+
+- name: Publish to npm
+ run: cd packages/wasm && npm publish
+```
+
+> **Note:** Ensure the CI runner has the Emscripten SDK if needed for the pdfium static archive build, or pre-download the `pdfium-wasm` release artifact.
+
+---
+
+## API Surface (Browser)
+
+```typescript
+// Auto-generated by wasm-pack + any TS wrapper
+
+export function init(): Promise<void>;
+
+export interface LiteParseConfig {
+ ocrLanguage?: string;
+ ocrEnabled?: boolean;
+ maxPages?: number;
+ targetPages?: string;
+ dpi?: number;
+ outputFormat?: "json" | "text";
+ preserveVerySmallText?: boolean;
+ password?: string;
+ quiet?: boolean;
+ ocrEngine?: OcrEngine; // User-provided OCR (e.g. tesseract.js)
+}
+
+export interface OcrEngine {
+ recognize(imageData: Uint8Array, width: number, height: number, language: string):
+ Promise<OcrResult[]>;
+}
+
+export interface OcrResult {
+ text: string;
+ bbox: [number, number, number, number]; // [x1, y1, x2, y2]
+ confidence: number;
+}
+
+export interface TextItem {
+ text: string;
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ fontName?: string;
+ fontSize?: number;
+ confidence?: number;
+}
+
+export interface ParsedPage {
+ pageNum: number;
+ width: number;
+ height: number;
+ text: string;
+ textItems: TextItem[];
+}
+
+export interface ParseResult {
+ pages: ParsedPage[];
+ text: string;
+}
+
+export class LiteParse {
+ constructor(config?: LiteParseConfig);
+ parse(data: Uint8Array): Promise<ParseResult>;
+ format(result: ParseResult): string;
+}
+```
+
+**Notably absent vs native API:**
+- No `parse(filePath: string)` — browser has no filesystem
+- No `screenshot()` — no file output; users can render pages themselves if needed
+- No `ocrServerUrl` / `tessdataPath` — replaced by `ocrEngine` callback
+- No `conversion` support — WASM only handles PDF input
+
+---
+
+## Implementation Order
+
+1. **Make `OcrEngine` async** (affects all crates) — do this first since it touches the most code
+2. **Switch `reqwest` from blocking to async** in `HttpOcrEngine`
+3. **Replace `web_time::Instant` with `web-time`** in `parser.rs`
+4. **Feature-gate `conversion` and `render` modules** for non-WASM
+5. **Update `pdfium-sys/build.rs`** for static linking on wasm32
+6. **Create `crates/liteparse-wasm/`** with wasm-bindgen exports
+7. **Create `packages/wasm/`** with npm package structure (package named `@llamaindex/liteparse-wasm`)
+8. **Test**: `wasm-pack build` and verify in a browser
+9. **Add CI** for WASM builds and npm publishing
+
+Steps 1-4 should be done carefully with testing on native builds to ensure nothing breaks. Steps 5-7 are WASM-specific and can be iterated on once the new pdfium-wasm release with the `.a` archive is available.
+
+---
+
+## Open Questions
+
+- **WASM binary size**: PDFium is large. The `.wasm` file may be 10-20MB+. Consider whether to document this or explore wasm-opt / size optimization flags.
+- **`reqwest` multipart on WASM**: The `multipart` feature of reqwest may not work on wasm32. If HTTP OCR is needed in the browser, may need an alternative HTTP approach or just rely on the JS callback OCR engine.
+- **Threading**: WASM is single-threaded by default. The current code is single-threaded anyway, so this should be fine, but `Send + Sync` bounds on `OcrEngine` may need to be relaxed for WASM (`wasm-bindgen` types are `!Send`).
+- **Memory**: Large PDFs may stress the WASM linear memory. `ALLOW_MEMORY_GROWTH` is typically enabled by default in wasm-pack builds.
+- **`wasm32-unknown-unknown` vs `wasm32-unknown-emscripten`**: The pdfium `.a` is built with Emscripten's clang. This *should* link fine with `wasm32-unknown-unknown` (both target wasm32), but if there are libc/syscall mismatches, may need to target `wasm32-unknown-emscripten` instead. Test this early.
diff --git a/crates/liteparse-wasm/Cargo.toml b/crates/liteparse-wasm/Cargo.toml
new file mode 100644
index 0000000..b9380b7
--- /dev/null
+++ b/crates/liteparse-wasm/Cargo.toml
@@ -0,0 +1,27 @@
+[package]
+name = "liteparse-wasm"
+version = "2.0.0"
+edition.workspace = true
+license.workspace = true
+repository.workspace = true
+description = "LiteParse PDF parser — WebAssembly bindings for the browser"
+
+[lib]
+crate-type = ["cdylib", "rlib"]
+
+[dependencies]
+liteparse = { path = "../liteparse", default-features = false }
+wasm-bindgen = "0.2"
+wasm-bindgen-futures = "0.4"
+js-sys = "0.3"
+serde = { version = "1", features = ["derive"] }
+serde_json = "1"
+serde-wasm-bindgen = "0.6"
+console_error_panic_hook = { version = "0.1", optional = true }
+
+[features]
+default = ["panic_hook"]
+panic_hook = ["dep:console_error_panic_hook"]
+
+[package.metadata.wasm-pack.profile.release]
+wasm-opt = ["-O3"]
diff --git a/crates/liteparse-wasm/src/lib.rs b/crates/liteparse-wasm/src/lib.rs
new file mode 100644
index 0000000..e1bf4e8
--- /dev/null
+++ b/crates/liteparse-wasm/src/lib.rs
@@ -0,0 +1,363 @@
+#![cfg(target_arch = "wasm32")]
+//! WebAssembly bindings for LiteParse.
+//!
+//! Exposes a small JS-facing API mirroring `packages/node`:
+//! - `LiteParse` class with `new(config)`, `parse(Uint8Array)`, `format(result)`
+//! - JS-side OCR callback bridge (any object with an async `recognize` method)
+
+use std::pin::Pin;
+
+use js_sys::{Function, Reflect, Uint8Array};
+use serde::{Deserialize, Serialize};
+use wasm_bindgen::prelude::*;
+use wasm_bindgen_futures::JsFuture;
+
+use liteparse::config::{LiteParseConfig, OutputFormat};
+use liteparse::ocr::{OcrEngine, OcrOptions, OcrResult};
+use liteparse::parser::LiteParse as CoreLiteParse;
+use liteparse::types::PdfInput;
+
+// ---------------------------------------------------------------------------
+// Setup
+// ---------------------------------------------------------------------------
+
+#[wasm_bindgen(start)]
+pub fn __wasm_start() {
+ #[cfg(feature = "panic_hook")]
+ console_error_panic_hook::set_once();
+}
+
+// ---------------------------------------------------------------------------
+// JS-facing config (camelCase to match the Node package)
+// ---------------------------------------------------------------------------
+
+#[derive(Debug, Clone, Serialize, Deserialize, Default)]
+#[serde(rename_all = "camelCase", default)]
+struct JsLiteParseConfig {
+ ocr_language: Option<String>,
+ ocr_enabled: Option<bool>,
+ ocr_server_url: Option<String>,
+ tessdata_path: Option<String>,
+ max_pages: Option<usize>,
+ target_pages: Option<String>,
+ dpi: Option<f32>,
+ output_format: Option<String>,
+ preserve_very_small_text: Option<bool>,
+ password: Option<String>,
+ quiet: Option<bool>,
+}
+
+impl JsLiteParseConfig {
+ fn into_core(self) -> Result<LiteParseConfig, JsError> {
+ let mut cfg = LiteParseConfig::default();
+ if let Some(v) = self.ocr_language {
+ cfg.ocr_language = v;
+ }
+ if let Some(v) = self.ocr_enabled {
+ cfg.ocr_enabled = v;
+ }
+ if self.ocr_server_url.is_some() {
+ cfg.ocr_server_url = self.ocr_server_url;
+ }
+ if self.tessdata_path.is_some() {
+ cfg.tessdata_path = self.tessdata_path;
+ }
+ if let Some(v) = self.max_pages {
+ cfg.max_pages = v;
+ }
+ if self.target_pages.is_some() {
+ cfg.target_pages = self.target_pages;
+ }
+ if let Some(v) = self.dpi {
+ cfg.dpi = v;
+ }
+ if let Some(v) = self.output_format {
+ cfg.output_format = match v.as_str() {
+ "json" => OutputFormat::Json,
+ "text" => OutputFormat::Text,
+ other => {
+ return Err(JsError::new(&format!(
+ "invalid outputFormat: {} (expected 'json' or 'text')",
+ other
+ )));
+ }
+ };
+ }
+ if let Some(v) = self.preserve_very_small_text {
+ cfg.preserve_very_small_text = v;
+ }
+ if self.password.is_some() {
+ cfg.password = self.password;
+ }
+ if let Some(v) = self.quiet {
+ cfg.quiet = v;
+ }
+ Ok(cfg)
+ }
+
+ fn from_core(cfg: &LiteParseConfig) -> Self {
+ Self {
+ ocr_language: Some(cfg.ocr_language.clone()),
+ ocr_enabled: Some(cfg.ocr_enabled),
+ ocr_server_url: cfg.ocr_server_url.clone(),
+ tessdata_path: cfg.tessdata_path.clone(),
+ max_pages: Some(cfg.max_pages),
+ target_pages: cfg.target_pages.clone(),
+ dpi: Some(cfg.dpi),
+ output_format: Some(match cfg.output_format {
+ OutputFormat::Json => "json".into(),
+ OutputFormat::Text => "text".into(),
+ }),
+ preserve_very_small_text: Some(cfg.preserve_very_small_text),
+ password: cfg.password.clone(),
+ quiet: Some(cfg.quiet),
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// JS-facing parse result
+// ---------------------------------------------------------------------------
+
+#[derive(Serialize)]
+#[serde(rename_all = "camelCase")]
+struct JsTextItem<'a> {
+ text: &'a str,
+ x: f32,
+ y: f32,
+ width: f32,
+ height: f32,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ font_name: Option<&'a str>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ font_size: Option<f32>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ confidence: Option<f32>,
+}
+
+#[derive(Serialize)]
+#[serde(rename_all = "camelCase")]
+struct JsParsedPage<'a> {
+ page_num: usize,
+ width: f32,
+ height: f32,
+ text: &'a str,
+ text_items: Vec<JsTextItem<'a>>,
+}
+
+#[derive(Serialize)]
+#[serde(rename_all = "camelCase")]
+struct JsParseResult<'a> {
+ pages: Vec<JsParsedPage<'a>>,
+ text: &'a str,
+}
+
+// ---------------------------------------------------------------------------
+// JS OCR engine bridge
+// ---------------------------------------------------------------------------
+
+/// Wraps a JS object that exposes an async `recognize(imageData, width, height, language)`
+/// method, returning `Promise<Array<{text, bbox, confidence}>>`.
+///
+/// `JsValue` is `!Send`, but on `wasm32` (single-threaded) the trait does not
+/// require `Send + Sync`, so this works.
+struct JsOcrEngine {
+ name: String,
+ obj: JsValue,
+}
+
+impl JsOcrEngine {
+ fn new(obj: JsValue) -> Self {
+ Self {
+ name: "js-callback".into(),
+ obj,
+ }
+ }
+}
+
+impl OcrEngine for JsOcrEngine {
+ fn name(&self) -> &str {
+ &self.name
+ }
+
+ fn recognize<'a, 'b: 'a, 'c: 'a>(
+ &'a self,
+ image_data: &'c [u8],
+ width: u32,
+ height: u32,
+ options: &'b OcrOptions,
+ ) -> Pin<Box<dyn Future<Output = Result<Vec<OcrResult>, Box<dyn std::error::Error>>> + '_>>
+ {
+ // Copy bytes into a JS Uint8Array up-front (must happen on the
+ // current thread anyway in wasm).
+ let arr = Uint8Array::new_with_length(image_data.len() as u32);
+ arr.copy_from(image_data);
+ let language = options.language.clone();
+
+ Box::pin(async move {
+ let recognize: JsValue = Reflect::get(&self.obj, &JsValue::from_str("recognize"))
+ .map_err(|e| format!("ocrEngine.recognize lookup failed: {:?}", e))?;
+ let recognize: Function = recognize
+ .dyn_into::<Function>()
+ .map_err(|_| "ocrEngine.recognize is not a function".to_string())?;
+
+ let args = js_sys::Array::new();
+ args.push(&arr);
+ args.push(&JsValue::from_f64(width as f64));
+ args.push(&JsValue::from_f64(height as f64));
+ args.push(&JsValue::from_str(&language));
+
+ let promise = recognize
+ .apply(&self.obj, &args)
+ .map_err(|e| format!("ocrEngine.recognize threw: {:?}", e))?;
+ let promise: js_sys::Promise = promise
+ .dyn_into::<js_sys::Promise>()
+ .map_err(|_| "ocrEngine.recognize did not return a Promise".to_string())?;
+
+ let resolved = JsFuture::from(promise)
+ .await
+ .map_err(|e| format!("ocrEngine.recognize rejected: {:?}", e))?;
+
+ let parsed: Vec<JsOcrResult> = serde_wasm_bindgen::from_value(resolved)
+ .map_err(|e| format!("ocrEngine.recognize result decode failed: {:?}", e))?;
+
+ Ok(parsed
+ .into_iter()
+ .map(|r| OcrResult {
+ text: r.text,
+ bbox: r.bbox,
+ confidence: r.confidence,
+ })
+ .collect())
+ })
+ }
+}
+
+#[derive(Deserialize)]
+struct JsOcrResult {
+ text: String,
+ bbox: [f32; 4],
+ confidence: f32,
+}
+
+// ---------------------------------------------------------------------------
+// LiteParse class (JS-facing)
+// ---------------------------------------------------------------------------
+
+#[wasm_bindgen]
+pub struct LiteParse {
+ inner: CoreLiteParse,
+ config: LiteParseConfig,
+}
+
+#[wasm_bindgen]
+impl LiteParse {
+ /// Construct a new parser. `config` is a JS object (all fields optional).
+ /// If `config.ocrEngine` is present, it is wired up as the OCR backend.
+ #[wasm_bindgen(constructor)]
+ pub fn new(config: JsValue) -> Result<LiteParse, JsError> {
+ let ocr_engine_js = if config.is_object() {
+ Reflect::get(&config, &JsValue::from_str("ocrEngine"))
+ .ok()
+ .filter(|v| !v.is_undefined() && !v.is_null())
+ } else {
+ None
+ };
+
+ let js_cfg: JsLiteParseConfig = if config.is_undefined() || config.is_null() {
+ JsLiteParseConfig::default()
+ } else {
+ serde_wasm_bindgen::from_value(config)
+ .map_err(|e| JsError::new(&format!("invalid config: {}", e)))?
+ };
+ let core_cfg = js_cfg.into_core()?;
+ let mut parser = CoreLiteParse::new(core_cfg.clone());
+ if let Some(js_engine) = ocr_engine_js {
+ parser = parser.with_ocr_engine(std::sync::Arc::new(JsOcrEngine::new(js_engine)));
+ }
+ Ok(LiteParse {
+ inner: parser,
+ config: core_cfg,
+ })
+ }
+
+ /// Return the resolved config (camelCase JS object).
+ #[wasm_bindgen(getter)]
+ pub fn config(&self) -> Result<JsValue, JsError> {
+ let cfg = JsLiteParseConfig::from_core(&self.config);
+ serde_wasm_bindgen::to_value(&cfg)
+ .map_err(|e| JsError::new(&format!("serialize config failed: {}", e)))
+ }
+
+ /// Parse PDF bytes. Returns `Promise<ParseResult>`.
+ pub async fn parse(&self, data: Vec<u8>) -> Result<JsValue, JsError> {
+ let result = self
+ .inner
+ .parse_input(PdfInput::Bytes(data))
+ .await
+ .map_err(|e| JsError::new(&format!("parse failed: {}", e)))?;
+
+ let js_pages: Vec<JsParsedPage<'_>> = result
+ .pages
+ .iter()
+ .map(|p| JsParsedPage {
+ page_num: p.page_number,
+ width: p.page_width,
+ height: p.page_height,
+ text: &p.text,
+ text_items: p
+ .text_items
+ .iter()
+ .map(|i| JsTextItem {
+ text: &i.text,
+ x: i.x,
+ y: i.y,
+ width: i.width,
+ height: i.height,
+ font_name: i.font_name.as_deref(),
+ font_size: i.font_size,
+ confidence: i.confidence,
+ })
+ .collect(),
+ })
+ .collect();
+
+ let js_result = JsParseResult {
+ pages: js_pages,
+ text: &result.text,
+ };
+
+ serde_wasm_bindgen::to_value(&js_result)
+ .map_err(|e| JsError::new(&format!("serialize result failed: {}", e)))
+ }
+
+ /// Format a parse result (JS object as returned from `parse`) as a string
+ /// according to the configured `outputFormat` (`"json"` or `"text"`).
+ pub fn format(&self, result: JsValue) -> Result<String, JsError> {
+ // Round-trip via serde_json to reuse the same formatters as native.
+ let pages_val = Reflect::get(&result, &JsValue::from_str("pages"))
+ .map_err(|_| JsError::new("result.pages missing"))?;
+ let raw_pages: serde_json::Value = serde_wasm_bindgen::from_value(pages_val)
+ .map_err(|e| JsError::new(&format!("decode pages failed: {}", e)))?;
+
+ match self.config.output_format {
+ OutputFormat::Json => serde_json::to_string(&raw_pages)
+ .map_err(|e| JsError::new(&format!("json encode failed: {}", e))),
+ OutputFormat::Text => {
+ // For text output, concatenate the per-page `text` strings.
+ let mut out = String::new();
+ if let Some(arr) = raw_pages.as_array() {
+ for (i, p) in arr.iter().enumerate() {
+ if i > 0 {
+ out.push_str("\n\n");
+ }
+ if let Some(s) = p.get("text").and_then(|v| v.as_str()) {
+ out.push_str(s);
+ }
+ }
+ }
+ Ok(out)
+ }
+ }
+ }
+}
diff --git a/crates/liteparse/Cargo.toml b/crates/liteparse/Cargo.toml
index b4e5e66..89dd1fc 100644
--- a/crates/liteparse/Cargo.toml
+++ b/crates/liteparse/Cargo.toml
@@ -24,9 +24,13 @@ image = { version = "0.25", default-features = false, features = ["png"] }
infer = "0.19.0"
pdfium = { package = "liteparse-pdfium", version = "1.0.0", path = "../pdfium" }
pdfium-sys = { package = "liteparse-pdfium-sys", version = "1.0.0", path = "../pdfium-sys" }
-reqwest = { version = "0.13.3", default-features = false, features = ["blocking", "json", "form", "multipart", "rustls"] }
+reqwest = { version = "0.13.3", default-features = false, features = ["json", "form", "multipart", "rustls"] }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
tempfile = "3.27.0"
tesseract-rs = { version = "0.2", features = ["build-tesseract"], optional = true }
-tokio = { version = "1.52.3", features = ["full"] }
+tokio = { version = "1.52.3", features = ["rt", "macros", "sync", "time", "io-util"] }
+web-time = "1.1.0"
+
+[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
+tokio = { version = "1.52.3", features = ["rt-multi-thread", "fs", "process"] }
diff --git a/crates/liteparse/src/lib.rs b/crates/liteparse/src/lib.rs
index 66a9416..231e6d1 100644
--- a/crates/liteparse/src/lib.rs
+++ b/crates/liteparse/src/lib.rs
@@ -1,4 +1,5 @@
pub mod config;
+#[cfg(not(target_arch = "wasm32"))]
pub mod conversion;
pub mod extract;
pub mod ocr;
@@ -6,5 +7,6 @@ pub mod ocr_merge;
pub mod output;
pub mod parser;
pub mod projection;
+#[cfg(not(target_arch = "wasm32"))]
pub mod render;
pub mod types;
diff --git a/crates/liteparse/src/main.rs b/crates/liteparse/src/main.rs
index 1fdb1ea..edb9950 100644
--- a/crates/liteparse/src/main.rs
+++ b/crates/liteparse/src/main.rs
@@ -305,7 +305,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut errors = 0usize;
for file_path in &files {
- let t0 = std::time::Instant::now();
+ let t0 = web_time::Instant::now();
// Build output path: mirror directory structure
let rel = file_path.strip_prefix(&cmd.input_dir).unwrap_or(file_path);
diff --git a/crates/liteparse/src/ocr/http_simple.rs b/crates/liteparse/src/ocr/http_simple.rs
index 68f1ded..9771b0b 100644
--- a/crates/liteparse/src/ocr/http_simple.rs
+++ b/crates/liteparse/src/ocr/http_simple.rs
@@ -1,6 +1,6 @@
-use std::time::Duration;
+use std::{pin::Pin, time::Duration};
-use reqwest::blocking::{
+use reqwest::{
Client,
multipart::{Form, Part},
};
@@ -39,14 +39,14 @@ impl HttpOcrEngine {
}
}
- fn _recognize_batch(
+ async fn _recognize_batch(
&self,
images: Vec<&[u8]>,
options: OcrOptions,
) -> Result<Vec<Vec<OcrResult>>, Box<dyn std::error::Error>> {
let mut results: Vec<Vec<OcrResult>> = vec![];
for i in images {
- let result = self.recognize(i, 0, 0, &options)?;
+ let result = self.recognize(i, 0, 0, &options).await?;
results.push(result);
}
Ok(results)
@@ -58,40 +58,45 @@ impl OcrEngine for HttpOcrEngine {
&self.name
}
- fn recognize(
- &self,
- image_data: &[u8],
+ fn recognize<'a, 'b: 'a, 'c: 'a>(
+ &'a self,
+ image_data: &'c [u8],
_width: u32,
_height: u32,
- options: &OcrOptions,
- ) -> Result<Vec<OcrResult>, Box<dyn std::error::Error>> {
- let client = Client::new();
- let form = Form::new()
- .part(
- "file",
- Part::bytes(image_data.to_vec())
- .file_name("image.png")
- .mime_str("image/png")?,
- )
- .text("language", options.language.clone());
- let response: HttpOcrResponse = client
- .post(&self.server_url)
- .multipart(form)
- .timeout(Duration::from_millis(60000))
- .send()?
- .json()?;
-
- let results: Vec<OcrResult> = response
- .results
- .iter()
- .map(|i| OcrResult {
- text: i.text.clone(),
- bbox: i.bbox,
- confidence: i.confidence,
- })
- .collect();
-
- Ok(results)
+ options: &'b OcrOptions,
+ ) -> Pin<Box<dyn Future<Output = Result<Vec<OcrResult>, Box<dyn std::error::Error>>> + Send + '_>>
+ {
+ Box::pin(async move {
+ let client = Client::new();
+ let form = Form::new()
+ .part(
+ "file",
+ Part::bytes(image_data.to_vec())
+ .file_name("image.png")
+ .mime_str("image/png")?,
+ )
+ .text("language", options.language.clone());
+ let response: HttpOcrResponse = client
+ .post(&self.server_url)
+ .multipart(form)
+ .timeout(Duration::from_millis(60000))
+ .send()
+ .await?
+ .json()
+ .await?;
+
+ let results: Vec<OcrResult> = response
+ .results
+ .iter()
+ .map(|i| OcrResult {
+ text: i.text.clone(),
+ bbox: i.bbox,
+ confidence: i.confidence,
+ })
+ .collect();
+
+ Ok(results)
+ })
}
}
@@ -123,13 +128,13 @@ mod tests {
assert!(parsed.results.is_empty());
}
- #[test]
- fn test_recognize_network_error() {
+ #[tokio::test]
+ async fn test_recognize_network_error() {
let e = HttpOcrEngine::new("http://127.0.0.1:1/ocr".into());
let opts = OcrOptions {
language: "eng".into(),
};
- let r = e.recognize(&[0u8; 4], 1, 1, &opts);
+ let r = e.recognize(&[0u8; 4], 1, 1, &opts).await;
assert!(r.is_err());
}
}
diff --git a/crates/liteparse/src/ocr/mod.rs b/crates/liteparse/src/ocr/mod.rs
index 1f2091c..687e3b9 100644
--- a/crates/liteparse/src/ocr/mod.rs
+++ b/crates/liteparse/src/ocr/mod.rs
@@ -1,5 +1,8 @@
+use std::pin::Pin;
+
+#[cfg(not(target_arch = "wasm32"))]
pub mod http_simple;
-#[cfg(feature = "tesseract")]
+#[cfg(all(feature = "tesseract", not(target_arch = "wasm32")))]
pub mod tesseract;
/// A single word-level OCR result with bounding box and confidence.
@@ -16,15 +19,33 @@ pub struct OcrOptions {
pub language: String,
}
+/// On native targets, `OcrEngine` and its returned futures must be `Send` so
+/// they can be moved across thread boundaries by the multi-threaded tokio
+/// runtime. On wasm32 there is only a single thread and JS-backed engines
+/// hold `!Send` types (`JsValue`, `js_sys::Function`, ...), so we relax
+/// those bounds for the wasm target.
+#[cfg(not(target_arch = "wasm32"))]
+pub trait OcrEngine: Send + Sync {
+ fn name(&self) -> &str;
+ fn recognize<'a, 'b: 'a, 'c: 'a>(
+ &'a self,
+ image_data: &'c [u8],
+ width: u32,
+ height: u32,
+ options: &'b OcrOptions,
+ ) -> Pin<Box<dyn Future<Output = Result<Vec<OcrResult>, Box<dyn std::error::Error>>> + Send + '_>>;
+}
+
+#[cfg(target_arch = "wasm32")]
pub trait OcrEngine {
fn name(&self) -> &str;
- fn recognize(
- &self,
- image_data: &[u8],
+ fn recognize<'a, 'b: 'a, 'c: 'a>(
+ &'a self,
+ image_data: &'c [u8],
width: u32,
height: u32,
- options: &OcrOptions,
- ) -> Result<Vec<OcrResult>, Box<dyn std::error::Error>>;
+ options: &'b OcrOptions,
+ ) -> Pin<Box<dyn Future<Output = Result<Vec<OcrResult>, Box<dyn std::error::Error>>> + '_>>;
}
#[cfg(test)]
@@ -36,29 +57,35 @@ mod tests {
fn name(&self) -> &str {
"dummy"
}
- fn recognize(
- &self,
- _image_data: &[u8],
+ fn recognize<'a, 'b: 'a, 'c: 'a>(
+ &'a self,
+ _image_data: &'c [u8],
_width: u32,
_height: u32,
- options: &OcrOptions,
- ) -> Result<Vec<OcrResult>, Box<dyn std::error::Error>> {
- Ok(vec![OcrResult {
- text: format!("lang={}", options.language),
- bbox: [0.0, 0.0, 10.0, 10.0],
- confidence: 0.9,
- }])
+ options: &'b OcrOptions,
+ ) -> Pin<
+ Box<
+ dyn Future<Output = Result<Vec<OcrResult>, Box<dyn std::error::Error>>> + Send + '_,
+ >,
+ > {
+ Box::pin(async move {
+ Ok(vec![OcrResult {
+ text: format!("lang={}", options.language),
+ bbox: [0.0, 0.0, 10.0, 10.0],
+ confidence: 0.9,
+ }])
+ })
}
}
- #[test]
- fn test_engine_trait_object() {
+ #[tokio::test]
+ async fn test_engine_trait_object() {
let engine: Box<dyn OcrEngine> = Box::new(DummyEngine);
assert_eq!(engine.name(), "dummy");
let opts = OcrOptions {
language: "eng".into(),
};
- let r = engine.recognize(&[], 1, 1, &opts).unwrap();
+ let r = engine.recognize(&[], 1, 1, &opts).await.unwrap();
assert_eq!(r.len(), 1);
assert_eq!(r[0].text, "lang=eng");
assert_eq!(r[0].bbox, [0.0, 0.0, 10.0, 10.0]);
diff --git a/crates/liteparse/src/ocr/tesseract.rs b/crates/liteparse/src/ocr/tesseract.rs
index 4d74c36..f33e464 100644
--- a/crates/liteparse/src/ocr/tesseract.rs
+++ b/crates/liteparse/src/ocr/tesseract.rs
@@ -1,3 +1,5 @@
+use std::pin::Pin;
+
use super::{OcrEngine, OcrOptions, OcrResult};
use tesseract_rs::{TessPageIteratorLevel, TesseractAPI};
@@ -37,71 +39,76 @@ impl OcrEngine for TesseractOcrEngine {
"tesseract"
}
- fn recognize(
- &self,
- image_data: &[u8],
+ fn recognize<'a, 'b: 'a, 'c: 'a>(
+ &'a self,
+ image_data: &'c [u8],
width: u32,
height: u32,
- options: &OcrOptions,
- ) -> Result<Vec<OcrResult>, Box<dyn std::error::Error>> {
- let language = Self::normalize_language(&options.language);
-
- let api = TesseractAPI::new();
-
- // Determine tessdata path: explicit config > TESSDATA_PREFIX env > tesseract-rs default
- let tessdata_path = self
- .tessdata_path
- .clone()
- .or_else(|| std::env::var("TESSDATA_PREFIX").ok());
-
- match &tessdata_path {
- Some(path) => api.init(path, language)?,
- None => {
- // tesseract-rs with build-tesseract downloads eng.traineddata automatically
- // and caches it; use its default path
- let default_path = default_tessdata_dir();
- api.init(&default_path, language)?;
+ options: &'b OcrOptions,
+ ) -> Pin<Box<dyn Future<Output = Result<Vec<OcrResult>, Box<dyn std::error::Error>>> + Send + '_>>
+ {
+ Box::pin(async move {
+ let language = Self::normalize_language(&options.language);
+
+ let api = TesseractAPI::new();
+
+ // Determine tessdata path: explicit config > TESSDATA_PREFIX env > tesseract-rs default
+ let tessdata_path = self
+ .tessdata_path
+ .clone()
+ .or_else(|| std::env::var("TESSDATA_PREFIX").ok());
+
+ match &tessdata_path {
+ Some(path) => api.init(path, language)?,
+ None => {
+ // tesseract-rs with build-tesseract downloads eng.traineddata automatically
+ // and caches it; use its default path
+ let default_path = default_tessdata_dir();
+ api.init(&default_path, language)?;
+ }
}
- }
- // Set image from raw RGB bytes (3 bytes per pixel)
- let bytes_per_pixel = 3;
- let bytes_per_line = width as i32 * bytes_per_pixel;
- api.set_image(
- image_data,
- width as i32,
- height as i32,
- bytes_per_pixel,
- bytes_per_line,
- )?;
-
- api.recognize()?;
-
- let iter = api.get_iterator()?;
-
- let mut results = Vec::new();
- loop {
- if let Ok((text, left, top, right, bottom, confidence)) = iter.get_word_with_bounds() {
- // tesseract-rs returns confidence 0-100, normalize to 0-1
- let conf = confidence / 100.0;
-
- // Filter low confidence (below 30%, matching TS behavior)
- if conf > 0.3 && !text.trim().is_empty() {
- results.push(OcrResult {
- text,
- bbox: [left as f32, top as f32, right as f32, bottom as f32],
- confidence: conf,
- });
+ // Set image from raw RGB bytes (3 bytes per pixel)
+ let bytes_per_pixel = 3;
+ let bytes_per_line = width as i32 * bytes_per_pixel;
+ api.set_image(
+ image_data,
+ width as i32,
+ height as i32,
+ bytes_per_pixel,
+ bytes_per_line,
+ )?;
+
+ api.recognize()?;
+
+ let iter = api.get_iterator()?;
+
+ let mut results = Vec::new();
+ loop {
+ if let Ok((text, left, top, right, bottom, confidence)) =
+ iter.get_word_with_bounds()
+ {
+ // tesseract-rs returns confidence 0-100, normalize to 0-1
+ let conf = confidence / 100.0;
+
+ // Filter low confidence (below 30%, matching TS behavior)
+ if conf > 0.3 && !text.trim().is_empty() {
+ results.push(OcrResult {
+ text,
+ bbox: [left as f32, top as f32, right as f32, bottom as f32],
+ confidence: conf,
+ });
+ }
}
- }
- match iter.next(TessPageIteratorLevel::RIL_WORD) {
- Ok(true) => continue,
- _ => break,
+ match iter.next(TessPageIteratorLevel::RIL_WORD) {
+ Ok(true) => continue,
+ _ => break,
+ }
}
- }
- Ok(results)
+ Ok(results)
+ })
}
}
diff --git a/crates/liteparse/src/ocr_merge.rs b/crates/liteparse/src/ocr_merge.rs
index a9f3ee4..cb0397b 100644
--- a/crates/liteparse/src/ocr_merge.rs
+++ b/crates/liteparse/src/ocr_merge.rs
@@ -7,7 +7,7 @@ use pdfium::Library;
///
/// OCR is triggered when a page has fewer than 100 characters of native text
/// or has embedded images.
-pub fn ocr_and_merge_pages(
+pub async fn ocr_and_merge_pages(
pages: &mut [Page],
pdf_path: &str,
dpi: f32,
@@ -21,54 +21,90 @@ pub fn ocr_and_merge_pages(
ocr_engine,
ocr_language,
)
+ .await
}
/// Run OCR on pages that need it and merge results into text_items.
/// Accepts a `PdfInput` for either file path or in-memory bytes.
-pub fn ocr_and_merge_pages_from_input(
+pub async fn ocr_and_merge_pages_from_input(
pages: &mut [Page],
input: &PdfInput,
dpi: f32,
ocr_engine: &dyn OcrEngine,
ocr_language: &str,
) -> Result<(), Box<dyn std::error::Error>> {
- let lib = Library::init();
- let document = match input {
- PdfInput::Path(path) => lib.load_document(path, None)?,
- PdfInput::Bytes(data) => lib.load_document_from_bytes(data, None)?,
- };
+ // Phase 1: render all pages that need OCR. The pdfium `Document` holds raw
+ // pointers that are not `Send`, so we must not hold it across an `.await`.
+ // Render synchronously into owned buffers and drop the document before
+ // awaiting the OCR engine.
+ struct RenderedPage {
+ idx: usize,
+ rgb_bytes: Vec<u8>,
+ width: u32,
+ height: u32,
+ }
- for page in pages.iter_mut() {
- let text_length: usize = page.text_items.iter().map(|item| item.text.len()).sum();
- let page_obj = document.page((page.page_number - 1) as i32)?;
- let has_images = !page_obj.image_bounds(25.0, 0.9).is_empty();
+ let rendered: Vec<RenderedPage> = {
+ let lib = Library::init();
+ let document = match input {
+ PdfInput::Path(path) => lib.load_document(path, None)?,
+ PdfInput::Bytes(data) => lib.load_document_from_bytes(data, None)?,
+ };
- if text_length >= 100 && !has_images {
- continue;
- }
+ let mut rendered = Vec::new();
+ for (idx, page) in pages.iter().enumerate() {
+ let text_length: usize = page.text_items.iter().map(|item| item.text.len()).sum();
+ let page_obj = document.page((page.page_number - 1) as i32)?;
+ let has_images = !page_obj.image_bounds(25.0, 0.9).is_empty();
- eprintln!(
- "[ocr] page {} needs OCR (text_length={}, has_images={})",
- page.page_number, text_length, has_images
- );
+ if text_length >= 100 && !has_images {
+ continue;
+ }
- // Render page to raw RGB pixels
- let bitmap = page_obj.render(dpi)?;
- let width = bitmap.width() as u32;
- let height = bitmap.height() as u32;
- let rgba = bitmap.to_rgba();
+ eprintln!(
+ "[ocr] page {} needs OCR (text_length={}, has_images={})",
+ page.page_number, text_length, has_images
+ );
+
+ let bitmap = page_obj.render(dpi)?;
+ let width = bitmap.width() as u32;
+ let height = bitmap.height() as u32;
+ let rgba = bitmap.to_rgba();
+
+ let img: ImageBuffer<Rgba<u8>, Vec<u8>> = ImageBuffer::from_raw(width, height, rgba)
+ .ok_or("failed to create image buffer")?;
+ let rgb_img = image::DynamicImage::ImageRgba8(img).to_rgb8();
+ let rgb_bytes = rgb_img.into_raw();
+
+ rendered.push(RenderedPage {
+ idx,
+ rgb_bytes,
+ width,
+ height,
+ });
+ }
+ rendered
+ // `document` and `lib` are dropped here before any `.await`
+ };
- // Convert RGBA to RGB for tesseract
- let img: ImageBuffer<Rgba<u8>, Vec<u8>> =
- ImageBuffer::from_raw(width, height, rgba).ok_or("failed to create image buffer")?;
- let rgb_img = image::DynamicImage::ImageRgba8(img).to_rgb8();
- let rgb_bytes = rgb_img.into_raw();
+ // Phase 2: run OCR (async) on rendered pages and merge results back.
+ for r in rendered {
+ let RenderedPage {
+ idx,
+ rgb_bytes,
+ width,
+ height,
+ } = r;
+ let page = &mut pages[idx];
let options = OcrOptions {
language: ocr_language.to_string(),
};
- let ocr_results = match ocr_engine.recognize(&rgb_bytes, width, height, &options) {
+ let ocr_results = match ocr_engine
+ .recognize(&rgb_bytes, width, height, &options)
+ .await
+ {
Ok(results) => results,
Err(e) => {
eprintln!("[ocr] failed for page {}: {}", page.page_number, e);
diff --git a/crates/liteparse/src/parser.rs b/crates/liteparse/src/parser.rs
index 697ac45..0e0946c 100644
--- a/crates/liteparse/src/parser.rs
+++ b/crates/liteparse/src/parser.rs
@@ -1,7 +1,9 @@
use crate::config::{LiteParseConfig, OutputFormat, parse_target_pages};
+#[cfg(not(target_arch = "wasm32"))]
use crate::conversion;
use crate::extract;
use crate::ocr::OcrEngine;
+#[cfg(not(target_arch = "wasm32"))]
use crate::ocr::http_simple::HttpOcrEngine;
#[cfg(feature = "tesseract")]
use crate::ocr::tesseract::TesseractOcrEngine;
@@ -21,17 +23,36 @@ pub struct ParseResult {
/// Main LiteParse orchestrator.
pub struct LiteParse {
config: LiteParseConfig,
+ /// Optional caller-provided OCR engine. When set, this overrides the
+ /// built-in selection logic (HTTP OCR / Tesseract). This is the primary
+ /// mechanism for plugging an OCR engine in environments without the
+ /// built-ins (e.g. WASM, where the JS side supplies a callback engine).
+ ocr_engine_override: Option<std::sync::Arc<dyn OcrEngine>>,
}
impl LiteParse {
pub fn new(config: LiteParseConfig) -> Self {
- Self { config }
+ Self {
+ config,
+ ocr_engine_override: None,
+ }
+ }
+
+ /// Override the OCR engine. When set, the engine is used regardless of
+ /// `ocr_server_url` / built-in Tesseract availability.
+ pub fn with_ocr_engine(mut self, engine: std::sync::Arc<dyn OcrEngine>) -> Self {
+ self.ocr_engine_override = Some(engine);
+ self
}
/// Parse a document from a file path, returning structured results.
///
/// Non-PDF files are automatically converted to PDF first (requires
/// LibreOffice/ImageMagick on the system).
+ ///
+ /// Not available on `wasm32` — the browser has no filesystem. Use
+ /// [`LiteParse::parse_input`] with [`PdfInput::Bytes`] instead.
+ #[cfg(not(target_arch = "wasm32"))]
pub async fn parse(&self, input: &str) -> Result<ParseResult, Box<dyn std::error::Error>> {
// Resolve file path to a PdfInput (convert non-PDFs first)
let pdf_input = if conversion::is_pdf(input) {
@@ -59,7 +80,7 @@ impl LiteParse {
}
};
- let t0 = std::time::Instant::now();
+ let t0 = web_time::Instant::now();
// Determine which pages to extract
let target_pages = self
@@ -77,7 +98,7 @@ impl LiteParse {
self.config.max_pages,
self.config.password.as_deref(),
)?;
- let t1 = std::time::Instant::now();
+ let t1 = web_time::Instant::now();
log(&format!(
"[liteparse] extract: {:.1}ms ({} pages)",
t1.duration_since(t0).as_secs_f64() * 1000.0,
@@ -86,16 +107,33 @@ impl LiteParse {
// OCR pass
if self.config.ocr_enabled {
- let engine: Box<dyn OcrEngine> = if let Some(ref url) = self.config.ocr_server_url {
- Box::new(HttpOcrEngine::new(url.clone()))
+ let engine: std::sync::Arc<dyn OcrEngine> = if let Some(e) =
+ self.ocr_engine_override.clone()
+ {
+ e
} else {
- #[cfg(feature = "tesseract")]
+ #[cfg(not(target_arch = "wasm32"))]
{
- Box::new(TesseractOcrEngine::new(self.config.tessdata_path.clone()))
+ if let Some(ref url) = self.config.ocr_server_url {
+ std::sync::Arc::new(HttpOcrEngine::new(url.clone()))
+ } else {
+ #[cfg(feature = "tesseract")]
+ {
+ std::sync::Arc::new(TesseractOcrEngine::new(
+ self.config.tessdata_path.clone(),
+ ))
+ }
+ #[cfg(not(feature = "tesseract"))]
+ {
+ return Err("OCR enabled but no --ocr-server-url provided and tesseract feature is disabled".into());
+ }
+ }
}
- #[cfg(not(feature = "tesseract"))]
+ #[cfg(target_arch = "wasm32")]
{
- return Err("OCR enabled but no --ocr-server-url provided and tesseract feature is disabled".into());
+ return Err(
+ "OCR enabled but no `ocrEngine` callback was provided (WASM builds have no built-in OCR engine)".into(),
+ );
}
};
ocr_merge::ocr_and_merge_pages_from_input(
@@ -104,9 +142,10 @@ impl LiteParse {
self.config.dpi,
engine.as_ref(),
&self.config.ocr_language,
- )?;
+ )
+ .await?;
}
- let t_ocr = std::time::Instant::now();
+ let t_ocr = web_time::Instant::now();
log(&format!(
"[liteparse] ocr: {:.1}ms",
t_ocr.duration_since(t1).as_secs_f64() * 1000.0
@@ -114,7 +153,7 @@ impl LiteParse {
// Grid projection
let parsed_pages = projection::project_pages_to_grid(pages);
- let t2 = std::time::Instant::now();
+ let t2 = web_time::Instant::now();
log(&format!(
"[liteparse] project: {:.1}ms",
t2.duration_since(t_ocr).as_secs_f64() * 1000.0
diff --git a/crates/pdfium-sys/build.rs b/crates/pdfium-sys/build.rs
index fc7e029..e9e3b44 100644
--- a/crates/pdfium-sys/build.rs
+++ b/crates/pdfium-sys/build.rs
@@ -23,31 +23,40 @@ fn main() {
});
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
+ let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
println!("cargo:rustc-link-search=native={}", lib_dir.display());
- // On Windows MSVC the import library is named `pdfium.dll.lib`, so
- // we must pass `pdfium.dll` as the lib name (the linker appends `.lib`).
- if target_os == "windows" {
- println!("cargo:rustc-link-lib=dylib=pdfium.dll");
+ if target_arch == "wasm32" {
+ // For wasm targets, pdfium is shipped as a static archive (libpdfium.a)
+ // and linked statically into the final .wasm module. There is no
+ // dynamic loading and no need to copy any shared library.
+ println!("cargo:rustc-link-lib=static=pdfium");
+ println!("cargo:lib_path={}", lib_dir.display());
} else {
- println!("cargo:rustc-link-lib=dylib=pdfium");
+ // On Windows MSVC the import library is named `pdfium.dll.lib`, so
+ // we must pass `pdfium.dll` as the lib name (the linker appends `.lib`).
+ if target_os == "windows" {
+ println!("cargo:rustc-link-lib=dylib=pdfium.dll");
+ } else {
+ println!("cargo:rustc-link-lib=dylib=pdfium");
+ }
+ println!("cargo:lib_path={}", lib_dir.display());
+
+ // Copy the dylib into OUT_DIR so it's on the rpath that cargo sets
+ // for crates with `links = ...`. This is needed on macOS where the
+ // dylib's install name is @rpath/libpdfium.dylib.
+ // On Windows the DLL lives in bin/, not lib/.
+ let dll_dir = if target_os == "windows" {
+ lib_dir
+ .parent()
+ .map(|p| p.join("bin"))
+ .unwrap_or(lib_dir.clone())
+ } else {
+ lib_dir.clone()
+ };
+ copy_dylib_to_target_deps(&dll_dir);
}
- println!("cargo:lib_path={}", lib_dir.display());
-
- // Copy the dylib into OUT_DIR so it's on the rpath that cargo sets
- // for crates with `links = ...`. This is needed on macOS where the
- // dylib's install name is @rpath/libpdfium.dylib.
- // On Windows the DLL lives in bin/, not lib/.
- let dll_dir = if target_os == "windows" {
- lib_dir
- .parent()
- .map(|p| p.join("bin"))
- .unwrap_or(lib_dir.clone())
- } else {
- lib_dir.clone()
- };
- copy_dylib_to_target_deps(&dll_dir);
run_bindgen(&include_dir);
}
diff --git a/packages/wasm/README.md b/packages/wasm/README.md
new file mode 100644
index 0000000..76fe2b4
--- /dev/null
+++ b/packages/wasm/README.md
@@ -0,0 +1,97 @@
+# @llamaindex/liteparse-wasm
+
+Browser/WebAssembly build of [LiteParse](https://github.com/run-llama/liteparse) — a fast, lightweight PDF parser with spatial text extraction.
+
+This package runs entirely in the browser. No server, no cloud calls.
+
+## Install
+
+```sh
+npm install @llamaindex/liteparse-wasm
+```
+
+## Quick start
+
+```ts
+import init, { LiteParse } from "@llamaindex/liteparse-wasm";
+
+// Load the wasm module (point at the file shipped with the package).
+await init();
+
+const parser = new LiteParse({
+ ocrEnabled: false, // OCR requires a JS-side engine (see below)
+ outputFormat: "json",
+});
+
+// `data` is a Uint8Array (e.g. from fetch / File / drag-drop).
+const bytes = new Uint8Array(await file.arrayBuffer());
+const result = await parser.parse(bytes);
+
+console.log(result.text); // full document text
+console.log(result.pages[0]); // per-page items with bboxes
+
+// Or format the structured result as a string:
+const out = parser.format(result);
+```
+
+## Config options
+
+All optional, camelCase:
+
+| Option | Type | Default | Description |
+|---|---|---|---|
+| `ocrLanguage` | `string` | `"eng"` | Language code passed to the OCR engine |
+| `ocrEnabled` | `boolean` | `true` | Run OCR on text-sparse pages |
+| `maxPages` | `number` | `1000` | Stop after this many pages |
+| `targetPages` | `string` | — | e.g. `"1-5,10,15-20"` |
+| `dpi` | `number` | `150` | Render DPI for OCR / screenshots |
+| `outputFormat` | `"json" \| "text"` | `"json"` | Format used by `parser.format(...)` |
+| `preserveVerySmallText` | `boolean` | `false` | Keep tiny text that's normally filtered |
+| `password` | `string` | — | Password for protected PDFs |
+| `quiet` | `boolean` | `false` | Suppress progress logging |
+| `ocrEngine` | `object` | — | JS-side OCR engine (see below) |
+
+## OCR in the browser
+
+The native HTTP-OCR and Tesseract backends are not available in the browser. To use OCR, pass an object with a `recognize` method:
+
+```ts
+const parser = new LiteParse({
+ ocrEnabled: true,
+ ocrLanguage: "eng",
+ ocrEngine: {
+ /**
+ * @param imageData PNG-encoded image bytes
+ * @param width rendered page width in pixels
+ * @param height rendered page height in pixels
+ * @param language e.g. "eng"
+ * @returns array of { text, bbox: [x1,y1,x2,y2], confidence }
+ */
+ async recognize(imageData, width, height, language) {
+ // e.g. call a worker that wraps tesseract.js, or a remote OCR service
+ return [
+ { text: "Hello", bbox: [10, 20, 80, 40], confidence: 0.98 },
+ ];
+ },
+ },
+});
+```
+
+## Building from source
+
+Requires Rust + [`wasm-pack`](https://rustwasm.github.io/wasm-pack/):
+
+```sh
+# from packages/wasm
+npm run build # web target (default)
+npm run build:bundler # for webpack/rollup/vite
+npm run build:nodejs # for node.js
+```
+
+Output goes to `pkg/`.
+
+> **Note:** A real build also needs a static `libpdfium.a` compiled for `wasm32-unknown-emscripten`/`wasm32-unknown-unknown` exposed via `PDFIUM_LIB_PATH`. See the project root `crates/WASM_PLAN.md` for details.
+
+## License
+
+Apache-2.0
diff --git a/packages/wasm/package.json b/packages/wasm/package.json
new file mode 100644
index 0000000..df07ec2
--- /dev/null
+++ b/packages/wasm/package.json
@@ -0,0 +1,48 @@
+{
+ "name": "@llamaindex/liteparse-wasm",
+ "version": "2.0.0",
+ "description": "Fast, lightweight PDF parsing with spatial text extraction — WebAssembly build for browsers",
+ "type": "module",
+ "main": "./pkg/liteparse_wasm.js",
+ "types": "./pkg/liteparse_wasm.d.ts",
+ "exports": {
+ ".": {
+ "types": "./pkg/liteparse_wasm.d.ts",
+ "import": "./pkg/liteparse_wasm.js"
+ },
+ "./liteparse_wasm_bg.wasm": "./pkg/liteparse_wasm_bg.wasm",
+ "./package.json": "./package.json"
+ },
+ "files": [
+ "pkg",
+ "README.md",
+ "LICENSE"
+ ],
+ "scripts": {
+ "build": "wasm-pack build ../../crates/liteparse-wasm --release --target web --out-dir ../../packages/wasm/pkg --out-name liteparse_wasm",
+ "build:bundler": "wasm-pack build ../../crates/liteparse-wasm --release --target bundler --out-dir ../../packages/wasm/pkg --out-name liteparse_wasm",
+ "build:nodejs": "wasm-pack build ../../crates/liteparse-wasm --release --target nodejs --out-dir ../../packages/wasm/pkg --out-name liteparse_wasm",
+ "clean": "rm -rf pkg",
+ "prepublishOnly": "npm run build"
+ },
+ "keywords": [
+ "pdf",
+ "parser",
+ "ocr",
+ "text-extraction",
+ "pdf-to-text",
+ "wasm",
+ "webassembly",
+ "browser",
+ "document-parsing"
+ ],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/run-llama/liteparse.git"
+ },
+ "author": "LlamaIndex",
+ "license": "Apache-2.0",
+ "engines": {
+ "node": ">=18.0.0"
+ }
+}