Examples

HTML to JPEG with JavaScript and Puppeteer

Install Puppeteer

npm install puppeteer

Installing the puppeteer package also downloads a compatible Chrome for Testing build, so there is nothing else to set up. The examples use ES modules — run them with node file.mjs, or set "type": "module" in your package.json.

Convert HTML to JPG and save to file

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent('<html><body><h1>Hello World</h1></body></html>');
await page.screenshot({ path: 'html.jpg', type: 'jpeg' });
await browser.close();

Find out how to change the size of the viewport with JavaScript and Puppeteer.

That was 7 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.