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.
77 lines (76 loc) • 4.24 kB
TypeScript
import type { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
/**
* Maximum allowed value for `viewportScale`. Values above this limit would produce canvases
* so large (an A4 page at scale 100 already yields ~5×10⁹ pixels) that they risk OOM crashes
* before the pixel-count guard in `processPdfPage` can fire.
*/
export declare const MAX_VIEWPORT_SCALE = 100;
/**
* Maximum canvas area in pixels. At 4 bytes per pixel, 100 MP ≈ 400 MB of raw bitmap memory.
* Any page whose rendered (floored) canvas area exceeds this limit is rejected before canvas
* allocation. The guard floors the viewport dimensions first so it bounds the bitmap actually
* allocated rather than the slightly larger fractional viewport area.
*/
export declare const MAX_CANVAS_PIXELS = 100000000;
/**
* Default upper bound on input PDF size in bytes. 256 MiB is a generous ceiling for legitimate
* PDFs while keeping a single conversion well below typical service container memory limits.
* The path branch of `getPdfFileBuffer()` runs `stat()` before `readFile()` so this also blocks
* unbounded reads from `/dev/zero`, FIFOs, sockets, and other non-regular files.
* Callers can override with `PdfToPngOptions.maxInputBytes`.
*/
export declare const MAX_INPUT_BYTES: number;
/**
* Upper bound on `concurrencyLimit` when `processPagesInParallel` is `true`. At this cap,
* peak in-flight canvas memory ≈ `16 × MAX_CANVAS_PIXELS × 4 bytes` ≈ 6.4 GiB, which is a
* defensible ceiling for typical Node.js service containers while still allowing realistic
* parallelism. Higher values would let a single conversion exhaust container memory.
*/
export declare const MAX_CONCURRENCY_LIMIT = 16;
/**
* Sliding-window size used for sequential (non-parallel) conversions. The window lets the
* off-thread PNG encode and disk write of finished pages overlap the main-thread render of the
* next page. Result order is preserved by the window helper; rendered pixels are unaffected.
*
* Sized 3 by paired A/B measurement: on text-heavy documents PNG encode costs ~2× render, and a
* second in-flight encode recovers a small but consistent margin (TAMReview.pdf: −2…−5% median
* end-to-end vs window 2 in interleaved trials, faster in 3 of 4 pairs; larger single-run
* differences did not replicate under controlled re-measurement). Window 4 measured within
* noise of 3. Peak cost: up to three live canvases.
*/
export declare const SEQUENTIAL_PIPELINE_WINDOW = 3;
/**
* Default values applied to `PdfToPngOptions` fields that are not explicitly set by the caller.
* These are also used as the source of truth for documented defaults in JSDoc comments on the type.
*/
export declare const PDF_TO_PNG_OPTIONS_DEFAULTS: {
viewportScale: number;
disableFontFace: boolean;
useSystemFonts: boolean;
enableXfa: boolean;
/** Used as the output filename stem when the PDF is supplied as a buffer rather than a file path. */
outputFileMask: string;
pdfFilePassword: undefined;
concurrencyLimit: number;
maxInputBytes: number;
};
/**
* Relative paths to the pdfjs-dist asset directories.
* Stored as raw strings so they can be resolved against `process.cwd()` at call time
* (inside `propsToPdfDocInitParams`) rather than at module-load time. This ensures
* applications that call `process.chdir()` after importing the library still get
* correct paths.
*/
export declare const CMAP_RELATIVE_URL = "./node_modules/pdfjs-dist/cmaps/";
export declare const STANDARD_FONTS_RELATIVE_URL = "./node_modules/pdfjs-dist/standard_fonts/";
/**
* Default pdfjs `DocumentInitParameters` used when initialising a PDF document.
* - `cMapUrl` / `cMapPacked`: point to the pre-packed character maps bundled with `pdfjs-dist`,
* required for rendering CJK and other non-Latin PDFs correctly.
* - `standardFontDataUrl`: path to the standard Type 1 / TrueType fonts bundled with `pdfjs-dist`,
* used as fallbacks when a PDF does not embed its fonts.
*
* Note: these values are raw relative paths. They are resolved to absolute paths at call time
* by `propsToPdfDocInitParams` via `normalizePath`.
*/
export declare const DOCUMENT_INIT_PARAMS_DEFAULTS: DocumentInitParameters;