afpp
Version:
Async Fast PDF Parser for Node.js — dependency-light, TypeScript-first, production-ready.
70 lines (69 loc) • 3.02 kB
TypeScript
import { Canvas, CanvasRenderingContext2D } from '@napi-rs/canvas';
export declare enum PROCESSING_TYPE {
IMAGE = "IMAGE",
MIXED = "MIXED",
TEXT = "TEXT"
}
export interface AfppParseOptions {
/**
* Concurrency level for page processing. Defaults to 1.
* Higher values may improve performance but increase memory usage.
* Set to 'auto' to use the number of available CPU cores (capped at 8).
* @default 1
*/
concurrency?: number | 'auto';
/**
* Image encoding format when rendering non-text pages. Defaults to 'png'.
* Supported formats: 'avif', 'jpeg', 'png', 'webp'.
* @default 'png'
*/
imageEncoding?: ImageEncoding;
/**
* Password for encrypted pdf files.
*/
password?: string;
/**
* Scale factor for image rendering. Defaults to 1.0.
* - 1.0: Standard quality (72 DPI equivalent)
* - 2.0: High quality (144 DPI equivalent, 4x memory)
* - 3.0: Print quality (216 DPI equivalent, 9x memory)
* @default 1.0
*/
scale?: number;
}
export interface CanvasAndContext {
canvas: Canvas;
context: CanvasRenderingContext2D;
}
export type ImageEncoding = 'avif' | 'jpeg' | 'png' | 'webp';
export type PageProcessor<T> = (content: Buffer | string, pageNumber: number, pageCount: number) => Promise<T> | T;
export interface PdfCanvasFactory {
create(width: number, height: number): CanvasAndContext;
destroy(canvasAndContext: CanvasAndContext): void;
reset(canvasAndContext: CanvasAndContext, width: number, height: number): void;
}
/**
* Result yielded by streaming PDF parser
*/
export interface StreamingResult<T> {
data: T;
pageCount: number;
pageNumber: number;
}
export declare function parsePdfFile(type: PROCESSING_TYPE.IMAGE, input: Buffer | string | Uint8Array | URL, options?: AfppParseOptions, callback?: undefined): Promise<Buffer[]>;
export declare function parsePdfFile(type: PROCESSING_TYPE.TEXT, input: Buffer | string | Uint8Array | URL, options?: AfppParseOptions, callback?: undefined): Promise<string[]>;
export declare function parsePdfFile<T>(type: PROCESSING_TYPE.MIXED, input: Buffer | string | Uint8Array | URL, options: AfppParseOptions, callback: PageProcessor<T>): Promise<T[]>;
/**
* Streaming PDF parser that yields results as pages are processed.
* Useful for large PDFs where you want to process pages as they become available
* rather than waiting for all pages to complete.
*
* @example
* ```typescript
* for await (const { pageNumber, data } of streamPdfFile(PROCESSING_TYPE.IMAGE, './large.pdf')) {
* await saveImage(data, `page-${pageNumber}.png`);
* }
* ```
*/
export declare function streamPdfFile(type: PROCESSING_TYPE.IMAGE, input: Buffer | string | Uint8Array | URL, options?: AfppParseOptions): AsyncGenerator<StreamingResult<Buffer>>;
export declare function streamPdfFile(type: PROCESSING_TYPE.TEXT, input: Buffer | string | Uint8Array | URL, options?: AfppParseOptions): AsyncGenerator<StreamingResult<string>>;