Skip to content

A fast, helpful, and open-source document parser

perf: disable image extraction when --no-ocr

Logan Markewicha703c0e9fc3a
diff --git a/src/core/parser.ts b/src/core/parser.ts
index a7b7439..2557c0f 100644
--- a/src/core/parser.ts
+++ b/src/core/parser.ts
@@ -162,7 +162,8 @@ export class LiteParse {
const pages = await this.pdfEngine.extractAllPages(
doc,
this.config.maxPages,
- this.config.targetPages
+ this.config.targetPages,
+ { extractImages: this.config.ocrEnabled }
);
// run BEFORE grid projection
@@ -310,7 +311,7 @@ export class LiteParse {
log(`Rendering page ${pageNum}...`);
const imageBuffer = await renderer.renderPageToBuffer(pdfInput, pageNum, this.config.dpi);
- const pageData = await this.pdfEngine.extractPage(doc, pageNum);
+ const pageData = await this.pdfEngine.extractPage(doc, pageNum, { extractImages: false });
results.push({
pageNum,
diff --git a/src/engines/pdf/interface.ts b/src/engines/pdf/interface.ts
index 36e6844..94bd0ab 100644
--- a/src/engines/pdf/interface.ts
+++ b/src/engines/pdf/interface.ts
@@ -1,10 +1,21 @@
import { TextItem } from "../../core/types.js";
+/** Options for page extraction */
+export interface ExtractOptions {
+ /** Whether to extract embedded image bounds (needed for OCR). Default: true */
+ extractImages?: boolean;
+}
+
export interface PdfEngine {
name: string;
loadDocument(input: string | Uint8Array, password?: string): Promise<PdfDocument>;
- extractPage(doc: PdfDocument, pageNum: number): Promise<PageData>;
- extractAllPages(doc: PdfDocument, maxPages?: number, targetPages?: string): Promise<PageData[]>;
+ extractPage(doc: PdfDocument, pageNum: number, options?: ExtractOptions): Promise<PageData>;
+ extractAllPages(
+ doc: PdfDocument,
+ maxPages?: number,
+ targetPages?: string,
+ options?: ExtractOptions
+ ): Promise<PageData[]>;
renderPageImage(
doc: PdfDocument,
pageNum: number,
diff --git a/src/engines/pdf/pdfjs.ts b/src/engines/pdf/pdfjs.ts
index 210b12a..287084b 100644
--- a/src/engines/pdf/pdfjs.ts
+++ b/src/engines/pdf/pdfjs.ts
@@ -1,5 +1,13 @@
import fs from "node:fs/promises";
-import { PdfEngine, PdfDocument, PageData, Image, Annotation, BoundingBox } from "./interface.js";
+import {
+ PdfEngine,
+ PdfDocument,
+ PageData,
+ Image,
+ Annotation,
+ BoundingBox,
+ ExtractOptions,
+} from "./interface.js";
import { TextItem } from "../../core/types.js";
import { PdfiumRenderer } from "./pdfium-renderer.js";
import { importPdfJs } from "./pdfjsImporter.js";
@@ -795,7 +803,7 @@ export class PdfJsEngine implements PdfEngine {
} as PdfJsExtendedDocument;
}
- async extractPage(doc: PdfDocument, pageNum: number): Promise<PageData> {
+ async extractPage(doc: PdfDocument, pageNum: number, options?: ExtractOptions): Promise<PageData> {
const pdfDocument = (doc as PdfJsExtendedDocument)._pdfDocument;
const page = await pdfDocument.getPage(pageNum);
@@ -942,21 +950,23 @@ export class PdfJsEngine implements PdfEngine {
}
let images: Image[] = [];
- try {
- const pdfInput = this.currentPdfPath || this.currentPdfData || doc.data;
- if (!this.pdfiumRenderer) {
- this.pdfiumRenderer = new PdfiumRenderer();
- await this.pdfiumRenderer.loadDocument(pdfInput);
+ if (options?.extractImages !== false) {
+ try {
+ const pdfInput = this.currentPdfPath || this.currentPdfData || doc.data;
+ if (!this.pdfiumRenderer) {
+ this.pdfiumRenderer = new PdfiumRenderer();
+ await this.pdfiumRenderer.loadDocument(pdfInput);
+ }
+ 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
}
- 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
@@ -1027,7 +1037,8 @@ export class PdfJsEngine implements PdfEngine {
async extractAllPages(
doc: PdfDocument,
maxPages?: number,
- targetPages?: string
+ targetPages?: string,
+ options?: ExtractOptions
): Promise<PageData[]> {
const numPages = Math.min(doc.numPages, maxPages || doc.numPages);
@@ -1048,7 +1059,7 @@ export class PdfJsEngine implements PdfEngine {
if (maxPages && pages.length >= maxPages) {
break;
}
- const pageData = await this.extractPage(doc, pageNum);
+ const pageData = await this.extractPage(doc, pageNum, options);
pages.push(pageData);
}