Skip to main content
jg-packages

Search

    HTML to PNG

    jg-render

    Render prepared HTML to exact PNGs in offline Chromium, with verified fonts and images, transparency, and a caller-owned layout check.

    v1.0.0Node.jsESMMIT

    npm

    npm install jg-render

    pnpm

    pnpm add jg-render

    Usage

    withOfflineRenderer creates and closes one context for a batch. Chromium itself belongs to the caller. The context disables document JavaScript and service workers, denies network access, reduces motion and renders at one pixel per CSS pixel.

    Supply an HTML document with inline styles and data URLs for assets. The renderer waits for declared fonts and decodes image elements. Use the optional layout callback for project-specific checks or adjustments after assets load. Return 'overflow' from that callback to report a layout that does not fit. The callback receives a live page and should use its caller’s cancellation signal for any external work.

    Use renderPng for PNG bytes and preparePage to run a trusted callback on a prepared page, for example to call Playwright’s PDF API. Each page closes after its callback settles, times out or is cancelled. A render defaults to 15 seconds and accepts dimensions up to 8192 on either side and a total of 33,554,432 pixels.

    Errors

    Code Message
    INVALID_HTML_RENDER_OPTIONS The HTML render options are invalid.
    HTML_RENDER_FAILED The HTML could not be rendered to PNG.

    The package’s errors carry a fixed data.reason without document source, resource URLs or asset bytes. Errors from the caller’s session or prepared-page callback propagate to the caller.

    Generated from TSDoc

    API reference

    Every export of [email protected], 6 in all, with its signature, its errors, and an example.

    Functions

    withOfflineRenderer

    function

    Runs one rendering session in a disposable context on caller-owned Chromium. Pages are offline, document JavaScript and service workers are disabled, motion is reduced and the pixel scale is one.

    Signature
    withOfflineRenderer<T>(
      browser: Browser,
      options: IOfflineRendererOptions,
      work: (renderer: IOfflineRenderer) => Promise<T>,
    ): Promise<T>

    Parameters

    browserBrowser

    The caller's launched Playwright Chromium.

    optionsIOfflineRendererOptions

    Safe context defaults and optional cancellation.

    work(renderer: IOfflineRenderer) => Promise<T>

    The renders or trusted operations to perform before the session is closed.

    Returns

    Promise<T>

    The caller's result after closing the owned context.

    Throws

    • INVALID_HTML_RENDER_OPTIONSThe HTML render options are invalid.
    • HTML_RENDER_FAILEDThe HTML could not be rendered to PNG.
    Example
    import { chromium } from 'playwright';
    import { withOfflineRenderer } from 'jg-render';
    
    const browser = await chromium.launch();
    try {
      const png = await withOfflineRenderer(browser, {}, (render) =>
        render.renderPng({
          html: '<!doctype html><svg width="80" height="80"><circle cx="40" cy="40" r="24"/></svg>',
          width: 80,
          height: 80,
          transparent: true,
        }),
      );
      // png contains an 80 by 80 image
      void png;
    } finally {
      await browser.close();
    }

    Constants

    ERRORS

    const

    stable public error codes

    Declaration
    const ERRORS: { INVALID_HTML_RENDER_OPTIONS: "INVALID_HTML_RENDER_OPTIONS"; HTML_RENDER_FAILED: "HTML_RENDER_FAILED" }

    Types

    IHtmlRenderInput

    type

    a prepared HTML document, its exact canvas, and the assets it is expected to use

    Declaration
    type IHtmlRenderInput = {
        html: string;
        width: number;
        height: number;
        requiredFonts?: IRequiredRenderFont[];
        transparent?: boolean;
        colorScheme?: BrowserContextOptions['colorScheme'];
        timeoutMs?: number;
        layout?: (page: Page) => Promise<void | 'overflow'>;
    };

    Members

    • htmlproperty
      string
    • widthproperty
      number
    • heightproperty
      number
    • requiredFontsproperty
      IRequiredRenderFont[]
    • transparentproperty
      boolean
    • colorSchemeproperty
      BrowserContextOptions["colorScheme"]
    • timeoutMsproperty
      number
    • layoutproperty
      (page: Page) => Promise<void | "overflow">

    IOfflineRenderer

    type

    the browser operations available inside one offline session

    Declaration
    type IOfflineRenderer = {
        preparePage<T>(input: IHtmlRenderInput, usePage: (page: Page) => Promise<T>): Promise<T>;
        renderPng(input: IHtmlRenderInput): Promise<Buffer>;
    };

    Members

    • preparePagemethod
      preparePage<T>(input: IHtmlRenderInput, usePage: (page: Page) => Promise<T>): Promise<T>

      Prepares a page and passes it to trusted code, such as a PDF producer, until the callback settles.

    • renderPngmethod
      renderPng(input: IHtmlRenderInput): Promise<Buffer<ArrayBufferLike>>

      Prepares a page and returns its exact-size PNG bytes.

    IOfflineRendererOptions

    type

    optional context defaults for trusted callers; offline and pixel settings are owned by the renderer

    Declaration
    type IOfflineRendererOptions = Pick<BrowserContextOptions, 'colorScheme' | 'locale' | 'timezoneId'> & {
        signal?: AbortSignal;
    };

    IRequiredRenderFont

    type

    each required

    Declaration
    type IRequiredRenderFont = {
        family: string;
        weight?: string;
        style?: string;
    };

    Members

    • familyproperty
      string
    • weightproperty
      string
    • styleproperty
      string