A fast, helpful, and open-source document parser
undo CLI arg changes, make image filtering more broad, make types make sense
Logan Markewich710a604ec617
diff --git a/cli/parse.ts b/cli/parse.tsindex 38a1a55..b5ee4c6 100644--- a/cli/parse.ts+++ b/cli/parse.ts@@ -26,7 +26,6 @@ interface ParseCommandOptions {dpi?: string;preciseBbox?: boolean;preserveSmallText?: boolean;- detectImages?: boolean;password?: string;config?: string;quiet?: boolean;@@ -82,7 +81,6 @@ program.option("--dpi <dpi>", "DPI for rendering", DEFAULT_DPI.toString()).option("--no-precise-bbox", "Disable precise bounding boxes").option("--preserve-small-text", "Preserve very small text")- .option("--detect-images", "Detect embedded images (logos, photos, charts) via PDFium").option("--password <password>", "Password for encrypted/protected documents").option("--config <file>", "Config file (JSON)").option("-q, --quiet", "Suppress progress output")@@ -127,7 +125,6 @@ programdpi: parseInt(options.dpi || DEFAULT_DPI.toString()),preciseBoundingBox: options.preciseBbox !== false,preserveVerySmallText: options.preserveSmallText || false,- detectImages: options.detectImages || false,password: options.password,};diff --git a/src/core/config.ts b/src/core/config.tsindex 902c140..a9bc5e8 100644--- a/src/core/config.ts+++ b/src/core/config.ts@@ -21,7 +21,6 @@ export const DEFAULT_CONFIG: LiteParseConfig = {preserveVerySmallText: false,preserveLayoutAlignmentAcrossPages: false,password: undefined,- detectImages: false,};export function mergeConfig(userConfig: Partial<LiteParseConfig>): LiteParseConfig {diff --git a/src/core/parser.ts b/src/core/parser.tsindex 3911c47..ddb35af 100644--- a/src/core/parser.ts+++ b/src/core/parser.ts@@ -70,7 +70,7 @@ export class LiteParse {this.config = mergeConfig(userConfig);// Initialize PDF engine- this.pdfEngine = new PdfJsEngine({ detectImages: this.config.detectImages });+ this.pdfEngine = new PdfJsEngine();// Initialize OCR engine// Auto-detect: use HTTP OCR if URL provided, otherwise use Tesseractdiff --git a/src/core/types.ts b/src/core/types.tsindex d1d9995..a32593b 100644--- a/src/core/types.ts+++ b/src/core/types.ts@@ -136,15 +136,6 @@ export interface LiteParseConfig {* @defaultValue `undefined`*/password?: string;-- /**- * Detect embedded images (logos, photos, charts) using PDFium and populate- * the `images` array on each page. When enabled, this also allows OCR to- * trigger on pages that contain images but have sufficient native text.- *- * @defaultValue `false`- */- detectImages: boolean;}/**diff --git a/src/engines/pdf/pdfium-renderer.ts b/src/engines/pdf/pdfium-renderer.tsindex 2b4f293..80b17b9 100644--- a/src/engines/pdf/pdfium-renderer.ts+++ b/src/engines/pdf/pdfium-renderer.ts@@ -2,6 +2,37 @@ import { PDFiumLibrary, type PDFiumPageRenderOptions } from "@hyzyla/pdfium";import sharp from "sharp";import { promises as fs } from "fs";+/**+ * Minimal interface for PDFium's WASM module internals.+ * These properties are private/protected in @hyzyla/pdfium's typings,+ * but we need direct access for low-level page object bound queries.+ */+interface PdfiumWasmModule {+ _FPDFPageObj_GetBounds?: (+ objHandle: number,+ leftPtr: number,+ bottomPtr: number,+ rightPtr: number,+ topPtr: number+ ) => number;+ _FPDF_GetPageWidthF: (pagePtr: number) => number;+ _FPDF_GetPageHeightF: (pagePtr: number) => number;+ _malloc: (size: number) => number;+ _free: (ptr: number) => void;+ HEAPU8: Uint8Array;+}++/** Minimum image dimension in PDF points to be considered for OCR */+const MIN_IMAGE_SIZE_PT = 25;+/** Images covering more than this fraction of the page are treated as backgrounds */+const MAX_IMAGE_PAGE_COVERAGE = 0.9;++interface PdfiumPageInternal {+ module: PdfiumWasmModule;+ pageIdx: number;+ objects(): Iterable<{ type: string; objectIdx: number }>;+}+/*** PDFium-based PDF screenshot renderer* Uses native PDFium library for high-quality, fast screenshots@@ -91,30 +122,27 @@ export class PdfiumRenderer {const document = await this.pdfium.loadDocument(pdfBuffer, password);try {- const page = document.getPage(pageNumber - 1);+ // Cast to internal interface — module/pageIdx/objectIdx are private in+ // @hyzyla/pdfium typings but required for low-level WASM bound queries+ const page = document.getPage(pageNumber - 1) as unknown as PdfiumPageInternal;const results: Array<{ x: number; y: number; width: number; height: number }> = [];- // Access the underlying WASM module for low-level bound queries- // eslint-disable-next-line @typescript-eslint/no-explicit-any- const mod = (page as any).module;- // eslint-disable-next-line @typescript-eslint/no-explicit-any- const pagePtr = (page as any).pageIdx;+ const mod = page.module;+ const pagePtr = page.pageIdx;if (!mod || !mod._FPDFPageObj_GetBounds) {return results;}// Get page dimensions via WASM (getSize() is private in typings)- const pageWidth: number = mod._FPDF_GetPageWidthF(pagePtr);- const pageHeight: number = mod._FPDF_GetPageHeightF(pagePtr);+ const pageWidth = mod._FPDF_GetPageWidthF(pagePtr);+ const pageHeight = mod._FPDF_GetPageHeightF(pagePtr);// Iterate page objects looking for imagesfor (const obj of page.objects()) {if (obj.type !== "image") continue;- // objectIdx is the WASM object handle/pointer directly- // eslint-disable-next-line @typescript-eslint/no-explicit-any- const objHandle = (obj as any).objectIdx;+ const objHandle = obj.objectIdx;if (!objHandle) continue;// Allocate memory for 4 floats (left, bottom, right, top)@@ -133,9 +161,9 @@ export class PdfiumRenderer {const w = right - left;const h = top - bottom;- // Filter out tiny images (< 10pt) and full-page backgrounds (> 90% coverage)- if (w < 10 || h < 10) continue;- if (w > pageWidth * 0.9 && h > pageHeight * 0.9) continue;+ if (w < MIN_IMAGE_SIZE_PT || h < MIN_IMAGE_SIZE_PT) continue;+ if (w > pageWidth * MAX_IMAGE_PAGE_COVERAGE && h > pageHeight * MAX_IMAGE_PAGE_COVERAGE)+ continue;// Convert PDF coords (Y-up) to viewport coords (Y-down)results.push({diff --git a/src/engines/pdf/pdfjs.ts b/src/engines/pdf/pdfjs.tsindex c233e43..06001c2 100644--- a/src/engines/pdf/pdfjs.ts+++ b/src/engines/pdf/pdfjs.ts@@ -432,11 +432,6 @@ export class PdfJsEngine implements PdfEngine {private pdfiumRenderer: PdfiumRenderer | null = null;private currentPdfPath: string | null = null;private currentPdfData: Uint8Array | null = null;- private detectImages: boolean;-- constructor(options?: { detectImages?: boolean }) {- this.detectImages = options?.detectImages ?? false;- }async loadDocument(input: string | Uint8Array, password?: string): Promise<PdfDocument> {let data: Uint8Array;@@ -605,22 +600,20 @@ export class PdfJsEngine implements PdfEngine {}let images: Image[] = [];- if (this.detectImages) {- try {- if (!this.pdfiumRenderer) {- this.pdfiumRenderer = new PdfiumRenderer();- }- const pdfInput = this.currentPdfPath || this.currentPdfData || doc.data;- const imageBounds = await this.pdfiumRenderer.extractImageBounds(pdfInput, pageNum);- images = imageBounds.map((bounds) => ({- x: bounds.x,- y: bounds.y,- width: bounds.width,- height: bounds.height,- }));- } catch {- // Image extraction is best-effort+ try {+ if (!this.pdfiumRenderer) {+ this.pdfiumRenderer = new PdfiumRenderer();}+ const pdfInput = this.currentPdfPath || this.currentPdfData || doc.data;+ const imageBounds = await this.pdfiumRenderer.extractImageBounds(pdfInput, pageNum);+ images = imageBounds.map((bounds) => ({+ x: bounds.x,+ y: bounds.y,+ width: bounds.width,+ height: bounds.height,+ }));+ } catch {+ // Image extraction is best-effort}// Skip annotation extraction - not currently used in processing pipeline