npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@moniav/hebrew-pdf

v0.4.2

Published

Hebrew-first PDF ingestion library with RTL, tables, OCR routing, and audit diagnostics

Readme

@moniav/hebrew-pdf

A standalone Node.js/TypeScript library for ingesting heterogeneous Hebrew PDFs into logical-order text, clean Markdown, structured tables, and a page-level audit report.

It is designed as shared infrastructure for Tessera, BursAIQ, TASE_data, LegalAnalytics, and future services. It has no dependency on any of those applications.

Coding agents (Claude Code, Codex) integrating this library into a consumer project should read AGENT_GUIDE.md, the condensed decision-oriented reference that ships inside the package at node_modules/@moniav/hebrew-pdf/AGENT_GUIDE.md.

Design promise

The library does not claim that one parser can correctly process every PDF. Instead, every page follows an explicit route and must pass a Hebrew quality gate. A page is either:

  1. accepted from the native PDF layer;
  2. recovered by an independent text extractor;
  3. recovered by an explicitly enabled structured extractor;
  4. recovered by OCR;
  5. or marked review-required without emitting convincing but untrusted text into the canonical export. Every review-required page carries a quarantined reviewPreview in the structured page model, built from the best rejected candidate, so a reviewer can compare it with the source.

It never reverses whole Hebrew strings.

Validated results

Version 0.2.0 was validated end to end by independent review of the extraction against rendered page images.

  • Bank of Israel credit-risk directive (62 pages of dense regulatory Hebrew): 52 pages accepted, 87.8% of the Hebrew characters the text layer holds, with correct clause numbering (65ב.), bullets, headings, linked footnotes, and formulas decoded to real operators. The 10 withheld pages are ruled tables and diagrams that correctly fail closed, each with a quarantined preview.
  • General table-of-contents document (4 pages): 4 accepted, 100% coverage; with structured recovery enabled the contents emit as typed tables with correct row pairing.
  • An unseen mixed Hebrew/English legal business document extracted cleanly on the first run (2/2 pages, exit 0), confirming the heuristics are not fitted to the fixtures.
  • Ten Bank of Israel directives, circulars and a Q&A file (353 pages): 352 pages published, the one withheld being blank. Section numbers, 55 of 55 footnotes, definitions, running headers and 272 question-and-answer records come through as typed blocks.

Install

pnpm add @moniav/hebrew-pdf

Runtime: Node.js 22 or newer. The default local adapters also expect Poppler (pdftotext, pdftoppm). OCR requires Tesseract with heb+eng language data. Optional structured recovery uses Python with pymupdf4llm installed and is enabled explicitly with --structured-recovery pymupdf4llm or the matching library option.

pnpm install
python -m pip install pymupdf4llm

Install Poppler and Tesseract through the operating system package manager and ensure their executables are on PATH. On Windows, pdftotext and pdftoppm are also discovered in the WinGet Poppler package, and Tesseract in its standard %ProgramFiles%\Tesseract-OCR location, so no PATH editing is required for those installs. When Hebrew language data cannot be placed next to Tesseract, point the standard TESSDATA_PREFIX environment variable at a directory containing heb.traineddata and eng.traineddata. A missing binary produces an actionable error naming it and how to install it, and the report's engineHealth.ocr key shows whether the OCR route was available, not_needed, disabled, or failed (with ocrError). PyMuPDF4LLM is optional; leave structured recovery off when Python is unavailable.

The package is consumable today from a registry, a packed tarball (npm pack), a git dependency, or a pnpm workspace; the packed artifact has been verified end-to-end from a clean consumer project.

Instructing a coding agent

To have a coding agent install and use the library in another project, paste the block below into its prompt, or into that project's CLAUDE.md or AGENTS.md so every session sees it. It sends the agent to AGENT_GUIDE.md, which ships inside the package.

## Hebrew PDF ingestion

Use `@moniav/hebrew-pdf` for reading any Hebrew or mixed Hebrew/English PDF.
Do not use pdf-parse, raw pdftotext calls, or RTL string reversal; they corrupt Hebrew order.

Install:
- `pnpm add @moniav/hebrew-pdf` (or the npm/yarn equivalent).
- Requires Node.js 22+. The package is ESM-only: use `import`, not `require`.
- System tools: Poppler (`pdftotext`, `pdftoppm`) for all PDFs; Tesseract with `heb` and `eng` traineddata for scanned pages.
  - Linux: `apt-get install poppler-utils tesseract-ocr tesseract-ocr-heb tesseract-ocr-eng`
  - macOS: `brew install poppler tesseract tesseract-lang`
  - Windows: `winget install oschwartz10612.Poppler` and the UB-Mannheim Tesseract installer; both are auto-discovered.
- Optional, only if table cells are needed: `python -m pip install pymupdf4llm`, then enable `structuredRecovery: "pymupdf4llm"`.

Before writing any integration code, read `node_modules/@moniav/hebrew-pdf/AGENT_GUIDE.md` in full and follow its rules.
For API details, read the `README.md` beside it.

Verify the install by ingesting one Hebrew PDF:
`npx hebrew-pdf sample.pdf --out sample.md --report sample.report.json`
Check `engineHealth` in the report. Exit code 2 means some pages need review, not that the run failed.

Library API

import { createHebrewPdf } from "@moniav/hebrew-pdf";

const hebrewPdf = createHebrewPdf({
  ocr: "auto",
  structuredRecovery: "pymupdf4llm",
  maxPages: 500,
});

const result = await hebrewPdf.ingest("./document.pdf");

console.log(result.markdown);          // display/export artifact
console.log(result.text);              // logical text with \f page separators
console.log(result.document.pages);    // canonical structured page model
console.log(result.report.needsReview);

Buffers are supported without the caller managing a temporary file:

const result = await hebrewPdf.ingest({
  data: uploadedBuffer,
  filename: "דוח-שנתי.pdf",
});

There is also a one-shot helper:

import { ingestHebrewPdf } from "@moniav/hebrew-pdf";

const result = await ingestHebrewPdf(buffer, { ocr: "off" });

Errors

ingest() rejects with a typed error for every fatal condition. Each error extends HebrewPdfError and carries a stable, machine-readable code. Branch on code, not on message text, since wording may change between releases.

| Code | Class | Meaning | |---|---|---| | invalid_pdf | InvalidPdfError | The input is not a parseable PDF: a missing header or a native parser failure. The original native error is preserved as cause. | | encrypted_pdf | EncryptedPdfError | The PDF appears to be password-protected/encrypted. | | pdf_too_large | PdfTooLargeError | The input exceeds maxBytes. A string-path input is size-checked before the full file is read into memory. | | too_many_pages | TooManyPagesError | The PDF has more pages than the configured maxPages. | | empty_input | EmptyInputError | The input buffer is empty. | | zero_pages | ZeroPagesError | The PDF parsed but reported zero pages, and does not look encrypted. | | invalid_options | InvalidOptionsError | A caller-supplied option failed validation, for example ocr: "yes-please" or maxPages: 0. | | aborted | AbortedError | Ingestion was cancelled through signal. |

import { HebrewPdfError, createHebrewPdf } from "@moniav/hebrew-pdf";

try {
  await createHebrewPdf().ingest("./document.pdf");
} catch (error) {
  if (error instanceof HebrewPdfError) {
    console.error(error.code, error.message);
  }
  throw error;
}

Every code is exported as the HebrewPdfErrorCode union type. HEBREW_PDF_ERROR_CODES lists them all at runtime.

Cancellation

Pass an AbortSignal to cancel ingestion. The signal is checked before work starts, between pages, after every engine call, and once more before the result is returned, so an aborted run always rejects with AbortedError and never resolves with a result. It is passed to every adapter: the bundled Poppler, PyMuPDF, pdftoppm, Tesseract, and custom OCR subprocesses are killed as soon as it fires, instead of waiting out the full command timeout. Native inspection runs in-process and cannot be interrupted; a large document is refused by maxPages before its pages are extracted.

const controller = new AbortController();
const resultPromise = createHebrewPdf({ signal: controller.signal }).ingest("./document.pdf");
setTimeout(() => controller.abort(), 5000);
await resultPromise; // rejects with AbortedError if the timeout wins the race

Canonical output

Markdown is not the internal data contract. Each result includes a serializable HebrewPdfDocument:

interface HebrewPdfDocument {
  schemaVersion: 1;
  language: "he";
  direction: "rtl";
  source: { filename: string; byteLength: number; sha256: string };
  pageCount: number;
  pages: Array<{
    number: number;
    route: PageRoute;
    markdown: string;
    text: string;
    blocks: DocumentBlock[]; // heading, paragraph, clause, list, footnote, definition, qa, table, page-header, page-footer, review
    rawText: string;
    edits: NormalizationEdit[];
    editMap: NormalizationMapStatus;
    tableDetected: boolean;
    needsReview: boolean;
    quality: QualityResult;
    reviewPreview?: { markdown: string; text: string; blocks: DocumentBlock[] };
  }>;
  // A question-and-answer table's rows, each whole even when a page break split it.
  records?: Array<{ marker: string; question: string; answer: string; updated?: string; pages: number[] }>;
}

reviewPreview exists only in this document model. The canonical Markdown export intentionally carries just a review marker comment for a rejected page, and the audit report carries the rejection reasons; the quarantined candidate text never leaks into either.

Structured table blocks contain headers, rows, and the original Markdown. This lets an application render a table, index individual cells, or derive plain text without reparsing the complete document. Pages also expose normalization telemetry so a reviewer can see exactly what changed: nfkcChanges, rtlPunctuationRepairs, acronymRepairs, latinBoundarySpaces, directionalControlsRemoved, splitWordRejoins (extractor mid-word breaks rejoined with page-level evidence), and symbolGlyphsMapped (Symbol-font formula glyphs decoded to Unicode operators).

Raw text and the edit list

Each page carries the text its route produced before any repair ran, as rawText, and an ordered non-overlapping list of the replacements that turn it into the page's Markdown, as edits. This is what makes exact citation against the source possible. On a real regulatory page most lines carry a directional control that normalization removes, so a quotation of more than one line taken from the display text is not a substring of the raw text.

Verify the map on ingest instead of trusting it. A page whose map does not replay is a library regression, and catching it here is the difference between a loud failure now and a wrong citation weeks later.

import { applyEdits, PACKAGE_VERSION, verifyEdits } from "@moniav/hebrew-pdf";

for (const page of result.document.pages) {
  const display = page.editMap.target === "markdown" ? page.markdown : page.reviewPreview?.markdown ?? "";
  const check = verifyEdits(page.rawText, page.edits, display);
  if (!check.ok) throw new Error(`page ${page.number}: map from hebrew-pdf ${PACKAGE_VERSION} did not replay at ${check.firstMismatchAt}: ${check.reason}`);
}

applyEdits(rawText, edits) replays a map and returns the display text. It throws RangeError on a list that is out of order, overlapping, or out of bounds, so verifyEdits is the call to use when a verdict is wanted rather than an exception.

Each edit records the raw range it replaces, the replacement, the display range that replacement occupies, and the repair that made it. The kinds are nfkc, bidi_control_removed, rtl_punctuation_repair, acronym_repair, latin_boundary_space, split_word_rejoin, symbol_glyph_mapped, footnote_reference, rtl_run_order, niqqud_reorder, ocr_glyph_repair, line_ending, whitespace, and layout_reconstruction for the plain-text route's rebuild of the page's line layout. Offsets are UTF-16 code units and both ranges are half-open.

editMap says where the map stands. target is the text the edits reconstruct: markdown for an accepted page, reviewPreview for a refused one, whose canonical Markdown is only a review marker. complete is false when the map must not be used to project a citation, which is the case for every refused page (gap: "page_refused"), a page no route could read (gap: "no_text"), and a page whose map the library replayed and found wrong (gap: "replay_failed"). origin is engine_output except on the one route that places verified geometry tables into the plain-text engine's page, where it is composed_candidate.

Pass requireProvenance: true when page text will be quoted or highlighted against the source. A page whose map does not replay is then quarantined for review with a provenance_replay_failed error instead of being published; without the option it is published with the same issue as a warning.

What rawText holds depends on the route: the native extractor's Markdown, the plain-text engine's own page text, the structured engine's Markdown, or an OCR engine's reading of the page. On the plain-text route the rebuild of the page's line layout is recorded as edits like any other stage, so the map reaches that engine's output rather than stopping at the rebuilt text.

For digital Hebrew documents, the native adapter also consumes positioned text and tagged-PDF structure from pdf-inspector. Right-margin regulatory numbers are reconstructed as explicit clause blocks, bold numeric/Hebrew markers become section headings, parenthesized Hebrew markers become ordered list items, and small-font footer notes become footnote blocks. A quoted term and its meaning ("שלוחות" – תאגידים נשלטים…) becomes a definition block, whether the page set it as prose or as a two-column layout; a ruled question-and-answer table's rows become qa records (number, question, answer, update date); and the line that opens or closes every page of a document (a running header, a page number) becomes a page-header or page-footer block. document.records gathers the qa rows into whole records, joining a row a page break split. A financial statement's note heading (ביאור 5 - הלוואות לזמן ארוך) on a line of its own becomes a heading block. Geometry supplies boundaries only; Poppler remains the independent source for logical wording. When a geometry-confirmed marker does not survive into canonical blocks, the pipeline reports numbered_structure_loss. It fails the page closed only when the marker's text is missing from the page entirely, which is content loss; when the text is present but was not reconstructed as its own block, the page is accepted with the same code as a warning. Single-character markers never fail a page closed, because the geometry detector reports a stray margin letter as a list item and a lone letter or digit is present in almost any Hebrew page.

Extraction routes

flowchart TD
    A[PDF page] --> B[pdf-inspector]
    B -->|image-only page| S{Structured recovery enabled?}
    B --> C{Native quality gate}
    C -->|pass| D[Canonical blocks]
    C -->|fail| E[pdftotext fallback]
    E --> G{Fallback gate incl. corroborated-table check}
    G -->|pass| D
    G -->|fail| S
    S -->|yes| I[PyMuPDF4LLM structured recovery]
    I --> J{Quality, word geometry, and 85% cross-engine agreement}
    J -->|pass| D
    S -->|no| O{OCR enabled?}
    J -->|fail| O
    O -->|yes| F[pdftoppm + Tesseract heb+eng, or custom OCR command]
    O -->|no| H[Review marker + quarantined reviewPreview]
    F --> K{OCR quality gate}
    K -->|pass| D
    K -->|fail| H

Every gate applies the same Hebrew quality assessment; a page only reaches review-required after each enabled route has been tried and rejected, and the best rejected candidate is kept as the page's reviewPreview.

The bundled native adapter pins @firecrawl/pdf-inspector 1.14.0. It provides PDF classification, reading order, multi-column detection, and Markdown tables. pdftotext provides an independent fallback. Tesseract is the default OCR route for plain scanned text. Pages the native adapter identifies as image-only skip the text fallback and continue to the enabled structured/OCR recovery stages.

Replaceable adapters

All engines implement PipelineDependencies:

interface PipelineDependencies {
  inspect(buffer: Buffer): Promise<NativeDocument>;
  extractFallbackPages(
    path: string,
    pageCount: number,
    options?: { timeoutMs?: number; signal?: AbortSignal },
  ): Promise<string[]>;
  extractStructuredPage?(input: StructuredPageInput): Promise<{
    markdown: string;
    structured: true;
    engine: string;
  }>;
  extractOcrPage(input: OcrPageInput): Promise<{
    markdown: string;
    structured: boolean;
    engine: string;
  }>;
}

Inject adapters at construction:

const hebrewPdf = createHebrewPdf({
  dependencies: {
    inspect: myNativeExtractor,
    extractFallbackPages: myFallback,
    extractStructuredPage: myLayoutAwareExtractor,
    extractOcrPage: myLayoutAwareOcr,
  },
});

This is the extension point for cloud OCR, a vision model, GPU table recognition, or a sandboxed extraction worker. Bundled adapters are also exported from @moniav/hebrew-pdf/adapters.

Cross-checks before a page is published

An accepted native page is read against the plain-text engine first. Numbers that the reference writes only reversed, or Hebrew words it spells only backwards, refuse the native text (native_reversed_against_reference), since every other gate passes on such a page. Bracket balance is judged against the source page, so a parenthesis that closes on the next page is a warning rather than a refusal.

Both engines can group a number with the wrong neighbour and still agree on every word and value, so an accepted text-layer page is also read against where its words sit. The native extractor's runs are ordered by line and by position, and each line is read with the Unicode bidi rules for a right-to-left paragraph (src/quality/geometry.ts). Hebrew words align the two readings; between two aligned words, values in another order ((3) 2 for (2) 3, 3-A-3/P for A-3/P-3) or a value the page does not print in place of one it does (2264 for 64 and its note mark 22) refuse the page (position_reading_disagrees). A run of values joined only by operators may read either way, since the page does not say whether it was embedded left to right; a value placed elsewhere on the page, such as a running header, and published tables, which are verified row by row, are not compared. A different count of negations and exceptions (לא, אין, למעט, אלא, prefixed forms included) is a warning (position_reading_negations_differ). Each compared page reports the result as positionCheck: whether the two readings are of the same text, word-order agreement both ways, missing and extra words, negation counts, value differences and findings. pnpm corpus reports how many labelled values and passages are published wrong on accepted pages.

The same positions cut the page into regions (src/quality/regions.ts): bands split by a wide vertical gap, columns split by an empty gutter that runs a band's full height, and a running header or footer when the first or last band holds at most three lines set smaller than the body or naming the page. A narrow side of clause numbers or item letters is a margin and stays with the lines it opens. The page is read region by region, so a column is read through before the next one starts, and each page reports regions, each with its kind, box, line count, status and reasons. verified means the published text has the region's words in its order and no check disagrees there. needs_review means a check found a fault there, and the reasons name it. unverified means nothing could be confirmed: the region has too few words to align, was published as a table (verified row by row against the plain-text engine instead), or the page as a whole is not the same text as the position reading. A reviewer reads the regions that need review against the page image at their box.

Every check above reads the text layer, so a font whose map gives the wrong character for a glyph passes them all: acri-takzir prints 7146/12 and every engine reads 21/6417. With rereadValues: true (--reread-values) each accepted text-layer page is rendered and read by Tesseract, Hebrew and English, at 300 DPI (src/quality/reread.ts). A printed line holding a number is compared only where OCR also reads at least half of its Hebrew words, and only numbers OCR reads with confidence 85 or more count. A whole page read in Hebrew still misreads digits (.3 for a list's 13.), so each disputed value is then cropped from a 400 DPI rendering, set inside a white border, and read twice in English with no character whitelist, as one line (--psm 7) and as a raw line (--psm 13). A disagreement stands only when both readings find a number in the box, neither reads back the layer's number, and the published text carries it; the page is then refused (reread_values_disagree) and reports reread. These settings were chosen by benchmark over the corpus: whole-page OCR alone disagreed on 166 lines, a digit whitelist turned Hebrew letters into digits, one-word mode (--psm 8) drops a thin leading 1 at the crop's edge, one-line mode alone reads a clean 7,500,000 as 7,900,000, and the two readings together leave 19, with every disputed acri-takzir page still refused. The re-read costs one OCR pass per page and needs Poppler and Tesseract with Hebrew data; when they are missing the page is published with a reread_unavailable warning. With it on, pnpm corpus --reread publishes 3 of 428 labelled values and passages wrong on accepted pages instead of 7, at the cost of refusing two correct pages (cbs-population page 2, gen-financial page 1).

Tables and scanned documents

A scanned page that carries the text layer of an earlier OCR pass is read from that layer by the plain-text engine and published only if it passes every gate; the report keeps a native_extractor_requested_ocr warning saying so. Its tables are rebuilt from Poppler's word boxes and checked against Poppler's own layout text, which proves the rows and cells but not the words, since both sides read the same layer. A page with no text layer, or one the native extractor distrusts for another reason, still goes to OCR or review.

Native Markdown tables are validated for a real header/separator and consistent cell width. Ordinary Hebrew prose containing | is not treated as a table.

Plain-text fallback and plain Tesseract cannot reconstruct table geometry. A native table suspicion is not trusted on its own: the detector mistakes right-aligned RTL prose for a grid often enough that acting on every suspicion would delete most of a clean document. The page fails closed when a second, independent engine corroborates the suspicion, meaning the native Markdown itself holds a compact multi-column table, the Poppler -layout text holds at least three consecutive column-aligned rows of short cells, or word geometry confirms a table during structured recovery. An uncorroborated suspicion is reported as a plain_text_cannot_recover_tables warning on the accepted page so a reviewer can still check it, unless every grid the native engine wrote for the page is prose cut into cells (wide rows of phrases), which reports nothing. A corroborated table that no route verifies is also published as text with that warning by default, with tableDetected: false and the page listed in pagesWithSuspectedTables; pass unverifiedTables: "refuse" to quarantine such pages for review instead, when callers need verified table structure or nothing. The report distinguishes the two views: pagesWithTables lists accepted pages whose output holds a verified Markdown table, while pagesWithSuspectedTables carries the native layout detector's unfiltered suspicions, including rejected pages. Tables are first rebuilt from the native extractor's own word positions (tableRecovery: "positioned", the default): consecutive lines that share empty vertical gutters are read as a grid, each cell's visual-order text is put back into logical order, and every row is verified against the Poppler text exactly as structured tables are. Verified tables replace the lines they cover; a table with an unverified row is left as text, and numbered lists and formula legends are never read as tables. A stacked multi-line header that the plain-text engine splits differently is left in the prose while the verified body rows below it are published, provided every word and value of the header is on the page. Two numbers joined by a dash and adjacent footnote marks may appear in either order, since RTL engines disagree on them; nothing else is reordered. This needs no Python. On the bundled fixtures it recovers every ruled table PyMuPDF finds plus the borderless index and LTV tables it misses, and publishes 15 of the 24 ruled tables in full. Pages whose tables verify report route: "positioned-tables-markdown" and agreement.tables; set tableRecovery: "off" to leave tables to structured recovery alone.

For the tables that do not verify, enable the optional PyMuPDF4LLM structured recovery, configure another layout-aware adapter, or use the CLI's --ocr-command contract. Structured output is checked against independent Poppler text. Both word precision and recall must reach 85%; missing reference text, a missing detected table, or lower agreement fails closed. Word agreement says nothing about values, so numbers, percent signs, ratings, and Latin identifiers are also compared one for one, repeats included: a changed, invented, or dropped value fails closed (structured_values_disagree), and the same values in a different order are reported (structured_value_order_differs). Geometry tables are never appended to the plain-text page. Each table row must match one to three consecutive Poppler lines exactly, letter for letter and value for value, with numbers and Latin words in the same order inside every left-to-right run of a cell; the table then replaces those lines in place, so it appears once and the prose around it survives. A table with any unmatched row is not published (structured_table_unverified) and the page keeps its plain text, with one exception: rows the engine spread over more lines than that are accepted as a block when the lines between the table's verified rows hold exactly their Hebrew words and numbers. A two-column definitions layout is published as definition blocks and a grid of item markers beside hanging-indented text as the list it is, rather than as tables. Reports expose agreement, value comparison, matched table rows, and engine health. The OCR command receives the absolute PDF path and a 1-based page number and prints Markdown.

The bundled structured adapter captures PyMuPDF word and line geometry before importing PyMuPDF4LLM, then reconstructs RTL cells in logical word order. Borderless contents/index grids are reduced to typed תיאור / מספר tables; text-aligned prose is not promoted to a table. Numeric columns are rendered as isolated LTR values while Hebrew headers and labels remain RTL. MuPDF parser diagnostics are routed to stderr and filtered, so engine messages never appear as document content.

For table-of-contents and other column-layout documents, enable structured recovery. Without it, such pages are still extracted with complete text in logical order, but as prose paragraphs rather than typed table rows; with it, the same pages route through geometry and emit real Markdown tables with correct row pairing.

CLI

The CLI is an optional thin wrapper around the same public API. Once the package is installed as a dependency, the hebrew-pdf binary is available directly:

npx hebrew-pdf document.pdf --out document.md --report document.report.json

When working inside this repository, the same CLI runs from source:

pnpm install
pnpm build
pnpm ingest document.pdf \
  --structured-recovery pymupdf4llm \
  --out document.md \
  --report document.report.json \
  --json document.structured.json

Useful options:

  • --ocr auto|off|force controls whether unresolved pages enter OCR.
  • --structured-recovery off|pymupdf4llm enables optional layout-aware recovery.
  • --ocr-command EXECUTABLE supplies a custom page-level Markdown OCR program.
  • --max-pages NUMBER rejects oversized documents before extraction.
  • --reread-values reads every accepted text-layer page's numbers again from its rendering and refuses a page whose text layer disagrees with the pixels.

Exit code 0 means every page passed; 2 means artifacts were written but review is required; 1 means a fatal processing error.

Human review demo

Run the included local review studio:

pnpm demo

Then open http://127.0.0.1:4173. The demo supports PDF upload, document and page-level statistics, synchronized source/result page navigation, rendered and raw extraction views, engine health (Poppler, structured, and OCR), cross-engine agreement, risk-sorted pages, issue details, next-flagged navigation, approve/flag decisions, reviewer notes, unresolved-page filtering, and review JSON export. Uploaded PDFs and ingestion results are retained only in server memory; the six newest sessions are kept until the demo process exits.

The rendered view mirrors the source page's RTL layout: clause numbers anchor at the right margin with the clause text flowing left of them, Hebrew-letter and numeric list markers render on the right, tables render RTL with numeric columns as isolated LTR cells, and Latin runs inside Hebrew text are bidi-isolated so mixed sentences read exactly as the original does. Rejected pages show their quarantined reviewPreview behind an explicit untrusted-content banner so a reviewer can compare it with the source before approving anything.

Hebrew quality policy

Detection runs before destructive cleanup. Signals include:

  • minimum readable text and expected Hebrew ratio;
  • visual-order (reversed) Hebrew, detected structurally rather than by vocabulary: Hebrew final forms (ך ם ן ף ץ) only end words and their medial counterparts only occur mid-word, so words that begin with a final form or end with a medial one indicate mirrored text in any Hebrew, with a closed anchor list kept as an additional tripwire for short fragments;
  • broken CID/font mappings and replacement characters;
  • private-use glyphs that survived Symbol-font decoding (unmapped_private_use_glyphs), so a formula that would render as empty boxes can no longer be accepted silently;
  • Hebrew presentation forms and fragmented letters;
  • bidi control characters;
  • malformed Markdown table widths;
  • structured/Poppler disagreement below 85% precision or recall;
  • structured/Poppler disagreement on any number, percent sign, rating, or Latin identifier;
  • geometry tables whose rows Poppler does not reproduce exactly;
  • missing structured tables where native layout detection found one;
  • fragmented or paragraph-sized headings;
  • unbalanced (), [], and {} punctuation;
  • explicit image/picture omission markers;
  • ambiguous Hebrew/number boundaries, preserved and retained as warnings;
  • native extractor OCR recommendations.
  • positioned marker loss between geometry and canonical blocks.

Normalization happens only after acceptance or inside a quarantined review preview:

  • NFKC normalization and bidi-control removal;
  • punctuation displaced by the source's directional embeddings (U+202A..U+202C) is resolved at the embedding edges before the controls are stripped, so marks read on the side a human sees in the original; signs written directly onto a token (AA-, 100%) are preserved;
  • bounded mirrored-punctuation repair under a balance invariant: a line whose brackets already nest correctly is never rewritten, and a rewrite must leave the line balanced;
  • signs and hyphens that swapped sides with an embedded number: שוטף 45 + reads שוטף + 45, ב 24- reads ב-24, התשס"ז2007- reads התשס"ז-2007, and a mirrored item whose word lost its space () 1(מוסד, )א(השר) is rebuilt as (1) מוסד, (א) השר;
  • pointed Hebrew put back in reading order: Poppler writes a letter's points before the letter (ְּבֵר), and they move after it; a space it put inside a pointed word is removed where the native extractor's glyph clusters write the word whole;
  • Hebrew/Latin/digit boundary spacing, with alphabetic clause ordinals (65א) protected;
  • Symbol-font private-use glyphs decoded to real Unicode operators and Greek letters;
  • extractor mid-word breaks rejoined only when the page itself proves the cut (the orphan tail never stands alone on the page and the joined word appears intact elsewhere on it);
  • footnote reference marks the plain-text engine fused into a value or word (1.12.5% for 12.5%.¹, ושמומנו19 for ושמומנו¹⁹) are restored as superscript digits where geometry found a small raised number or superscript glyphs on the page; a mark whose footnote is defined on the page becomes a [^N] reference instead.

Hebrew/number adjacency is otherwise preserved. English text, URLs, email addresses, dates, decimal numbers, %, and ₪ are not reversed. Correct input round-trips byte-identical through the whole normalization and block-parsing path, enforced by tests.

Supported PDF classes

Semantic headings include numbered sections, nested subsections, notes (ביאור N), appendices, and common Hebrew section titles instead of trusting visual font size alone.

The routing design covers digital text PDFs, scanned PDFs, mixed text/scan documents, multi-column pages, RTL/LTR mixed text, native tables, and pluggable scanned-table OCR. Malformed, encrypted, corrupt, rotated, custom-font, sparse-table, and misleading text-layer cases must be represented in a regression corpus. See fixtures/README.md.

“All kinds” means the library has a safe route or explicit failure state for each class—not that any engine can guarantee perfect extraction from every possible PDF.

Project checks

pnpm check
pnpm test
pnpm build

Security boundaries

  • 100 MiB default input limit and configurable page limit, both validated as typed errors rather than silently dropped;
  • 120-second subprocess timeout, 64 MiB stdout limit, and 256 KiB stderr limit;
  • shell: false for subprocesses;
  • caller-supplied filenames are sanitized to a safe basename before touching disk;
  • temporary uploads and OCR images removed on both success and failure, including a failed write;
  • tables rebuilt from word positions published only where every row verifies against Poppler text;
  • a labelled corpus of 33 documents from ten publishers (fixtures/corpus, pnpm corpus) that scores extraction against facts checked on the page, with every open gap recorded in tests/corpus/known-failures.json;
  • the page limit enforced after classification and before page extraction;
  • cancellation via AbortSignal, passed to every subprocess and checked after every engine call;
  • failed candidate text excluded from final output.

For public uploads, run PDF/OCR tools in isolated workers with CPU, memory, concurrency, malware-scanning, and object-storage controls.

License

MIT. Dependencies and system tools retain their own licences.