How to convert HTML to PNG with JavaScript
Three reliable ways to turn HTML and CSS into a PNG image: in the browser with a DOM snapshot library, with html2canvas, or on a server with a headless browser.
Updated · 6 min read
Turning a piece of HTML into an image is a common need. You might want a social card, a documentation screenshot, a receipt, or a chart you can attach to an email. JavaScript gives you three practical routes, and the right one depends on where the code runs and how exact the result must be.
Method 1: Snapshot the DOM in the browser
Libraries such as modern-screenshot and html-to-image clone a DOM node, inline its styles, fonts and images, wrap the result in an SVG foreignObject, and draw that SVG onto a canvas. Because the browser itself renders the content, fidelity is high for modern CSS such as grid, flexbox, gradients and custom properties.
import { domToPng } from 'modern-screenshot';
const node = document.querySelector('#card');
const dataUrl = await domToPng(node, { scale: 2 });
const link = document.createElement('a');
link.download = 'card.png';
link.href = dataUrl;
link.click();The scale option sets the pixel ratio, so a scale of 2 produces an image with twice the pixels in each direction. This is how you get sharp output for retina screens.
Method 2: html2canvas
html2canvas takes a different approach. It reads the DOM and computed styles and redraws the page onto a canvas with its own renderer. That avoids foreignObject, but it means any CSS feature the library has not implemented can look different from the real page.
import html2canvas from 'html2canvas';
const canvas = await html2canvas(document.querySelector('#card'), {
scale: 2,
backgroundColor: null,
});
canvas.toBlob((blob) => {
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.download = 'card.png';
link.href = url;
link.click();
URL.revokeObjectURL(url);
});Setting backgroundColor to null keeps transparent areas transparent in the PNG.
Method 3: A headless browser on a server
When you need pixel-exact output, automation, or want to render pages for many users, run a real browser on the server. Puppeteer and Playwright both load your HTML in Chromium and capture it.
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1200, height: 630, deviceScaleFactor: 2 });
await page.setContent(html, { waitUntil: 'networkidle0' });
await page.screenshot({ path: 'card.png', omitBackground: true });
await browser.close();The cost is infrastructure. You need to host a browser, manage memory, and secure any HTML that comes from users.
Which method should you use?
- Use a DOM snapshot library when users export from a web page and you want no server cost.
- Use html2canvas if you already depend on it and your CSS is simple.
- Use Puppeteer or Playwright for automated pipelines, scheduled reports and exact rendering.
- Use a ready-made converter if you only need the image and not the code.
Problems you will probably hit
- Images from other websites can be blank or throw a tainted canvas error unless they allow cross-origin access.
- Web fonts must be embedded, or the snapshot may fall back to a default font.
- Content outside the visible area is cropped unless you set explicit width and height.
- Very large outputs fail because browsers limit canvas size, roughly 16,384 pixels per side.
Skip the code and convert HTML to PNG in your browser
Open HTML to PNG