A fast, helpful, and open-source document parser
add bbox guide
Logan Markewich4b18288b8e34
diff --git a/docs/src/content/docs/liteparse/cli-reference.md b/docs/src/content/docs/liteparse/cli-reference.mdindex 298aff7..0716989 100644--- a/docs/src/content/docs/liteparse/cli-reference.md+++ b/docs/src/content/docs/liteparse/cli-reference.md@@ -34,7 +34,7 @@ lit parse [options] <file>| `--max-pages <n>` | Maximum pages to parse | `10000` || `--target-pages <pages>` | Pages to parse (e.g., `"1-5,10"`) | — (all pages) || `--dpi <dpi>` | Rendering DPI | `150` |-| `--no-precise-bbox` | Disable precise bounding boxes | — |+| `--no-precise-bbox` | **Deprecated:** Disable populating the output boundingBoxes array. Will be removed in v2.0. Text item coordinates (`x`, `y`, `width`, `height`) are always present regardless. | — || `--preserve-small-text` | Keep very small text | — || `--password <password>` | Password for encrypted/protected documents | — || `--config <file>` | JSON config file path | — |@@ -93,7 +93,7 @@ lit batch-parse [options] <input-dir> <output-dir>| `--num-workers <n>` | Pages to OCR in parallel | CPU cores - 1 || `--max-pages <n>` | Maximum pages per file | `10000` || `--dpi <dpi>` | Rendering DPI | `150` |-| `--no-precise-bbox` | Disable precise bounding boxes | — |+| `--no-precise-bbox` | **Deprecated:** Disable populating the output boundingBoxes array. Will be removed in v2.0. Text item coordinates (`x`, `y`, `width`, `height`) are always present regardless. | — || `--recursive` | Search subdirectories | — || `--extension <ext>` | Only process this extension (e.g., `".pdf"`) | — (all supported) || `--password <password>` | Password for encrypted/protected documents (applied to all files) | — |diff --git a/docs/src/content/docs/liteparse/guides/agent-skill.md b/docs/src/content/docs/liteparse/guides/agent-skill.mdindex a5bd1ad..11a8827 100644--- a/docs/src/content/docs/liteparse/guides/agent-skill.md+++ b/docs/src/content/docs/liteparse/guides/agent-skill.md@@ -2,7 +2,7 @@title: Agent Skilldescription: Add LiteParse as a skill for coding agents like Claude Code, Cursor, and others.sidebar:- order: 4+ order: 6---LiteParse can be installed as a **coding agent skill** using Vercel's [skills](https://github.com/vercel-labs/skills) utility. This gives your coding agent the ability to process documents, generate screenshots, and parse text from files, all locally.@@ -41,7 +41,6 @@ You might want to configure some defaults so that your agent doesn't have to spe"maxPages": 1000,"dpi": 150,"outputFormat": "json",- "preciseBoundingBox": true,"preserveVerySmallText": false}```diff --git a/docs/src/content/docs/liteparse/guides/library-usage.md b/docs/src/content/docs/liteparse/guides/library-usage.mdindex 8ee09fc..79f282f 100644--- a/docs/src/content/docs/liteparse/guides/library-usage.md+++ b/docs/src/content/docs/liteparse/guides/library-usage.md@@ -40,9 +40,9 @@ for (const page of result.pages) {const parser = new LiteParse({ outputFormat: "json" });const result = await parser.parse("document.pdf");-for (const page of result.json.pages) {- for (const bbox of page.boundingBoxes) {- console.log(`[${bbox.x1}, ${bbox.y1}] → [${bbox.x2}, ${bbox.y2}]`);+for (const page of result.json?.pages || []) {+ for (const item of page.textItems) {+ console.log(`[${item.x}, ${item.y}] → [${item.x + item.width}, ${item.y + item.height}] ${item.text}`);}}```diff --git a/docs/src/content/docs/liteparse/guides/visual-citations.md b/docs/src/content/docs/liteparse/guides/visual-citations.mdnew file mode 100644index 0000000..11e8f1f--- /dev/null+++ b/docs/src/content/docs/liteparse/guides/visual-citations.md@@ -0,0 +1,170 @@+---+title: Visual Citations with Bounding Boxes+description: Use bounding boxes and screenshots to show exactly where information was found in a document.+sidebar:+ order: 4+---++When building agents or RAG workflows, it is often not enough to parse text and call it done. Frequently, users and applications will require you to show _where_ that text came from.++LiteParse gives you spatial coordinates for every text item, plus page screenshots, so you can highlight exact regions on the rendered page.++## How bounding boxes work++When you parse a document with JSON output, each page includes a key data source for visual citations: **`textItems`**. Every extracted text element with its position (`x`, `y`, `width`, `height`) and content.++```json+$ lit parse document.pdf --format json+{+ "pages": [{+ "page": 1,+ "width": 612,+ "height": 792,+ "text": "...",+ "textItems": [+ { "text": "Revenue grew 15%", "x": 72, "y": 200, "width": 150, "height": 12, ... }+ ],+ }]+}+```++Coordinates are in **PDF points** (1 point = 1/72 inch). Origin is the top-left corner of the page, with X increasing right and Y increasing down.++## Library usage++The library lets you do both in a single script, parse for bboxes and generate screenshots. For example, you might be looking for specific information like "Revenue" and want to show exactly where it appears on the page:++```typescript+import { LiteParse } from "@llamaindex/liteparse";++const parser = new LiteParse({ outputFormat: "json", dpi: 150 });++const result = await parser.parse("report.pdf");+const screenshots = await parser.screenshot("report.pdf");++// Find a text item by its content+for (const page of result.json?.pages || []) {+ for (const item of page.textItems) {+ if (item.text.includes("Revenue")) {+ console.log(`Found on page ${page.page}: (${item.x}, ${item.y}) ${item.width}×${item.height}`);+ }+ }+}+```++## Converting coordinates to image pixels++Text item coordinates are in PDF points, but screenshots are in pixels. To draw highlights on a screenshot, you need to scale the coordinates:++```typescript+const scaleFactor = dpi / 72; // PDF points → pixels at your chosen DPI++function itemToPixels(item, dpi = 150) {+ const scale = dpi / 72;+ return {+ x: item.x * scale,+ y: item.y * scale,+ width: item.width * scale,+ height: item.height * scale,+ };+}+```++For example, at the default 150 DPI the scale factor is `150 / 72 ≈ 2.08`, so a text item at `(72, 200)` maps to pixel `(150, 416)`.++## Full example: highlighting citations with sharp++Here's a complete workflow that parses a PDF, searches for matching text, and draws yellow highlight boxes on the page screenshot:++```typescript+import { LiteParse } from "@llamaindex/liteparse";+import sharp from "sharp";++const DPI = 150;+const SCALE = DPI / 72;++async function main() {+ const parser = new LiteParse({ outputFormat: "json", dpi: DPI });++ const result = await parser.parse("report.pdf");+ const screenshots = await parser.screenshot("report.pdf");++ // Search for text items containing a query, grouped by page+ const query = "revenue";+ const hitsByPage = new Map<number, Array<{ x: number; y: number; width: number; height: number }>>();++ for (const page of result.json?.pages || []) {+ for (const item of page.textItems) {+ if (item.text.toLowerCase().includes(query)) {+ if (!hitsByPage.has(page.page)) hitsByPage.set(page.page, []);+ hitsByPage.get(page.page)!.push(item);+ }+ }+ }++ // Draw all highlights per page into a single image+ for (const [pageNum, items] of hitsByPage) {+ const shot = screenshots.find((s) => s.pageNum === pageNum);+ if (!shot) continue;++ const composites = await Promise.all(+ items.map(async (item) => {+ const rect = {+ left: Math.round(item.x * SCALE),+ top: Math.round(item.y * SCALE),+ width: Math.round(item.width * SCALE),+ height: Math.round(item.height * SCALE),+ };++ const overlay = await sharp({+ create: {+ width: rect.width,+ height: rect.height,+ channels: 4,+ background: { r: 255, g: 255, b: 0, alpha: 0.3 },+ },+ })+ .png()+ .toBuffer();++ return { input: overlay, left: rect.left, top: rect.top };+ })+ );++ const highlighted = await sharp(shot.imageBuffer)+ .composite(composites)+ .png()+ .toBuffer();++ await sharp(highlighted).toFile(`citation_page${pageNum}.png`);+ console.log(`Saved citation_page${pageNum}.png (${items.length} highlights)`);+ }+}++main().catch(console.error);+```++## CLI usage++Parse to JSON to get bounding boxes:++```bash+lit parse document.pdf --format json -o result.json+```++Generate page screenshots alongside:++```bash+lit screenshot document.pdf -o ./screenshots+```++From there, you (or an agent) can process the resulting JSON and screenshots as needed using any tools available.++## Deprecated: `boundingBoxes`++The `boundingBoxes` array in JSON output is **deprecated** and will be removed in **v2.0**. It is a redundant representation of the same spatial data already available on each text item (`x`, `y`, `width`, `height`). Use `textItems` directly instead — it has the same coordinates plus text content, font metadata, and consistent indexing.++## Tips++- Use the same `dpi` value for both `parse()` and `screenshot()`. The default is `150` for both.+- Page `width` and `height` in the JSON are in PDF points, matching the coordinate space. Use these if you need to normalize coordinates to percentages.diff --git a/src/core/types.ts b/src/core/types.tsindex 6156f6e..62a9bd0 100644--- a/src/core/types.ts+++ b/src/core/types.ts@@ -108,6 +108,9 @@ export interface LiteParseConfig {* Calculate precise bounding boxes for each text line. Disable for faster* parsing when bounding boxes aren't needed.*+ * @deprecated Controls the deprecated `boundingBoxes` output. Will be removed in v2.0.+ * Text item coordinates (`x`, `y`, `width`, `height`) are always present regardless.+ ** @defaultValue `true`*/preciseBoundingBox: boolean;@@ -248,6 +251,8 @@ export interface OcrData {* An axis-aligned bounding box defined by its top-left and bottom-right corners.** All coordinates are in PDF points.+ *+ * @deprecated Use {@link TextItem} coordinates (`x`, `y`, `width`, `height`) instead. Will be removed in v2.0.*/export interface BoundingBox {/** X coordinate of the top-left corner. */@@ -274,7 +279,10 @@ export interface ParsedPage {text: string;/** Individual text elements extracted from the page. */textItems: TextItem[];- /** Bounding boxes for text lines. Present when {@link LiteParseConfig.preciseBoundingBox} is enabled. */+ /**+ * @deprecated Use {@link TextItem} coordinates instead. Will be removed in v2.0.+ * Present when {@link LiteParseConfig.preciseBoundingBox} is enabled.+ */boundingBoxes?: BoundingBox[];}@@ -303,7 +311,9 @@ export interface ParseResultJson {fontName?: string;fontSize?: number;}>;- /** Bounding boxes for text lines. */+ /**+ * @deprecated Use `textItems` coordinates instead. Will be removed in v2.0.+ */boundingBoxes: BoundingBox[];}>;}