UNPKG

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.

84 lines (83 loc) 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DOCUMENT_INIT_PARAMS_DEFAULTS = exports.STANDARD_FONTS_RELATIVE_URL = exports.CMAP_RELATIVE_URL = exports.PDF_TO_PNG_OPTIONS_DEFAULTS = exports.SEQUENTIAL_PIPELINE_WINDOW = exports.MAX_CONCURRENCY_LIMIT = exports.MAX_INPUT_BYTES = exports.MAX_CANVAS_PIXELS = exports.MAX_VIEWPORT_SCALE = void 0; /** * 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. */ exports.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. */ exports.MAX_CANVAS_PIXELS = 100_000_000; /** * 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`. */ exports.MAX_INPUT_BYTES = 256 * 1024 * 1024; /** * 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. */ exports.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. */ exports.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. */ exports.PDF_TO_PNG_OPTIONS_DEFAULTS = { viewportScale: 1, disableFontFace: true, useSystemFonts: false, enableXfa: true, /** Used as the output filename stem when the PDF is supplied as a buffer rather than a file path. */ outputFileMask: 'buffer', pdfFilePassword: undefined, concurrencyLimit: 4, maxInputBytes: exports.MAX_INPUT_BYTES, }; /** * 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. */ exports.CMAP_RELATIVE_URL = './node_modules/pdfjs-dist/cmaps/'; exports.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`. */ exports.DOCUMENT_INIT_PARAMS_DEFAULTS = { cMapUrl: exports.CMAP_RELATIVE_URL, cMapPacked: true, standardFontDataUrl: exports.STANDARD_FONTS_RELATIVE_URL, }; // Test-only asset lists (STANDARD_FONTS, STANDARD_CMAPS) live in __tests__/test-data-constants.ts