withOfflineRenderer
functionRuns 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.
withOfflineRenderer<T>(
browser: Browser,
options: IOfflineRendererOptions,
work: (renderer: IOfflineRenderer) => Promise<T>,
): Promise<T>Parameters
browserBrowserThe caller's launched Playwright Chromium.
optionsIOfflineRendererOptionsSafe 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.
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();
}