Tutorial

How to Convert HTML to an Image

Turn raw HTML code into a PNG, JPEG, or WebP image. Perfect for generating email templates, invoices, and social cards.

Steps

  1. Host or encode your HTMLPut your HTML on a URL (even localhost via tunnel) or use a data URI for simple HTML snippets.
  2. Capture with SnapAPIPoint SnapAPI at the URL hosting your HTML. It renders in a real Chrome browser with full CSS support.
  3. Crop with selectorsUse the selector parameter to capture just the element you care about — skip surrounding chrome.

Code Examples

# Capture HTML hosted locally (use ngrok or similar for localhost)
curl "https://snapapi.tech/v1/screenshot?url=https://your-html-host.com/invoice.html&api_key=YOUR_KEY&selector=%23invoice&format=png" -o invoice.png
import requests

# Capture a specific element from your HTML page
response = requests.get("https://snapapi.tech/v1/screenshot", params={
    "url": "https://your-html-host.com/invoice.html",
    "api_key": "YOUR_KEY",
    "selector": "#invoice",
    "format": "png"
})
with open("invoice.png", "wb") as f:
    f.write(response.content)
const res = await fetch(
  "https://snapapi.tech/v1/screenshot?" + new URLSearchParams({
    url: "https://your-html-host.com/invoice.html",
    api_key: "YOUR_KEY",
    selector: "#invoice",
    format: "png"
  })
);
require("fs").writeFileSync("invoice.png", Buffer.from(await res.arrayBuffer()));
Get Free API KeyFull API Docs