pdf-to-png-converter
Version:
Node.js utility to convert PDF file/buffer pages to PNG files/buffers. No build-time compilation required — pre-built native binaries included for all major platforms.
38 lines (37 loc) • 2.19 kB
TypeScript
import type { PageRotation } from './interfaces/index.js';
import type { WorkerDocumentOptions } from './interfaces/worker.protocol.js';
/** One page-render assignment; `index` is the position in the conversion's ordered task list. */
export interface WorkerPageTask {
index: number;
pageNumber: number;
pageName: string;
}
/** A page as rendered inside a worker, re-materialized on the main thread. */
export interface WorkerRenderedPage {
pageNumber: number;
name: string;
width: number;
height: number;
rotation: PageRotation;
content: Buffer | undefined;
}
/**
* Renders pages across a pool of worker threads.
*
* Each worker receives its own structured-clone copy of `pdfBuffer` (pdf.js documents cannot be
* shared across threads) and loads the document once. Tasks are dispatched dynamically — a worker
* gets its next page as soon as it finishes one — so heavy pages don't stall the queue behind
* them. `onPageRendered` runs on the main thread per completed page (this is where file-mode
* output is written through the existing sink, keeping every path-security guard main-side);
* an error it throws is attributed to that page's index. The pool never settles while an
* `onPageRendered` call is still pending — no write escapes the function's lifetime.
*
* Failure semantics: after the first error no new pages are dispatched and in-flight pages
* settle. Per-page failures (a page that fails to render, or whose output write fails) are
* collected by index and the LOWEST-index one is thrown — mirroring
* `processPagesWithSlidingWindow`. Worker-level failures (document load failure, worker crash,
* startup failure, unexpected exit) are FATAL: the first one is thrown with priority over any
* per-page error, regardless of what the crashed worker was doing at the time. Workers are
* always terminated before this function settles.
*/
export declare function renderPagesInWorkerPool(pdfBuffer: Uint8Array, documentOptions: WorkerDocumentOptions, materializeContent: boolean, tasks: WorkerPageTask[], poolSize: number, onPageRendered: (index: number, page: WorkerRenderedPage) => Promise<void>): Promise<void>;