Skip to main content

Pdf.Rasterizer

Examples below assume this module is imported with an imports: entry under alias Pdf. Kind references follow that alias — substitute your own if you import it under a different name.

Renders one page of a PDF to image bytes (PNG by default; JPEG or WebP to shrink the payload for a vision model) via pdf.js on a server-side canvas (@napi-rs/canvas). The reported width/height are the rendered pixel dimensions at the configured scale — the coordinate space downstream consumers (Pdf.FormFields, Image.Overlay) measure against.


Example

kind: Pdf.Rasterizer
metadata: { name: Render }
scale: 2
- name: page
inputs:
data: "${{ steps.fetch.result.bytes }}"
page: 1
invoke: !ref Render

Configuration

FieldTypeRequiredDescription
scalenumberno (default 1)Render scale over the PDF's native size (1 ≈ 72 DPI), capped at 8. Pin to the same value as the Pdf.FormFields consuming the measured coordinates.
formatpng | jpeg | webpno (default png)Output image format. A per-invocation format takes precedence.
qualityintegerno (default 80)Encoder quality (1–100) for the lossy formats; ignored for png.

Invocation inputs

InputTypeRequiredDescription
dataUint8ArrayyesBuffered PDF bytes (e.g. the bytes produced by Octet.Decoder, or an S3.Get body collected to bytes).
pageintegerno (default 1)1-based page number to render.
formatpng | jpeg | webpnoOutput image format. Takes precedence over the resource-level format.
qualityintegernoEncoder quality (1–100) for the lossy formats; ignored for png.

Output

FieldTypeDescription
imageUint8ArrayRendered page as buffered image bytes in the chosen format.
widthintegerRendered image width in pixels (page width × scale).
heightintegerRendered image height in pixels (page height × scale).
pageCountintegerTotal number of pages in the document.
scalenumberThe render scale the image was produced at — wire it into a Pdf.FormFields invocation's scale so the coordinate contract is a value, not a convention.
mediaTypestringMIME type of the encoded image (image/png, image/jpeg, or image/webp) — wire into a vision message's image part.

Errors

CodeRaised when
ERR_INVALID_INPUTdata is not a Uint8Array, the bytes are not parseable as a PDF, page exceeds the document's page count, format is not png/jpeg/webp, or quality is not an integer between 1 and 100.