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.
42 lines (41 loc) • 2.72 kB
TypeScript
import type { PDFDocumentProxy } from 'pdfjs-dist';
import type { InMemoryPngPageOutput, MetadataPngPageOutput, PageRotation } from './interfaces/index.js';
/**
* Converts a (possibly fractional) viewport length into the integer pixel count the canvas
* will actually allocate. `@napi-rs/canvas` truncates fractional dimensions toward zero when a
* canvas is constructed (e.g. a 892.5-wide viewport yields an 892 px bitmap), so the reported
* `width`/`height` must be floored to match the rendered PNG. Viewport lengths are always
* non-negative, so `Math.floor` is equivalent to that truncation. Flooring here keeps the
* render path and the `returnMetadataOnly` path reporting identical, image-accurate pixel sizes.
*
* @internal Exported for unit testing only; not part of the public API (`src/index.ts`).
*/
export declare function toPixelDimension(viewportLength: number): number;
/**
* Builds the error thrown when floored page dimensions collapse to a non-renderable size.
*
* A very small `viewportScale` (e.g. `0.001` on a 612 pt page) floors to `0` px. A 0-wide or
* 0-tall bitmap cannot be rendered, so both the render and `returnMetadataOnly` paths reject it
* with this identical, actionable message instead of either returning a phantom `0×0` metadata
* result or surfacing an opaque canvas-factory `AssertionError`.
*
* @internal
*/
export declare function nonRenderableDimensionsError(width: number, height: number): Error;
/**
* Builds the error thrown when a page's rendered (floored) canvas area exceeds `MAX_CANVAS_PIXELS`.
*
* The guard compares the floored bitmap that is actually allocated — `floor(width) × floor(height)`
* — against the limit, so it bounds real canvas memory rather than the slightly larger fractional
* viewport area. A page this large cannot be rendered: `renderPdfPage` rejects it before allocating
* a canvas to avoid OOM. The `returnMetadataOnly` path shares this guard so it reports the same
* outcome a render would — rejecting an unrenderable page rather than returning phantom dimensions
* for it. Mirrors {@link nonRenderableDimensionsError}, which keeps the two paths symmetric on the
* lower bound.
*
* @internal
*/
export declare function canvasPixelLimitError(canvasWidth: number, canvasHeight: number): Error;
export declare function normalizeRotation(raw: number): PageRotation;
export declare function getPageMetadata(pdf: PDFDocumentProxy, pageName: string, pageNumber: number, pageViewportScale: number): Promise<MetadataPngPageOutput>;
export declare function renderPdfPage(pdf: PDFDocumentProxy, pageName: string, pageNumber: number, pageViewportScale: number, returnPageContent: boolean): Promise<InMemoryPngPageOutput>;