Skip to content

A fast, helpful, and open-source document parser

add napi bindings

Logan Markewichae69889a3512
diff --git a/.github/workflows/node-bindings.yml b/.github/workflows/node-bindings.yml
new file mode 100644
index 0000000..3664582
--- /dev/null
+++ b/.github/workflows/node-bindings.yml
@@ -0,0 +1,276 @@
+name: Node.js Bindings
+
+on:
+ push:
+ branches: [main]
+ tags: ['v*']
+ pull_request:
+ branches: [main]
+
+env:
+ CARGO_TERM_COLOR: always
+ # pdfium-sys auto-downloads during build, this ensures consistent version
+ PDFIUM_RELEASE_TAG: chromium/7827
+
+jobs:
+ build:
+ strategy:
+ fail-fast: false
+ matrix:
+ include:
+ - os: macos-latest
+ target: aarch64-apple-darwin
+ node-arch: darwin-arm64
+ pdfium-dylib: libpdfium.dylib
+
+ - os: macos-13
+ target: x86_64-apple-darwin
+ node-arch: darwin-x64
+ pdfium-dylib: libpdfium.dylib
+
+ - os: ubuntu-latest
+ target: x86_64-unknown-linux-gnu
+ node-arch: linux-x64-gnu
+ pdfium-dylib: libpdfium.so
+
+ - os: ubuntu-latest
+ target: aarch64-unknown-linux-gnu
+ node-arch: linux-arm64-gnu
+ pdfium-dylib: libpdfium.so
+ use-cross: true
+
+ - os: windows-latest
+ target: x86_64-pc-windows-msvc
+ node-arch: win32-x64-msvc
+ pdfium-dylib: pdfium.dll
+
+ runs-on: ${{ matrix.os }}
+ name: Build ${{ matrix.node-arch }}
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 20
+
+ - uses: dtolnay/rust-toolchain@stable
+ with:
+ targets: ${{ matrix.target }}
+
+ - name: Install cross-compilation tools (Linux ARM64)
+ if: matrix.use-cross
+ run: |
+ sudo apt-get update
+ sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
+
+ - name: Install dependencies
+ working-directory: packages/node
+ run: npm ci
+
+ - name: Build native module
+ working-directory: packages/node
+ run: npx napi build --cargo-cwd ../../crates/liteparse-napi --platform --release --js false --dts native.d.ts --target ${{ matrix.target }} .
+
+ - name: Copy pdfium dylib (Unix)
+ if: runner.os != 'Windows'
+ working-directory: packages/node
+ run: bash scripts/copy-pdfium.sh .
+
+ - name: Copy pdfium dylib (Windows)
+ if: runner.os == 'Windows'
+ working-directory: packages/node
+ shell: pwsh
+ run: |
+ # Find pdfium.dll in the build cache
+ $cacheBase = "$env:LOCALAPPDATA\pdfium-rs"
+ if (-not (Test-Path $cacheBase)) {
+ $cacheBase = "$env:USERPROFILE\.cache\pdfium-rs"
+ }
+ $dll = Get-ChildItem -Path $cacheBase -Recurse -Filter "pdfium.dll" | Select-Object -First 1
+ if ($dll) {
+ Copy-Item $dll.FullName -Destination ".\pdfium.dll"
+ Write-Host "Copied $($dll.FullName) -> .\pdfium.dll"
+ } else {
+ # Also check for pdfium.dll.lib -> the actual DLL is in bin/
+ $dllLib = Get-ChildItem -Path $cacheBase -Recurse -Filter "pdfium.dll" -ErrorAction SilentlyContinue | Select-Object -First 1
+ if (-not $dllLib) {
+ Write-Error "Could not find pdfium.dll in cache"
+ exit 1
+ }
+ }
+
+ - name: Build TypeScript
+ working-directory: packages/node
+ run: npx tsc
+
+ - name: Upload artifacts
+ uses: actions/upload-artifact@v4
+ with:
+ name: bindings-${{ matrix.node-arch }}
+ path: |
+ packages/node/*.node
+ packages/node/${{ matrix.pdfium-dylib }}
+ if-no-files-found: error
+
+ # Test the built binaries on their native platforms
+ test:
+ needs: build
+ strategy:
+ fail-fast: false
+ matrix:
+ include:
+ - os: macos-latest
+ node-arch: darwin-arm64
+ - os: macos-13
+ node-arch: darwin-x64
+ - os: ubuntu-latest
+ node-arch: linux-x64-gnu
+ - os: windows-latest
+ node-arch: win32-x64-msvc
+ runs-on: ${{ matrix.os }}
+ name: Test ${{ matrix.node-arch }}
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 20
+
+ - name: Install dependencies
+ working-directory: packages/node
+ run: npm ci
+
+ - name: Download artifacts
+ uses: actions/download-artifact@v4
+ with:
+ name: bindings-${{ matrix.node-arch }}
+ path: packages/node
+
+ - name: Build TypeScript
+ working-directory: packages/node
+ run: npx tsc
+
+ - name: Smoke test
+ working-directory: packages/node
+ run: |
+ node -e "
+ import('./dist/lib.js').then(async ({ LiteParse }) => {
+ const p = new LiteParse({ ocrEnabled: false, quiet: true });
+ console.log('Config:', JSON.stringify(p.getConfig()));
+ console.log('Native module loaded successfully');
+ });
+ "
+
+ # Publish to npm on version tags
+ publish:
+ if: startsWith(github.ref, 'refs/tags/v')
+ needs: test
+ runs-on: ubuntu-latest
+ name: Publish to npm
+ permissions:
+ contents: read
+ id-token: write
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 20
+ registry-url: https://registry.npmjs.org
+
+ - name: Install dependencies
+ working-directory: packages/node
+ run: npm ci
+
+ - name: Build TypeScript
+ working-directory: packages/node
+ run: npx tsc
+
+ - name: Download all artifacts
+ uses: actions/download-artifact@v4
+ with:
+ path: artifacts
+
+ - name: Prepare platform packages
+ working-directory: packages/node
+ run: |
+ # Create and publish platform-specific packages
+ declare -A PLATFORMS=(
+ ["darwin-arm64"]="aarch64-apple-darwin"
+ ["darwin-x64"]="x86_64-apple-darwin"
+ ["linux-x64-gnu"]="x86_64-unknown-linux-gnu"
+ ["linux-arm64-gnu"]="aarch64-unknown-linux-gnu"
+ ["win32-x64-msvc"]="x86_64-pc-windows-msvc"
+ )
+
+ for ARCH in "${!PLATFORMS[@]}"; do
+ PKG_NAME="@llamaindex/liteparse-${ARCH}"
+ PKG_DIR="npm/${ARCH}"
+ ARTIFACT_DIR="../../artifacts/bindings-${ARCH}"
+
+ if [ ! -d "$ARTIFACT_DIR" ]; then
+ echo "Skipping ${ARCH} - no artifacts found"
+ continue
+ fi
+
+ mkdir -p "$PKG_DIR"
+
+ # Copy native files
+ cp "$ARTIFACT_DIR"/*.node "$PKG_DIR/" 2>/dev/null || true
+ cp "$ARTIFACT_DIR"/*.dylib "$PKG_DIR/" 2>/dev/null || true
+ cp "$ARTIFACT_DIR"/*.so "$PKG_DIR/" 2>/dev/null || true
+ cp "$ARTIFACT_DIR"/*.dll "$PKG_DIR/" 2>/dev/null || true
+
+ # Determine OS and CPU for package.json
+ case "$ARCH" in
+ darwin-arm64) OS="darwin"; CPU="arm64" ;;
+ darwin-x64) OS="darwin"; CPU="x64" ;;
+ linux-x64-gnu) OS="linux"; CPU="x64" ;;
+ linux-arm64-gnu) OS="linux"; CPU="arm64" ;;
+ win32-x64-msvc) OS="win32"; CPU="x64" ;;
+ esac
+
+ VERSION=$(node -p "require('./package.json').version")
+
+ cat > "$PKG_DIR/package.json" << PKGJSON
+ {
+ "name": "${PKG_NAME}",
+ "version": "${VERSION}",
+ "os": ["${OS}"],
+ "cpu": ["${CPU}"],
+ "main": "liteparse.${ARCH}.node",
+ "files": ["*.node", "*.dylib", "*.so", "*.dll"],
+ "license": "Apache-2.0",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/run-llama/liteparse.git"
+ }
+ }
+ PKGJSON
+
+ echo "Created platform package: ${PKG_NAME}"
+ ls -la "$PKG_DIR/"
+ done
+
+ - name: Publish platform packages
+ working-directory: packages/node
+ env:
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
+ run: |
+ for dir in npm/*/; do
+ if [ -f "$dir/package.json" ]; then
+ echo "Publishing $(node -p "require('./${dir}/package.json').name")..."
+ cd "$dir"
+ npm publish --access public --provenance || true
+ cd ../../
+ fi
+ done
+
+ - name: Publish main package
+ working-directory: packages/node
+ env:
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
+ run: npm publish --access public --provenance
diff --git a/Cargo.lock b/Cargo.lock
index 9515e21..5189fd3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -353,6 +353,15 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6"
+[[package]]
+name = "convert_case"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca"
+dependencies = [
+ "unicode-segmentation",
+]
+
[[package]]
name = "core-foundation"
version = "0.9.4"
@@ -428,6 +437,16 @@ dependencies = [
"typenum",
]
+[[package]]
+name = "ctor"
+version = "0.2.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32a2785755761f3ddc1492979ce1e48d2c00d09311c39e4466429188f3dd6501"
+dependencies = [
+ "quote",
+ "syn",
+]
+
[[package]]
name = "deflate64"
version = "0.1.12"
@@ -1181,6 +1200,22 @@ dependencies = [
"tokio",
]
+[[package]]
+name = "liteparse-napi"
+version = "2.0.0"
+dependencies = [
+ "image",
+ "liteparse",
+ "liteparse-pdfium",
+ "liteparse-pdfium-sys",
+ "napi",
+ "napi-build",
+ "napi-derive",
+ "serde",
+ "serde_json",
+ "tokio",
+]
+
[[package]]
name = "liteparse-pdfium"
version = "1.0.0"
@@ -1299,6 +1334,66 @@ dependencies = [
"pxfm",
]
+[[package]]
+name = "napi"
+version = "2.16.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "55740c4ae1d8696773c78fdafd5d0e5fe9bc9f1b071c7ba493ba5c413a9184f3"
+dependencies = [
+ "bitflags",
+ "ctor",
+ "napi-derive",
+ "napi-sys",
+ "once_cell",
+ "serde",
+ "serde_json",
+ "tokio",
+]
+
+[[package]]
+name = "napi-build"
+version = "2.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c9c366d2c8c60b86fa632df75f745509b52f9128f91a6bad4c796e44abb505e1"
+
+[[package]]
+name = "napi-derive"
+version = "2.16.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7cbe2585d8ac223f7d34f13701434b9d5f4eb9c332cccce8dee57ea18ab8ab0c"
+dependencies = [
+ "cfg-if",
+ "convert_case",
+ "napi-derive-backend",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "napi-derive-backend"
+version = "1.0.75"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1639aaa9eeb76e91c6ae66da8ce3e89e921cd3885e99ec85f4abacae72fc91bf"
+dependencies = [
+ "convert_case",
+ "once_cell",
+ "proc-macro2",
+ "quote",
+ "regex",
+ "semver",
+ "syn",
+]
+
+[[package]]
+name = "napi-sys"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "427802e8ec3a734331fec1035594a210ce1ff4dc5bc1950530920ab717964ea3"
+dependencies = [
+ "libloading",
+]
+
[[package]]
name = "native-tls"
version = "0.2.18"
@@ -2406,6 +2501,12 @@ version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
+[[package]]
+name = "unicode-segmentation"
+version = "1.13.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9629274872b2bfaf8d66f5f15725007f635594914870f65218920345aa11aa8c"
+
[[package]]
name = "unicode-xid"
version = "0.2.6"
diff --git a/Cargo.toml b/Cargo.toml
index 76d02d1..81323e0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,6 +4,7 @@ members = [
"crates/pdfium-sys",
"crates/pdfium",
"crates/liteparse",
+ "crates/liteparse-napi",
]
[workspace.package]
diff --git a/crates/liteparse-napi/Cargo.toml b/crates/liteparse-napi/Cargo.toml
new file mode 100644
index 0000000..1ac8dad
--- /dev/null
+++ b/crates/liteparse-napi/Cargo.toml
@@ -0,0 +1,28 @@
+[package]
+name = "liteparse-napi"
+version = "2.0.0"
+edition.workspace = true
+license.workspace = true
+repository.workspace = true
+description = "Node.js native bindings for LiteParse"
+
+[lib]
+crate-type = ["cdylib"]
+
+[features]
+default = ["tesseract"]
+tesseract = ["liteparse/tesseract"]
+
+[dependencies]
+liteparse = { package = "liteparse", version = "2.0.0", path = "../liteparse", default-features = false }
+pdfium-sys = { package = "liteparse-pdfium-sys", version = "1.0.0", path = "../pdfium-sys" }
+napi = { version = "2", features = ["async", "serde-json", "napi9"] }
+napi-derive = "2"
+pdfium = { package = "liteparse-pdfium", version = "1.0.0", path = "../pdfium" }
+image = { version = "0.25", default-features = false, features = ["png"] }
+serde = { version = "1", features = ["derive"] }
+serde_json = "1"
+tokio = { version = "1", features = ["full"] }
+
+[build-dependencies]
+napi-build = "2"
diff --git a/crates/liteparse-napi/build.rs b/crates/liteparse-napi/build.rs
new file mode 100644
index 0000000..2f8c9fc
--- /dev/null
+++ b/crates/liteparse-napi/build.rs
@@ -0,0 +1,33 @@
+extern crate napi_build;
+
+use std::env;
+
+fn main() {
+ napi_build::setup();
+
+ let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
+
+ // Set rpath so the .node binary finds libpdfium next to itself at runtime.
+ // In the npm package, libpdfium is bundled alongside the .node file.
+ match target_os.as_str() {
+ "macos" => {
+ // @loader_path = directory containing the .node file
+ println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path");
+ }
+ "linux" => {
+ // $ORIGIN = directory containing the .node file
+ println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");
+ }
+ _ => {
+ // Windows: DLLs are found via PATH or same directory automatically
+ }
+ }
+
+ // Also add the build-time pdfium path so local dev builds work without
+ // copying libpdfium manually.
+ if let Ok(lib_path) = env::var("DEP_PDFIUM_LIB_PATH") {
+ if target_os == "macos" || target_os == "linux" {
+ println!("cargo:rustc-link-arg=-Wl,-rpath,{lib_path}");
+ }
+ }
+}
diff --git a/crates/liteparse-napi/src/lib.rs b/crates/liteparse-napi/src/lib.rs
new file mode 100644
index 0000000..3edf545
--- /dev/null
+++ b/crates/liteparse-napi/src/lib.rs
@@ -0,0 +1,118 @@
+use napi::bindgen_prelude::*;
+use napi_derive::napi;
+
+mod types;
+
+use types::{JsLiteParseConfig, JsParseResult, JsScreenshotResult};
+
+/// Main LiteParse parser class.
+#[napi]
+pub struct LiteParse {
+ inner: liteparse::parser::LiteParse,
+ config: liteparse::config::LiteParseConfig,
+}
+
+#[napi]
+impl LiteParse {
+ /// Create a new LiteParse instance with optional configuration.
+ /// Any fields not provided will use defaults.
+ #[napi(constructor)]
+ pub fn new(config: Option<JsLiteParseConfig>) -> Self {
+ let rust_config = config.map(|c| c.into_rust()).unwrap_or_default();
+ let inner = liteparse::parser::LiteParse::new(rust_config.clone());
+ Self {
+ inner,
+ config: rust_config,
+ }
+ }
+
+ /// Parse a document. Accepts a file path (string) or raw PDF bytes (Buffer).
+ #[napi]
+ pub async fn parse(&self, input: Either<String, Buffer>) -> Result<JsParseResult> {
+ use liteparse::types::PdfInput;
+
+ let pdf_input = match input {
+ Either::A(path) => PdfInput::Path(path),
+ Either::B(buf) => PdfInput::Bytes(buf.to_vec()),
+ };
+
+ let result = self
+ .inner
+ .parse_input(pdf_input)
+ .await
+ .map_err(|e| Error::from_reason(e.to_string()))?;
+
+ Ok(JsParseResult::from_rust(&result, &self.config))
+ }
+
+ /// Take screenshots of document pages. Returns PNG image buffers.
+ #[napi]
+ pub fn screenshot(
+ &self,
+ input: String,
+ page_numbers: Option<Vec<u32>>,
+ ) -> Result<Vec<JsScreenshotResult>> {
+ let dpi = self.config.dpi;
+ let lib = pdfium::Library::init();
+ let document = lib
+ .load_document(&input, self.config.password.as_deref())
+ .map_err(|e| Error::from_reason(e.to_string()))?;
+ let page_count = document.page_count() as u32;
+
+ let pages: Vec<u32> = match page_numbers {
+ Some(nums) => nums,
+ None => (1..=page_count).collect(),
+ };
+
+ let mut results = Vec::with_capacity(pages.len());
+ for page_num in pages {
+ if page_num < 1 || page_num > page_count {
+ return Err(Error::from_reason(format!(
+ "page {page_num} out of range (document has {page_count} pages)"
+ )));
+ }
+ let page = document
+ .page((page_num - 1) as i32)
+ .map_err(|e| Error::from_reason(e.to_string()))?;
+ let bitmap = page
+ .render(dpi)
+ .map_err(|e| Error::from_reason(e.to_string()))?;
+
+ let width = bitmap.width() as u32;
+ let height = bitmap.height() as u32;
+ let rgba = bitmap.to_rgba();
+
+ // Encode as PNG into a buffer
+ let mut png_buf: Vec<u8> = Vec::new();
+ let encoder = image::codecs::png::PngEncoder::new(&mut png_buf);
+ use image::ImageEncoder;
+ encoder
+ .write_image(&rgba, width, height, image::ColorType::Rgba8.into())
+ .map_err(|e| Error::from_reason(format!("PNG encode failed: {e}")))?;
+
+ results.push(JsScreenshotResult {
+ page_num,
+ width,
+ height,
+ image_buffer: png_buf.into(),
+ });
+ }
+
+ Ok(results)
+ }
+
+ /// Format a parse result according to the configured output format.
+ #[napi]
+ pub fn format(&self, result: JsParseResult) -> Result<String> {
+ let rust_result = result.to_rust();
+ self.inner
+ .format(&rust_result)
+ .map_err(|e| Error::from_reason(e.to_string()))
+ }
+
+ /// Get the current configuration.
+ #[napi(getter)]
+ pub fn config(&self) -> JsLiteParseConfig {
+ JsLiteParseConfig::from_rust(&self.config)
+ }
+}
diff --git a/crates/liteparse-napi/src/types.rs b/crates/liteparse-napi/src/types.rs
new file mode 100644
index 0000000..ef9e044
--- /dev/null
+++ b/crates/liteparse-napi/src/types.rs
@@ -0,0 +1,234 @@
+use napi_derive::napi;
+
+use liteparse::config::{LiteParseConfig, OutputFormat};
+use liteparse::parser::ParseResult;
+use liteparse::types::{ParsedPage, TextItem};
+
+// ---------------------------------------------------------------------------
+// Config
+// ---------------------------------------------------------------------------
+
+#[napi(object)]
+#[derive(Clone)]
+pub struct JsLiteParseConfig {
+ /// OCR language code (e.g., "eng", "fra").
+ pub ocr_language: Option<String>,
+ /// Whether OCR is enabled.
+ pub ocr_enabled: Option<bool>,
+ /// HTTP OCR server URL. If set, uses HTTP OCR instead of Tesseract.
+ pub ocr_server_url: Option<String>,
+ /// Path to tessdata directory for Tesseract.
+ pub tessdata_path: Option<String>,
+ /// Maximum number of pages to parse.
+ pub max_pages: Option<u32>,
+ /// Specific pages to parse (e.g., "1-5,10,15-20").
+ pub target_pages: Option<String>,
+ /// DPI for rendering pages (used for OCR and screenshots).
+ pub dpi: Option<f64>,
+ /// Output format: "json" or "text".
+ pub output_format: Option<String>,
+ /// Keep very small text that would normally be filtered out.
+ pub preserve_very_small_text: Option<bool>,
+ /// Password for encrypted/protected documents.
+ pub password: Option<String>,
+ /// Suppress progress output.
+ pub quiet: Option<bool>,
+}
+
+impl JsLiteParseConfig {
+ pub fn into_rust(self) -> LiteParseConfig {
+ 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 let Some(v) = self.ocr_server_url {
+ cfg.ocr_server_url = Some(v);
+ }
+ if let Some(v) = self.tessdata_path {
+ cfg.tessdata_path = Some(v);
+ }
+ if let Some(v) = self.max_pages {
+ cfg.max_pages = v as usize;
+ }
+ if let Some(v) = self.target_pages {
+ cfg.target_pages = Some(v);
+ }
+ if let Some(v) = self.dpi {
+ cfg.dpi = v as f32;
+ }
+ if let Some(v) = self.output_format {
+ cfg.output_format = match v.as_str() {
+ "text" => OutputFormat::Text,
+ _ => OutputFormat::Json,
+ };
+ }
+ if let Some(v) = self.preserve_very_small_text {
+ cfg.preserve_very_small_text = v;
+ }
+ if let Some(v) = self.password {
+ cfg.password = Some(v);
+ }
+ if let Some(v) = self.quiet {
+ cfg.quiet = v;
+ }
+ cfg
+ }
+
+ pub fn from_rust(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 as u32),
+ target_pages: cfg.target_pages.clone(),
+ dpi: Some(cfg.dpi as f64),
+ output_format: Some(match cfg.output_format {
+ OutputFormat::Json => "json".to_string(),
+ OutputFormat::Text => "text".to_string(),
+ }),
+ preserve_very_small_text: Some(cfg.preserve_very_small_text),
+ password: cfg.password.clone(),
+ quiet: Some(cfg.quiet),
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// TextItem
+// ---------------------------------------------------------------------------
+
+#[napi(object)]
+#[derive(Clone)]
+pub struct JsTextItem {
+ pub text: String,
+ pub x: f64,
+ pub y: f64,
+ pub width: f64,
+ pub height: f64,
+ pub font_name: Option<String>,
+ pub font_size: Option<f64>,
+ pub confidence: Option<f64>,
+}
+
+impl JsTextItem {
+ pub fn from_rust(item: &TextItem) -> Self {
+ Self {
+ text: item.text.clone(),
+ x: item.x as f64,
+ y: item.y as f64,
+ width: item.width as f64,
+ height: item.height as f64,
+ font_name: item.font_name.clone(),
+ font_size: item.font_size.map(|v| v as f64),
+ confidence: item.confidence.map(|v| v as f64).or(Some(1.0)),
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// ParsedPage
+// ---------------------------------------------------------------------------
+
+#[napi(object)]
+#[derive(Clone)]
+pub struct JsParsedPage {
+ pub page_num: u32,
+ pub width: f64,
+ pub height: f64,
+ pub text: String,
+ pub text_items: Vec<JsTextItem>,
+}
+
+impl JsParsedPage {
+ pub fn from_rust(page: &ParsedPage) -> Self {
+ Self {
+ page_num: page.page_number as u32,
+ width: page.page_width as f64,
+ height: page.page_height as f64,
+ text: page.text.clone(),
+ text_items: page.text_items.iter().map(JsTextItem::from_rust).collect(),
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// ParseResult
+// ---------------------------------------------------------------------------
+
+#[napi(object)]
+#[derive(Clone)]
+pub struct JsParseResult {
+ pub pages: Vec<JsParsedPage>,
+ pub text: String,
+}
+
+// ---------------------------------------------------------------------------
+// ScreenshotResult
+// ---------------------------------------------------------------------------
+
+#[napi(object)]
+#[derive(Clone)]
+pub struct JsScreenshotResult {
+ pub page_num: u32,
+ pub width: u32,
+ pub height: u32,
+ pub image_buffer: napi::bindgen_prelude::Buffer,
+}
+
+impl JsParseResult {
+ pub fn from_rust(result: &ParseResult, _config: &LiteParseConfig) -> Self {
+ Self {
+ pages: result
+ .pages
+ .iter()
+ .map(JsParsedPage::from_rust)
+ .collect(),
+ text: result.text.clone(),
+ }
+ }
+
+ pub fn to_rust(&self) -> ParseResult {
+ ParseResult {
+ pages: self
+ .pages
+ .iter()
+ .map(|p| ParsedPage {
+ page_number: p.page_num as usize,
+ page_width: p.width as f32,
+ page_height: p.height as f32,
+ text: p.text.clone(),
+ text_items: p
+ .text_items
+ .iter()
+ .map(|item| TextItem {
+ text: item.text.clone(),
+ x: item.x as f32,
+ y: item.y as f32,
+ width: item.width as f32,
+ height: item.height as f32,
+ rotation: 0.0,
+ font_name: item.font_name.clone(),
+ font_size: item.font_size.map(|v| v as f32),
+ font_height: None,
+ font_ascent: None,
+ font_descent: None,
+ font_weight: None,
+ font_flags: None,
+ text_width: None,
+ font_is_buggy: false,
+ mcid: None,
+ fill_color: None,
+ stroke_color: None,
+ confidence: item.confidence.map(|v| v as f32),
+ })
+ .collect(),
+ })
+ .collect(),
+ text: self.text.clone(),
+ }
+ }
+}
diff --git a/crates/liteparse/Cargo.toml b/crates/liteparse/Cargo.toml
index 7cb9388..12ee2af 100644
--- a/crates/liteparse/Cargo.toml
+++ b/crates/liteparse/Cargo.toml
@@ -7,7 +7,7 @@ repository.workspace = true
description = "Fast, lightweight PDF and document parsing with spatial text extraction"
[lib]
-name = "liteparse_rs"
+name = "liteparse"
[[bin]]
name = "lit"
diff --git a/crates/liteparse/src/extract.rs b/crates/liteparse/src/extract.rs
index 7cfe67d..468414a 100644
--- a/crates/liteparse/src/extract.rs
+++ b/crates/liteparse/src/extract.rs
@@ -1,4 +1,4 @@
-use crate::types::{Page as LitePage, TextItem};
+use crate::types::{Page as LitePage, PdfInput, TextItem};
use pdfium::{Font, FontType, Library, Page, RectF, TextPage};
/// Extract pages from a PDF file and return them as structured data.
@@ -85,6 +85,60 @@ pub fn extract_pages_filtered(
Ok(pages)
}
+/// Extract pages from a `PdfInput` (file path or bytes) with filtering.
+///
+/// Returns `(pages, pdf_data)` where `pdf_data` is the owned byte buffer when
+/// input was `PdfInput::Bytes`. The caller should keep `pdf_data` alive as long
+/// as any pdfium objects derived from it are in use. This is needed because
+/// pdfium's memory-loaded documents require the buffer to outlive the document.
+pub fn extract_pages_from_input(
+ input: &PdfInput,
+ target_pages: Option<&[u32]>,
+ max_pages: usize,
+ password: Option<&str>,
+) -> Result<Vec<LitePage>, Box<dyn std::error::Error>> {
+ let lib = Library::init();
+ let document = match input {
+ PdfInput::Path(path) => lib.load_document(path, password)?,
+ PdfInput::Bytes(data) => lib.load_document_from_bytes(data, password)?,
+ };
+ let page_count = document.page_count();
+ let mut pages = Vec::new();
+
+ for page_index in 0..page_count {
+ let page_number = page_index as u32 + 1;
+
+ if let Some(targets) = target_pages
+ && !targets.contains(&page_number)
+ {
+ continue;
+ }
+
+ if pages.len() >= max_pages {
+ break;
+ }
+
+ let page = document.page(page_index)?;
+ let text_page = page.text()?;
+ let view_box = page.view_box().unwrap_or(RectF {
+ left: 0.0,
+ top: page.height(),
+ right: page.width(),
+ bottom: 0.0,
+ });
+ let text_items = extract_page_text_items(&page, &text_page, &view_box)?;
+
+ pages.push(LitePage {
+ page_number: page_number as usize,
+ page_width: page.width(),
+ page_height: page.height(),
+ text_items,
+ });
+ }
+
+ Ok(pages)
+}
+
/// Extract raw text items and print each page as a JSON-line object to stdout.
pub fn extract(pdf_path: &str, page_num: Option<u32>) -> Result<(), Box<dyn std::error::Error>> {
let pages = extract_pages(pdf_path, page_num)?;
diff --git a/crates/liteparse/src/main.rs b/crates/liteparse/src/main.rs
index b8ca802..1fdb1ea 100644
--- a/crates/liteparse/src/main.rs
+++ b/crates/liteparse/src/main.rs
@@ -1,9 +1,9 @@
use clap::{Args, Parser, Subcommand};
-use liteparse_rs::config::{LiteParseConfig, OutputFormat};
-use liteparse_rs::conversion;
-use liteparse_rs::extract;
-use liteparse_rs::parser::LiteParse;
-use liteparse_rs::render;
+use liteparse::config::{LiteParseConfig, OutputFormat};
+use liteparse::conversion;
+use liteparse::extract;
+use liteparse::parser::LiteParse;
+use liteparse::render;
#[derive(Parser, Debug)]
#[command(
@@ -225,7 +225,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let target_pages = cmd
.target_pages
.as_ref()
- .map(|s| liteparse_rs::config::parse_target_pages(s))
+ .map(|s| liteparse::config::parse_target_pages(s))
.transpose()
.map_err(|e| format!("invalid --target-pages: {}", e))?;
diff --git a/crates/liteparse/src/ocr_merge.rs b/crates/liteparse/src/ocr_merge.rs
index 90435fd..a9f3ee4 100644
--- a/crates/liteparse/src/ocr_merge.rs
+++ b/crates/liteparse/src/ocr_merge.rs
@@ -1,5 +1,5 @@
use crate::ocr::{OcrEngine, OcrOptions};
-use crate::types::{Page, TextItem};
+use crate::types::{Page, PdfInput, TextItem};
use image::{ImageBuffer, Rgba};
use pdfium::Library;
@@ -13,9 +13,30 @@ pub fn ocr_and_merge_pages(
dpi: f32,
ocr_engine: &dyn OcrEngine,
ocr_language: &str,
+) -> Result<(), Box<dyn std::error::Error>> {
+ ocr_and_merge_pages_from_input(
+ pages,
+ &PdfInput::Path(pdf_path.to_string()),
+ dpi,
+ ocr_engine,
+ ocr_language,
+ )
+}
+
+/// 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(
+ 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 = lib.load_document(pdf_path, None)?;
+ let document = match input {
+ PdfInput::Path(path) => lib.load_document(path, None)?,
+ PdfInput::Bytes(data) => lib.load_document_from_bytes(data, None)?,
+ };
for page in pages.iter_mut() {
let text_length: usize = page.text_items.iter().map(|item| item.text.len()).sum();
diff --git a/crates/liteparse/src/parser.rs b/crates/liteparse/src/parser.rs
index 9089b23..e42363c 100644
--- a/crates/liteparse/src/parser.rs
+++ b/crates/liteparse/src/parser.rs
@@ -8,7 +8,7 @@ use crate::ocr::tesseract::TesseractOcrEngine;
use crate::ocr_merge;
use crate::output::{json, text};
use crate::projection;
-use crate::types::ParsedPage;
+use crate::types::{ParsedPage, PdfInput};
/// Result of parsing a document.
pub struct ParseResult {
@@ -28,23 +28,34 @@ impl LiteParse {
Self { config }
}
- /// Parse a document file, returning structured results.
+ /// 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).
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) {
+ PdfInput::Path(input.to_string())
+ } else {
+ let converted = conversion::convert_to_pdf(input, self.config.password.as_deref())
+ .await?;
+ PdfInput::Path(converted.pdf_path)
+ };
+
+ self.parse_input(pdf_input).await
+ }
+
+ /// Parse a document from either a file path or raw bytes.
+ ///
+ /// Use `PdfInput::Path` for files on disk or `PdfInput::Bytes` for
+ /// in-memory PDF data (e.g. from a network response or Node.js Buffer).
+ pub async fn parse_input(&self, input: PdfInput) -> Result<ParseResult, Box<dyn std::error::Error>> {
let log = |msg: &str| {
if !self.config.quiet {
eprintln!("{}", msg);
}
};
- // Resolve input to a PDF path (convert if needed)
- let pdf_path = if conversion::is_pdf(input) {
- input.to_string()
- } else {
- conversion::convert_to_pdf(input, self.config.password.as_deref())
- .await?
- .pdf_path
- };
-
let t0 = std::time::Instant::now();
// Determine which pages to extract
@@ -57,8 +68,8 @@ impl LiteParse {
.map_err(|e| format!("invalid --target-pages: {}", e))?;
// Extract text items from PDF pages
- let mut pages = extract::extract_pages_filtered(
- &pdf_path,
+ let mut pages = extract::extract_pages_from_input(
+ &input,
target_pages.as_deref(),
self.config.max_pages,
self.config.password.as_deref(),
@@ -84,9 +95,9 @@ impl LiteParse {
return Err("OCR enabled but no --ocr-server-url provided and tesseract feature is disabled".into());
}
};
- ocr_merge::ocr_and_merge_pages(
+ ocr_merge::ocr_and_merge_pages_from_input(
&mut pages,
- &pdf_path,
+ &input,
self.config.dpi,
engine.as_ref(),
&self.config.ocr_language,
diff --git a/crates/liteparse/src/types.rs b/crates/liteparse/src/types.rs
index 5f01b28..63cd002 100644
--- a/crates/liteparse/src/types.rs
+++ b/crates/liteparse/src/types.rs
@@ -19,6 +19,18 @@ pub enum InputType {
Buffer(Vec<u8>),
}
+/// Input source for PDF parsing — either a file path or raw bytes.
+///
+/// When using `Bytes`, the data is kept alive alongside the pdfium `Document`
+/// (pdfium requires the buffer to outlive the document handle).
+#[derive(Debug, Clone)]
+pub enum PdfInput {
+ /// Path to a PDF file on disk.
+ Path(String),
+ /// Raw PDF bytes (e.g. from a network response or in-memory buffer).
+ Bytes(Vec<u8>),
+}
+
/// Represents a single text item extracted from a PDF page,
/// including its content, position, size, rotation, and font metadata.
#[derive(Debug, Clone, Serialize)]
diff --git a/packages/node/.gitignore b/packages/node/.gitignore
new file mode 100644
index 0000000..ac3c62e
--- /dev/null
+++ b/packages/node/.gitignore
@@ -0,0 +1,7 @@
+dist/
+node_modules/
+*.node
+*.dylib
+*.so
+*.dll
+npm/
diff --git a/packages/node/native.d.ts b/packages/node/native.d.ts
new file mode 100644
index 0000000..a85fc79
--- /dev/null
+++ b/packages/node/native.d.ts
@@ -0,0 +1,72 @@
+/* tslint:disable */
+/* eslint-disable */
+
+/* auto-generated by NAPI-RS */
+
+export interface JsLiteParseConfig {
+ /** OCR language code (e.g., "eng", "fra"). */
+ ocrLanguage?: string
+ /** Whether OCR is enabled. */
+ ocrEnabled?: boolean
+ /** HTTP OCR server URL. If set, uses HTTP OCR instead of Tesseract. */
+ ocrServerUrl?: string
+ /** Path to tessdata directory for Tesseract. */
+ tessdataPath?: string
+ /** Maximum number of pages to parse. */
+ maxPages?: number
+ /** Specific pages to parse (e.g., "1-5,10,15-20"). */
+ targetPages?: string
+ /** DPI for rendering pages (used for OCR and screenshots). */
+ dpi?: number
+ /** Output format: "json" or "text". */
+ outputFormat?: string
+ /** Keep very small text that would normally be filtered out. */
+ preserveVerySmallText?: boolean
+ /** Password for encrypted/protected documents. */
+ password?: string
+ /** Suppress progress output. */
+ quiet?: boolean
+}
+export interface JsTextItem {
+ text: string
+ x: number
+ y: number
+ width: number
+ height: number
+ fontName?: string
+ fontSize?: number
+ confidence?: number
+}
+export interface JsParsedPage {
+ pageNum: number
+ width: number
+ height: number
+ text: string
+ textItems: Array<JsTextItem>
+}
+export interface JsParseResult {
+ pages: Array<JsParsedPage>
+ text: string
+}
+export interface JsScreenshotResult {
+ pageNum: number
+ width: number
+ height: number
+ imageBuffer: Buffer
+}
+/** Main LiteParse parser class. */
+export declare class LiteParse {
+ /**
+ * Create a new LiteParse instance with optional configuration.
+ * Any fields not provided will use defaults.
+ */
+ constructor(config?: JsLiteParseConfig | undefined | null)
+ /** Parse a document. Accepts a file path (string) or raw PDF bytes (Buffer). */
+ parse(input: string | Buffer): Promise<JsParseResult>
+ /** Take screenshots of document pages. Returns PNG image buffers. */
+ screenshot(input: string, pageNumbers?: Array<number> | undefined | null): Array<JsScreenshotResult>
+ /** Format a parse result according to the configured output format. */
+ format(result: JsParseResult): string
+ /** Get the current configuration. */
+ get config(): JsLiteParseConfig
+}
diff --git a/packages/node/package-lock.json b/packages/node/package-lock.json
new file mode 100644
index 0000000..df9bc7e
--- /dev/null
+++ b/packages/node/package-lock.json
@@ -0,0 +1,89 @@
+{
+ "name": "@llamaindex/liteparse",
+ "version": "2.0.0",
+ "lockfileVersion": 3,
+ "requires": true,
+ "packages": {
+ "": {
+ "name": "@llamaindex/liteparse",
+ "version": "2.0.0",
+ "license": "Apache-2.0",
+ "dependencies": {
+ "commander": "^12.0.0"
+ },
+ "bin": {
+ "lit": "dist/cli.js",
+ "liteparse": "dist/cli.js"
+ },
+ "devDependencies": {
+ "@napi-rs/cli": "^2.18.4",
+ "@types/node": "^22.0.0",
+ "typescript": "~5.9.2"
+ },
+ "engines": {
+ "node": ">=18.0.0"
+ },
+ "optionalDependencies": {
+ "@llamaindex/liteparse-darwin-arm64": "2.0.0",
+ "@llamaindex/liteparse-darwin-x64": "2.0.0",
+ "@llamaindex/liteparse-linux-arm64-gnu": "2.0.0",
+ "@llamaindex/liteparse-linux-arm64-musl": "2.0.0",
+ "@llamaindex/liteparse-linux-x64-gnu": "2.0.0",
+ "@llamaindex/liteparse-linux-x64-musl": "2.0.0",
+ "@llamaindex/liteparse-win32-x64-msvc": "2.0.0"
+ }
+ },
+ "node_modules/@napi-rs/cli": {
+ "version": "2.18.4",
+ "resolved": "https://registry.npmjs.org/@napi-rs/cli/-/cli-2.18.4.tgz",
+ "integrity": "sha512-SgJeA4df9DE2iAEpr3M2H0OKl/yjtg1BnRI5/JyowS71tUWhrfSu2LT0V3vlHET+g1hBVlrO60PmEXwUEKp8Mg==",
+ "dev": true,
+ "bin": {
+ "napi": "scripts/index.js"
+ },
+ "engines": {
+ "node": ">= 10"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/Brooooooklyn"
+ }
+ },
+ "node_modules/@types/node": {
+ "version": "22.19.19",
+ "resolved": "https://registry.npmjs.org/@types/node/-/node-22.19.19.tgz",
+ "integrity": "sha512-dyh/xO2Fh5bYrfWaaqGrRQQGkNdmYw6AmaAUvYeUMNTWQtvb796ikLdmTchRmOlOiIJ1TDXfWgVx1QkUlQ6Hew==",
+ "dev": true,
+ "dependencies": {
+ "undici-types": "~6.21.0"
+ }
+ },
+ "node_modules/commander": {
+ "version": "12.1.0",
+ "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz",
+ "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==",
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/typescript": {
+ "version": "5.9.3",
+ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
+ "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
+ "dev": true,
+ "bin": {
+ "tsc": "bin/tsc",
+ "tsserver": "bin/tsserver"
+ },
+ "engines": {
+ "node": ">=14.17"
+ }
+ },
+ "node_modules/undici-types": {
+ "version": "6.21.0",
+ "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
+ "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
+ "dev": true
+ }
+ }
+}
diff --git a/packages/node/package.json b/packages/node/package.json
new file mode 100644
index 0000000..d49dce8
--- /dev/null
+++ b/packages/node/package.json
@@ -0,0 +1,80 @@
+{
+ "name": "@llamaindex/liteparse",
+ "version": "2.0.0",
+ "description": "Fast, lightweight PDF and document parsing with spatial text extraction",
+ "type": "module",
+ "main": "./dist/lib.js",
+ "types": "./dist/lib.d.ts",
+ "bin": {
+ "lit": "./dist/cli.js",
+ "liteparse": "./dist/cli.js"
+ },
+ "exports": {
+ ".": {
+ "types": "./dist/lib.d.ts",
+ "import": "./dist/lib.js"
+ },
+ "./package.json": "./package.json"
+ },
+ "napi": {
+ "name": "liteparse",
+ "triples": {
+ "defaults": true,
+ "additional": [
+ "aarch64-apple-darwin",
+ "aarch64-unknown-linux-gnu",
+ "aarch64-unknown-linux-musl"
+ ]
+ }
+ },
+ "files": [
+ "dist",
+ "*.node",
+ "*.dylib",
+ "*.so",
+ "*.dll",
+ "README.md",
+ "LICENSE"
+ ],
+ "scripts": {
+ "build:rs": "napi build --cargo-cwd ../../crates/liteparse-napi --platform --release --js false --dts native.d.ts .",
+ "build:pdfium": "bash scripts/copy-pdfium.sh .",
+ "build:ts": "tsc",
+ "build": "npm run build:rs && npm run build:pdfium && npm run build:ts",
+ "prepublishOnly": "npm run build"
+ },
+ "dependencies": {
+ "commander": "^12.0.0"
+ },
+ "devDependencies": {
+ "@napi-rs/cli": "^2.18.4",
+ "@types/node": "^22.0.0",
+ "typescript": "~5.9.2"
+ },
+ "optionalDependencies": {
+ "@llamaindex/liteparse-darwin-arm64": "2.0.0",
+ "@llamaindex/liteparse-darwin-x64": "2.0.0",
+ "@llamaindex/liteparse-linux-arm64-gnu": "2.0.0",
+ "@llamaindex/liteparse-linux-arm64-musl": "2.0.0",
+ "@llamaindex/liteparse-linux-x64-gnu": "2.0.0",
+ "@llamaindex/liteparse-linux-x64-musl": "2.0.0",
+ "@llamaindex/liteparse-win32-x64-msvc": "2.0.0"
+ },
+ "engines": {
+ "node": ">=18.0.0"
+ },
+ "keywords": [
+ "pdf",
+ "parser",
+ "ocr",
+ "text-extraction",
+ "pdf-to-text",
+ "document-parsing"
+ ],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/run-llama/liteparse.git"
+ },
+ "author": "LlamaIndex",
+ "license": "Apache-2.0"
+}
diff --git a/packages/node/scripts/copy-pdfium.sh b/packages/node/scripts/copy-pdfium.sh
new file mode 100755
index 0000000..8fe1149
--- /dev/null
+++ b/packages/node/scripts/copy-pdfium.sh
@@ -0,0 +1,68 @@
+#!/usr/bin/env bash
+#
+# Copy the pdfium shared library next to the .node file so it can be found
+# at runtime via @loader_path (macOS) or $ORIGIN (Linux).
+#
+# Usage: ./scripts/copy-pdfium.sh [output-dir]
+# output-dir: directory containing the .node file (default: .)
+#
+# The script auto-detects the pdfium library location from:
+# 1. PDFIUM_LIB_PATH env var (set by CI or user)
+# 2. The pdfium-sys build cache (~/.cache/pdfium-rs or ~/Library/Caches/pdfium-rs)
+
+set -euo pipefail
+
+OUTPUT_DIR="${1:-.}"
+
+# Determine OS and library filename
+case "$(uname -s)" in
+ Darwin*) DYLIB="libpdfium.dylib" ;;
+ Linux*) DYLIB="libpdfium.so" ;;
+ MINGW*|MSYS*|CYGWIN*) DYLIB="pdfium.dll" ;;
+ *) echo "Unsupported OS: $(uname -s)" >&2; exit 1 ;;
+esac
+
+# Find the pdfium library
+find_pdfium() {
+ # 1. Explicit env var
+ if [ -n "${PDFIUM_LIB_PATH:-}" ] && [ -f "${PDFIUM_LIB_PATH}/${DYLIB}" ]; then
+ echo "${PDFIUM_LIB_PATH}/${DYLIB}"
+ return
+ fi
+
+ # 2. Vendor directory
+ local vendor="$(dirname "$0")/../../vendor/pdfium/release/lib/${DYLIB}"
+ if [ -f "$vendor" ]; then
+ echo "$vendor"
+ return
+ fi
+
+ # 3. Build cache
+ local cache_base
+ case "$(uname -s)" in
+ Darwin*) cache_base="$HOME/Library/Caches/pdfium-rs" ;;
+ *) cache_base="${XDG_CACHE_HOME:-$HOME/.cache}/pdfium-rs" ;;
+ esac
+
+ # Find most recent cached pdfium
+ if [ -d "$cache_base" ]; then
+ local found
+ found=$(find "$cache_base" -name "$DYLIB" -type f 2>/dev/null | head -1)
+ if [ -n "$found" ]; then
+ echo "$found"
+ return
+ fi
+ fi
+
+ echo ""
+}
+
+PDFIUM_PATH=$(find_pdfium)
+
+if [ -z "$PDFIUM_PATH" ]; then
+ echo "Error: Could not find ${DYLIB}. Set PDFIUM_LIB_PATH to the directory containing it." >&2
+ exit 1
+fi
+
+cp "$PDFIUM_PATH" "${OUTPUT_DIR}/${DYLIB}"
+echo "Copied ${PDFIUM_PATH} -> ${OUTPUT_DIR}/${DYLIB}"
diff --git a/packages/node/src/cli.ts b/packages/node/src/cli.ts
new file mode 100644
index 0000000..f0ac9ca
--- /dev/null
+++ b/packages/node/src/cli.ts
@@ -0,0 +1,93 @@
+#!/usr/bin/env node
+
+import { program } from "commander";
+import { LiteParse, type LiteParseConfig } from "./lib.js";
+import { readFileSync } from "node:fs";
+import { writeFileSync } from "node:fs";
+
+program
+ .name("liteparse")
+ .description("Fast, lightweight PDF and document parsing")
+ .version("2.0.0");
+
+program
+ .command("parse")
+ .description("Parse a document and extract text")
+ .argument("<file>", "Path to the document file")
+ .option("-o, --output <file>", "Output file path")
+ .option("--format <format>", 'Output format: json|text (default: "text")')
+ .option("--ocr-server-url <url>", "HTTP OCR server URL")
+ .option("--no-ocr", "Disable OCR")
+ .option("--ocr-language <lang>", "OCR language (default: eng)")
+ .option("--max-pages <n>", "Max pages to parse", parseInt)
+ .option(
+ "--target-pages <pages>",
+ 'Pages to parse (e.g., "1-5,10,15-20")',
+ )
+ .option("--dpi <dpi>", "Rendering DPI", parseFloat)
+ .option("--preserve-small-text", "Keep very small text")
+ .option("--password <password>", "Password for encrypted documents")
+ .option("--config <file>", "JSON config file path")
+ .option("-q, --quiet", "Suppress progress output")
+ .action(async (file: string, opts: Record<string, unknown>) => {
+ try {
+ const config: Partial<LiteParseConfig> = {};
+
+ // Load config file if provided
+ if (opts.config) {
+ const fileConfig = JSON.parse(
+ readFileSync(opts.config as string, "utf-8"),
+ );
+ Object.assign(config, fileConfig);
+ }
+
+ // CLI options override config file
+ if (opts.format) config.outputFormat = opts.format as "json" | "text";
+ if (opts.ocrServerUrl)
+ config.ocrServerUrl = opts.ocrServerUrl as string;
+ if (opts.ocr === false) config.ocrEnabled = false;
+ if (opts.ocrLanguage) config.ocrLanguage = opts.ocrLanguage as string;
+ if (opts.maxPages) config.maxPages = opts.maxPages as number;
+ if (opts.targetPages) config.targetPages = opts.targetPages as string;
+ if (opts.dpi) config.dpi = opts.dpi as number;
+ if (opts.preserveSmallText) config.preserveVerySmallText = true;
+ if (opts.password) config.password = opts.password as string;
+ if (opts.quiet) config.quiet = true;
+
+ // Default CLI output to text (library defaults to json)
+ if (!config.outputFormat) config.outputFormat = "text";
+
+ const parser = new LiteParse(config);
+ const result = await parser.parse(file);
+
+ const output =
+ config.outputFormat === "json"
+ ? JSON.stringify(
+ {
+ pages: result.pages.map((p) => ({
+ page: p.pageNum,
+ width: p.width,
+ height: p.height,
+ text: p.text,
+ textItems: p.textItems,
+ })),
+ },
+ null,
+ 2,
+ )
+ : result.text;
+
+ if (opts.output) {
+ writeFileSync(opts.output as string, output, "utf-8");
+ } else {
+ process.stdout.write(output);
+ }
+ } catch (err) {
+ console.error(
+ `Error: ${err instanceof Error ? err.message : String(err)}`,
+ );
+ process.exit(1);
+ }
+ });
+
+program.parse(process.argv);
diff --git a/packages/node/src/lib.ts b/packages/node/src/lib.ts
new file mode 100644
index 0000000..49e3a8f
--- /dev/null
+++ b/packages/node/src/lib.ts
@@ -0,0 +1,155 @@
+import {
+ native,
+ type LiteParseNative,
+ type LiteParseNativeConfig,
+ type NativeParseResult,
+ type NativeParsedPage,
+ type NativeTextItem,
+} from "./native.js";
+
+// ---------------------------------------------------------------------------
+// Public types — match the existing TypeScript API
+// ---------------------------------------------------------------------------
+
+export type LiteParseInput = string | Buffer | Uint8Array;
+export type OutputFormat = "json" | "text";
+
+export interface LiteParseConfig {
+ ocrLanguage: string;
+ ocrEnabled: boolean;
+ ocrServerUrl?: string;
+ tessdataPath?: string;
+ maxPages: number;
+ targetPages?: string;
+ dpi: number;
+ outputFormat: OutputFormat;
+ preserveVerySmallText: boolean;
+ password?: string;
+ quiet: boolean;
+}
+
+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 interface ScreenshotResult {
+ pageNum: number;
+ width: number;
+ height: number;
+ imageBuffer: Buffer;
+}
+
+// ---------------------------------------------------------------------------
+// LiteParse class
+// ---------------------------------------------------------------------------
+
+export class LiteParse {
+ private _native: LiteParseNative;
+ private _config: LiteParseConfig;
+
+ constructor(userConfig: Partial<LiteParseConfig> = {}) {
+ const nativeConfig: LiteParseNativeConfig = {
+ ocrLanguage: userConfig.ocrLanguage,
+ ocrEnabled: userConfig.ocrEnabled,
+ ocrServerUrl: userConfig.ocrServerUrl,
+ tessdataPath: userConfig.tessdataPath,
+ maxPages: userConfig.maxPages,
+ targetPages: userConfig.targetPages,
+ dpi: userConfig.dpi,
+ outputFormat: userConfig.outputFormat,
+ preserveVerySmallText: userConfig.preserveVerySmallText,
+ password: userConfig.password,
+ quiet: userConfig.quiet,
+ };
+
+ this._native = new native.LiteParse(nativeConfig);
+
+ // Read back the resolved config from the native side
+ const resolved = this._native.config;
+ this._config = {
+ ocrLanguage: resolved.ocrLanguage ?? "eng",
+ ocrEnabled: resolved.ocrEnabled ?? true,
+ ocrServerUrl: resolved.ocrServerUrl ?? undefined,
+ tessdataPath: resolved.tessdataPath ?? undefined,
+ maxPages: resolved.maxPages ?? 1000,
+ targetPages: resolved.targetPages ?? undefined,
+ dpi: resolved.dpi ?? 150,
+ outputFormat: (resolved.outputFormat as OutputFormat) ?? "json",
+ preserveVerySmallText: resolved.preserveVerySmallText ?? false,
+ password: resolved.password ?? undefined,
+ quiet: resolved.quiet ?? false,
+ };
+ }
+
+ async parse(input: LiteParseInput): Promise<ParseResult> {
+ // Convert Uint8Array to Buffer for the native side
+ const nativeInput =
+ typeof input === "string" ? input : Buffer.from(input);
+ const result: NativeParseResult = await this._native.parse(nativeInput);
+ return {
+ pages: result.pages.map(toPage),
+ text: result.text,
+ };
+ }
+
+ screenshot(
+ input: string,
+ pageNumbers?: number[],
+ ): ScreenshotResult[] {
+ return this._native.screenshot(input, pageNumbers ?? null).map((r) => ({
+ pageNum: r.pageNum,
+ width: r.width,
+ height: r.height,
+ imageBuffer: r.imageBuffer,
+ }));
+ }
+
+ getConfig(): LiteParseConfig {
+ return { ...this._config };
+ }
+}
+
+function toPage(p: NativeParsedPage): ParsedPage {
+ return {
+ pageNum: p.pageNum,
+ width: p.width,
+ height: p.height,
+ text: p.text,
+ textItems: p.textItems.map(toTextItem),
+ };
+}
+
+function toTextItem(item: NativeTextItem): TextItem {
+ return {
+ text: item.text,
+ x: item.x,
+ y: item.y,
+ width: item.width,
+ height: item.height,
+ fontName: item.fontName,
+ fontSize: item.fontSize,
+ confidence: item.confidence,
+ };
+}
+
+export default LiteParse;
diff --git a/packages/node/src/native.ts b/packages/node/src/native.ts
new file mode 100644
index 0000000..8db9012
--- /dev/null
+++ b/packages/node/src/native.ts
@@ -0,0 +1,132 @@
+// Native binary loader - tries platform-specific packages, falls back to local .node file.
+//
+// In production: the correct @llamaindex/liteparse-<platform> optional dependency
+// provides the .node binary. During development: `napi build` places it alongside package.json.
+
+import { createRequire } from "node:module";
+import { join, dirname } from "node:path";
+import { fileURLToPath } from "node:url";
+
+const require = createRequire(import.meta.url);
+const __dirname = dirname(fileURLToPath(import.meta.url));
+
+interface NativeBindings {
+ LiteParse: new (config?: LiteParseNativeConfig) => LiteParseNative;
+}
+
+export interface LiteParseNativeConfig {
+ ocrLanguage?: string;
+ ocrEnabled?: boolean;
+ ocrServerUrl?: string;
+ tessdataPath?: string;
+ maxPages?: number;
+ targetPages?: string;
+ dpi?: number;
+ outputFormat?: string;
+ preserveVerySmallText?: boolean;
+ password?: string;
+ quiet?: boolean;
+}
+
+export interface NativeTextItem {
+ text: string;
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ fontName?: string;
+ fontSize?: number;
+ confidence?: number;
+}
+
+export interface NativeParsedPage {
+ pageNum: number;
+ width: number;
+ height: number;
+ text: string;
+ textItems: NativeTextItem[];
+}
+
+export interface NativeParseResult {
+ pages: NativeParsedPage[];
+ text: string;
+}
+
+export interface NativeScreenshotResult {
+ pageNum: number;
+ width: number;
+ height: number;
+ imageBuffer: Buffer;
+}
+
+export interface LiteParseNative {
+ parse(input: string | Buffer): Promise<NativeParseResult>;
+ screenshot(
+ input: string,
+ pageNumbers?: number[] | null,
+ ): NativeScreenshotResult[];
+ format(result: NativeParseResult): string;
+ readonly config: LiteParseNativeConfig;
+}
+
+function loadNative(): NativeBindings {
+ // Platform-specific package names generated by napi-rs
+ const triples: Record<string, string> = {
+ "darwin-x64": "@llamaindex/liteparse-darwin-x64",
+ "darwin-arm64": "@llamaindex/liteparse-darwin-arm64",
+ "linux-x64-gnu": "@llamaindex/liteparse-linux-x64-gnu",
+ "linux-x64-musl": "@llamaindex/liteparse-linux-x64-musl",
+ "linux-arm64-gnu": "@llamaindex/liteparse-linux-arm64-gnu",
+ "linux-arm64-musl": "@llamaindex/liteparse-linux-arm64-musl",
+ "win32-x64-msvc": "@llamaindex/liteparse-win32-x64-msvc",
+ };
+
+ // Try platform-specific package first
+ const platform = process.platform;
+ const arch = process.arch;
+
+ const candidates: string[] = [];
+ if (platform === "linux") {
+ // Try gnu first, then musl
+ candidates.push(`${platform}-${arch}-gnu`);
+ candidates.push(`${platform}-${arch}-musl`);
+ } else if (platform === "win32") {
+ candidates.push(`${platform}-${arch}-msvc`);
+ } else {
+ candidates.push(`${platform}-${arch}`);
+ }
+
+ for (const key of candidates) {
+ const pkg = triples[key];
+ if (pkg) {
+ try {
+ return require(pkg);
+ } catch {
+ // Not installed, try next
+ }
+ }
+ }
+
+ // Fallback: local .node file (development builds)
+ // Try several paths since __dirname may be dist/ or dist/src/
+ const searchDirs = [__dirname, join(__dirname, ".."), join(__dirname, "..", "..")];
+ for (const dir of searchDirs) {
+ try {
+ return require(join(dir, `liteparse.${platform}-${arch}.node`));
+ } catch {
+ // try next
+ }
+ try {
+ return require(join(dir, "liteparse.node"));
+ } catch {
+ // try next
+ }
+ }
+
+ throw new Error(
+ `Failed to load native module for ${platform}-${arch}. ` +
+ `Ensure the correct optional dependency is installed.`,
+ );
+}
+
+export const native = loadNative();
diff --git a/packages/node/tsconfig.json b/packages/node/tsconfig.json
new file mode 100644
index 0000000..028f6d6
--- /dev/null
+++ b/packages/node/tsconfig.json
@@ -0,0 +1,17 @@
+{
+ "compilerOptions": {
+ "target": "ES2022",
+ "module": "NodeNext",
+ "moduleResolution": "NodeNext",
+ "outDir": "./dist",
+ "rootDir": "./src",
+ "strict": true,
+ "esModuleInterop": true,
+ "declaration": true,
+ "declarationMap": true,
+ "sourceMap": true,
+ "skipLibCheck": true
+ },
+ "include": ["src"],
+ "exclude": ["node_modules", "dist"]
+}