Skip to content

A fast, helpful, and open-source document parser

update readme

Logan Markewich4c4424619c5d
diff --git a/OCR_API_SPEC.md b/OCR_API_SPEC.md
new file mode 100644
index 0000000..9855580
--- /dev/null
+++ b/OCR_API_SPEC.md
@@ -0,0 +1,242 @@
+# LiteParse OCR API Specification
+
+This document defines the standard HTTP API that OCR servers must implement to work with LiteParse.
+
+## Overview
+
+LiteParse expects a simple HTTP endpoint that accepts an image and returns text with bounding boxes. Your OCR server can internally use any OCR engine (EasyOCR, PaddleOCR, Tesseract, Cloud APIs, etc.) as long as it conforms to this API.
+
+## Endpoint
+
+```
+POST /ocr
+```
+
+## Request Format
+
+**Content-Type:** `multipart/form-data`
+
+**Fields:**
+
+| Field | Type | Required | Description |
+|-------|------|----------|-------------|
+| `file` | binary | Yes | Image file (PNG, JPG, etc.) |
+| `language` | string | No | Language code (default: `en`) |
+
+### Language Codes
+
+Use ISO 639-1 two-letter codes:
+- `en` - English
+- `zh` - Chinese
+- `ja` - Japanese
+- `ko` - Korean
+- `fr` - French
+- `de` - German
+- `es` - Spanish
+- `ar` - Arabic
+- etc.
+
+Your server should map these to whatever format your underlying OCR engine expects.
+
+## Response Format
+
+**Content-Type:** `application/json`
+
+**Structure:**
+
+```json
+{
+ "results": [
+ {
+ "text": "recognized text",
+ "bbox": [x1, y1, x2, y2],
+ "confidence": 0.95
+ }
+ ]
+}
+```
+
+**Fields:**
+
+| Field | Type | Description |
+|-------|------|-------------|
+| `results` | array | Array of text detection results |
+| `results[].text` | string | Recognized text content |
+| `results[].bbox` | [number, number, number, number] | Bounding box `[x1, y1, x2, y2]` where (x1,y1) is top-left and (x2,y2) is bottom-right |
+| `results[].confidence` | number | Confidence score between 0.0 and 1.0 |
+
+## Example
+
+### Request
+
+```bash
+curl -X POST http://localhost:8080/ocr \
+ -F "language=en"
+```
+
+### Response
+
+```json
+{
+ "results": [
+ {
+ "text": "Hello",
+ "bbox": [10, 20, 60, 40],
+ "confidence": 0.98
+ },
+ {
+ "text": "World",
+ "bbox": [70, 20, 130, 40],
+ "confidence": 0.97
+ }
+ ]
+}
+```
+
+## Error Handling
+
+Return appropriate HTTP status codes:
+
+- `200 OK` - Success
+- `400 Bad Request` - Invalid request (missing file, invalid language, etc.)
+- `500 Internal Server Error` - OCR processing failed
+
+Error response format:
+
+```json
+{
+ "error": "Description of the error"
+}
+```
+
+## Implementation Notes
+
+### Coordinate System
+
+- Origin (0,0) is at the **top-left** of the image
+- X increases to the right
+- Y increases downward
+- All coordinates are in pixels
+
+### Bounding Box Format
+
+Always return axis-aligned bounding boxes as `[x1, y1, x2, y2]`:
+- `x1, y1` = top-left corner
+- `x2, y2` = bottom-right corner
+- `x2 > x1` and `y2 > y1`
+
+If your OCR engine returns rotated boxes or polygon coordinates, convert them to axis-aligned boxes by taking min/max coordinates.
+
+### Confidence Scores
+
+- Normalize to range 0.0 to 1.0
+- 1.0 = 100% confident
+- 0.0 = 0% confident
+- If your OCR engine doesn't provide confidence, use `1.0`
+
+### Text Ordering
+
+Results should be ordered by reading order (top-to-bottom, left-to-right for most languages).
+
+## Example Implementations
+
+See the `/ocr` directory for reference implementations:
+
+- `ocr/easyocr/` - Wrapper for EasyOCR
+- `ocr/paddleocr/` - Wrapper for PaddleOCR
+- `ocr/tesseract/` - Wrapper for Tesseract (if you want to run it as a server)
+
+## Testing Your Server
+
+Quick test:
+
+```bash
+# 1. Start your server
+python server.py
+
+# 2. Test with curl
+curl -X POST http://localhost:8080/ocr \
+ -F "language=en" \
+ | jq .
+
+# 3. Expected output:
+# {
+# "results": [
+# {
+# "text": "...",
+# "bbox": [x1, y1, x2, y2],
+# "confidence": 0.xx
+# }
+# ]
+# }
+```
+
+Use with LiteParse:
+
+```bash
+pnpm parse document.pdf --ocr-server-url http://localhost:8080/ocr
+```
+
+## FAQ
+
+### Q: What if my OCR returns rotated bounding boxes?
+
+Convert to axis-aligned boxes:
+
+```python
+def polygon_to_bbox(polygon):
+ """Convert polygon [[x1,y1], [x2,y2], ...] to [x1, y1, x2, y2]"""
+ xs = [point[0] for point in polygon]
+ ys = [point[1] for point in polygon]
+ return [min(xs), min(ys), max(xs), max(ys)]
+```
+
+### Q: What if my OCR doesn't return confidence scores?
+
+Just return `1.0` for all results.
+
+### Q: Can I return empty results?
+
+Yes, return `{"results": []}` if no text is detected.
+
+### Q: Should I filter low-confidence results?
+
+You can, but LiteParse will also handle filtering based on its own thresholds.
+
+### Q: What image formats should I accept?
+
+At minimum: PNG, JPG. Optionally: TIFF, WebP, BMP, GIF.
+
+### Q: Should I handle rotation correction?
+
+Optional. If your OCR engine supports it, you can auto-correct rotation before processing.
+
+### Q: What about multi-page documents?
+
+LiteParse handles page splitting. Your server only needs to process single images.
+
+### Q: Performance considerations?
+
+- Keep server response time under 10 seconds per image
+- Support concurrent requests
+- Consider GPU acceleration for better performance
+- Cache OCR models in memory (don't reload per request)
+
+## Compliance Checklist
+
+- [ ] Accepts `POST /ocr` endpoint
+- [ ] Accepts `file` and `language` form fields
+- [ ] Returns JSON with `results` array
+- [ ] Each result has `text`, `bbox`, and `confidence`
+- [ ] Bounding boxes in `[x1, y1, x2, y2]` format
+- [ ] Confidence normalized to 0.0-1.0 range
+- [ ] Returns 200 status on success
+- [ ] Returns appropriate error codes and messages
+- [ ] Handles common image formats (PNG, JPG)
+- [ ] Processes images in under 10 seconds
+
+## Support
+
+Questions? Open an issue on GitHub or refer to the example implementations in `/ocr`.
diff --git a/README.md b/README.md
index 26505c8..a03bebe 100644
--- a/README.md
+++ b/README.md
@@ -57,53 +57,19 @@ brew tap yourusername/liteparse
brew install liteparse
```
-### Library Usage
-
-Install as a dependency in your project:
-
-```bash
-npm install liteparse
-# or
-pnpm add liteparse
-```
+#### Option 4: Instal from Source
-```typescript
-import { LiteParse } from 'liteparse';
+You can clone the repo and install the CLI globally from source:
-const parser = new LiteParse({ ocrEnabled: true });
-const result = await parser.parse('document.pdf');
-console.log(result.text);
```
-
-### Build from Source
-
-If you want to contribute or build from source:
-
-#### Prerequisites
-
-- Node.js 18.0.0 or higher
-- npm or pnpm package manager
-
-#### Steps
-
-```bash
-# Clone the repository
git clone https://github.com/run-llama/liteparse.git
cd liteparse
-
-# Install dependencies
-npm install
-# or
-pnpm install
-
-# Build TypeScript
npm run build
-
-# Use the CLI locally
-node dist/src/index.js parse document.pdf
+npm pack
+npm install -g ./liteparse-*.tgz
```
-## Development Usage
+## Usage
### Parse PDF Files
@@ -111,19 +77,19 @@ You can run tests without building the binary using the CLI:
```bash
# Basic parsing
-pnpm parse document.pdf
+liteparse parse document.pdf
# Parse with specific format
-pnpm parse document.pdf --format json -o output.md
+liteparse parse document.pdf --format json -o output.md
# Parse specific pages
-pnpm parse document.pdf --target-pages "1-5,10,15-20"
+liteparse parse document.pdf --target-pages "1-5,10,15-20"
# Parse without OCR
-pnpm parse document.pdf --no-ocr
+liteparse parse document.pdf --no-ocr
# Use custom config file
-pnpm parse document.pdf --config config.json
+liteparse parse document.pdf --config config.json
```
### Generate Screenshots
@@ -132,16 +98,34 @@ Screenshots are essential for LLM agents to extract visual information that text
```bash
# Screenshot all pages
-pnpm screenshot document.pdf
+liteparse screenshot document.pdf -o ./screenshots
# Screenshot specific pages
-pnpm screenshot document.pdf --pages "1,3,5"
+liteparse screenshot document.pdf --pages "1,3,5" -o ./screenshots
# Custom output directory and DPI
-pnpm screenshot document.pdf -o ./images --dpi 300
+liteparse screenshot document.pdf -o ./images --dpi 300 -o ./screenshots
# Screenshot page range
-pnpm screenshot document.pdf --pages "1-10"
+liteparse screenshot document.pdf --pages "1-10" -o ./screenshots
+```
+
+### Library Usage
+
+Install as a dependency in your project:
+
+```bash
+npm install liteparse
+# or
+pnpm add liteparse
+```
+
+```typescript
+import { LiteParse } from 'liteparse';
+
+const parser = new LiteParse({ ocrEnabled: true });
+const result = await parser.parse('document.pdf');
+console.log(result.text);
```
### CLI Options
@@ -183,8 +167,6 @@ Options:
## OCR Setup
-**OCR works out of the box!** The default Tesseract.js engine runs in-process with zero setup.
-
### Default: Tesseract.js
```bash
@@ -202,37 +184,10 @@ pnpm parse document.pdf --no-ocr
For higher accuracy or better performance, you can use an HTTP OCR server. We provide ready-to-use example wrappers for popular OCR engines:
-#### EasyOCR
-
-**Setup:**
-```bash
-cd ocr/easyocr
-docker build -t liteparse-easyocr .
-docker run -p 8828:8828 liteparse-easyocr
-```
-
-**Usage:**
-```bash
-pnpm parse document.pdf --ocr-server-url http://localhost:8828/ocr
-```
-
-#### PaddleOCR
-
-**Setup:**
-```bash
-cd ocr/paddleocr
-docker build -t liteparse-paddleocr .
-docker run -p 8829:8829 liteparse-paddleocr
-```
-
-**Usage:**
-```bash
-pnpm parse document.pdf --ocr-server-url http://localhost:8829/ocr --ocr-language zh
-```
+- [EasyOCR](ocr/easyocr/README.md)
+- [PaddleOCR](ocr/paddleocr/README.md)
-### Custom OCR Server
-
-You can integrate **any OCR service** by implementing the simple LiteParse OCR API specification (see `OCR_API_SPEC.md`).
+You can integrate any OCR service by implementing the simple LiteParse OCR API specification (see [`OCR_API_SPEC.md`](OCR_API_SPEC.md)).
The API requires:
- POST `/ocr` endpoint
@@ -254,7 +209,8 @@ LiteParse supports **automatic conversion** of various document formats to PDF b
- **PowerPoint**: `.ppt`, `.pptx`, `.pptm`, `.odp`
- **Spreadsheets**: `.xls`, `.xlsx`, `.xlsm`, `.ods`, `.csv`, `.tsv`
-**Setup:**
+Just install the dependency and LiteParse will automatically convert these formats to PDF for parsing:
+
```bash
# macOS
brew install --cask libreoffice
@@ -263,22 +219,11 @@ brew install --cask libreoffice
apt-get install libreoffice
```
-**Usage:**
-```bash
-# Parse Word document
-pnpm parse report.docx -o report.txt
-
-# Parse PowerPoint
-pnpm parse slides.pptx --format json -o slides.json
-
-# Parse Excel spreadsheet
-pnpm parse data.xlsx -o data.txt
-```
-
#### Images (via ImageMagick)
- **Formats**: `.jpg`, `.jpeg`, `.png`, `.gif`, `.bmp`, `.tiff`, `.webp`, `.svg`
-**Setup:**
+Just install ImageMagick and LiteParse will convert images to PDF for parsing (with OCR):
+
```bash
# macOS
brew install imagemagick
@@ -287,15 +232,6 @@ brew install imagemagick
apt-get install imagemagick
```
-**Usage:**
-```bash
-# Parse image (automatically runs OCR)
-pnpm parse receipt.jpg --ocr-engine tesseract -o receipt.txt
-
-# Parse scanned diagram
-pnpm parse diagram.png --format json -o diagram.json
-```
-
## Configuration
You can configure parsing options via CLI flags or a JSON config file. The config file allows you to set sensible defaults and override as needed.
@@ -336,231 +272,22 @@ Use with:
pnpm parse document.pdf --config liteparse.config.json
```
-## Programmatic Usage
-
-### Installation
-
-```bash
-npm install liteparse
-# or
-pnpm add liteparse
-```
-
-### Basic Example
-
-```typescript
-import { LiteParse, type LiteParseConfig } from 'liteparse';
-
-// Default configuration: uses built-in Tesseract OCR
-const parser = new LiteParse({
- outputFormat: 'json',
- ocrEnabled: true,
- tableDetection: true,
-});
-
-// Parse a PDF file
-const result = await parser.parse('document.pdf');
-
-// Access parsed text
-console.log(result.text);
-
-// Access structured data
-console.log(result.json);
-
-// Access individual pages
-for (const page of result.pages) {
- console.log(`Page ${page.page}: ${page.text}`);
- console.log(`Text items:`, page.textItems);
- console.log(`Tables:`, page.tables);
-}
-```
-
-### Using HTTP OCR Server
-
-```typescript
-import { LiteParse } from 'liteparse';
-
-// Configure with HTTP OCR server (EasyOCR, PaddleOCR, or custom)
-const parser = new LiteParse({
- ocrServerUrl: 'http://localhost:8828/ocr',
- ocrLanguage: 'zh',
- outputFormat: 'json',
-});
-
-const result = await parser.parse('chinese-document.pdf');
-console.log(result.text);
-```
-
-### Generating Screenshots
-
-```typescript
-import { LiteParse } from 'liteparse';
-import fs from 'fs/promises';
-
-const parser = new LiteParse({ dpi: 300 });
-
-// Generate screenshots for specific pages
-const screenshots = await parser.screenshot('document.pdf', [1, 2, 3]);
-
-// Save screenshots
-for (const screenshot of screenshots) {
- const filename = `page_${screenshot.pageNum}.png`;
- await fs.writeFile(filename, screenshot.imageBuffer);
- console.log(`Saved: ${filename} (${screenshot.width}x${screenshot.height})`);
-}
-
-// Generate screenshots for all pages (omit page numbers)
-const allScreenshots = await parser.screenshot('document.pdf');
-```
-
-### Configuration Options
-
-```typescript
-import { LiteParse, type LiteParseConfig } from 'liteparse';
-
-const config: LiteParseConfig = {
- // Output format
- outputFormat: 'json', // 'json' | 'text'
-
- // OCR configuration
- ocrEnabled: true,
- ocrServerUrl: 'http://localhost:8828/ocr', // Optional HTTP server
- ocrLanguage: 'en', // Language code
-
- // Parsing options
- maxPages: 1000,
- targetPages: '1-10,15', // Specific pages to parse
- dpi: 150, // Resolution for rendering
-
- // Feature toggles
- tableDetection: true,
- preciseBoundingBox: true,
- skipDiagonalText: false,
- preserveVerySmallText: false,
-};
-
-const parser = new LiteParse(config);
-```
-
-### TypeScript Types
-
-```typescript
-import {
- LiteParse,
- type LiteParseConfig,
- type ParseResult,
- type PageResult,
- type TextItem,
- type BoundingBox,
- type TableItem,
- type ImageItem,
-} from 'liteparse';
-
-// ParseResult structure
-const result: ParseResult = {
- text: string, // Plain text content
- json: object, // Structured JSON data
- pages: PageResult[], // Per-page results
-};
-
-// PageResult structure
-const page: PageResult = {
- page: number, // Page number (1-indexed)
- width: number, // Page width in points
- height: number, // Page height in points
- text: string, // Page text content
- textItems: TextItem[], // Individual text items with positions
- boundingBoxes: BoundingBox[], // Text bounding boxes
- tables: TableItem[], // Detected tables
- images: ImageItem[], // Embedded images
-};
-```
-
-## Output Formats
-
-### JSON Format
-
-```json
-{
- "pages": [
- {
- "page": 1,
- "width": 612,
- "height": 792,
- "text": "Page text content...",
- "textItems": [
- {
- "text": "Hello",
- "x": 100,
- "y": 200,
- "width": 50,
- "height": 12,
- "fontName": "Arial",
- "fontSize": 12
- }
- ],
- "boundingBoxes": [...],
- "tables": [...],
- "images": [...]
- }
- ]
-}
-```
-
-### Text Format
-
-Plain text with page separators:
-
-```
---- Page 1 ---
-Page text content...
-
---- Page 2 ---
-More text...
-```
-
-## Architecture
-
-```
-liteparse/
-├── src/
-│ ├── core/ # Configuration and main Parser class
-│ ├── engines/ # PDF and OCR engine abstractions
-│ │ ├── pdf/ # PDF.js engine
-│ │ └── ocr/ # OCR engines (Tesseract, HTTP)
-│ ├── processing/ # Core processing pipeline
-│ │ ├── grid.ts # Spatial text projection
-│ │ ├── bbox.ts # Bounding box building
-│ │ └── tables.ts # Table detection
-│ ├── output/ # Output formatters
-│ └── vendor/ # Bundled PDF.js
-├── cli/ # Command-line interface
-├── ocr/ # Example OCR servers
-│ ├── easyocr/ # EasyOCR wrapper server
-│ └── paddleocr/ # PaddleOCR wrapper server
-├── OCR_API_SPEC.md # Standard OCR API specification
-└── examples/ # Usage examples
-```
-
-The OCR system uses a simple client-server model:
-- **Client**: Single HTTP OCR engine that calls standard API
-- **Server**: Your choice (EasyOCR, PaddleOCR, custom) conforming to API spec
-- **Default**: Built-in Tesseract.js (no server needed)
-
## Development
+We provide a fairly rich `AGENTS.md`/`CLAUDE.md` that we recommend using to help with development + coding agents.
+
```bash
# Install dependencies
-pnpm install
+npm install
# Build TypeScript
-pnpm build
+npm run build
# Watch mode
-pnpm dev
+npm run dev
# Test parsing
-pnpm test
+npm test
```
## License
@@ -576,7 +303,3 @@ Built on top of:
- [EasyOCR](https://github.com/JaidedAI/EasyOCR) - HTTP OCR server (optional)
- [PaddleOCR](https://github.com/PaddlePaddle/PaddleOCR) - HTTP OCR server (optional)
- [Sharp](https://github.com/lovell/sharp) - Image processing
-
-## Contributing
-
-Contributions welcome! This is Phase 1 of the implementation. See `ROADMAP.md` for planned features.