Examples

Screenshot of a particular size with JavaScript and Playwright

Install Playwright and Chromium

npm install playwright
npx playwright install chromium

The examples use ES modules — run them with node file.mjs, or set "type": "module" in your package.json.

Take a screenshot of the resized viewport and save to file

import { chromium } from 'playwright';

const browser = await chromium.launch();
const page = await browser.newPage();
await page.setViewportSize({ width: 480, height: 240 });
await page.goto('https://www.htmltoimage.com/demo');
await page.screenshot({ path: 'size.png' });
await browser.close();

Find out how to take a full-page screenshot with JavaScript and Playwright.

That was 8 lines of code — plus a browser to run in production

Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:

curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com"

Try the Urlbox screenshot API — free trial, no browser babysitting.