Generate a social card from HTML
Design the card with ordinary HTML and CSS, preview it live, then export the rendered component as a shareable image.
Give the card its intended CSS dimensions, wait for fonts and images, then export a PNG. Export width/height resize the output without reflowing the card.
Render the card to PNG
Install @zumer/snapdom, select the rendered element, and capture it:
import { snapdom } from '@zumer/snapdom'
await document.fonts.ready
const card = document.querySelector('#social-card')
await Promise.all(Array.from(card.querySelectorAll('img'), img => img.decode()))
const image = await snapdom.toPng(card, {
width: 1200,
height: 630,
dpr: 1,
embedFonts: true,
backgroundColor: '#ffffff',
})
document.querySelector('#preview').appendChild(image)The image matches the card the user saw
This pattern works well for user-created share cards inside browser apps because the exported image matches the card preview. For an upload, call snapdom.toBlob(card, { format: 'png', width: 1200, height: 630, dpr: 1 }); the preview above returns an image element.
Generate a social card
Edit the headline inside the card, then export the rendered card.
Limits and common mistakes
Open Graph crawlers cannot run a user’s browser capture on demand. Your application still needs to store the generated image at a public URL and reference it from og:image.
Frequently asked questions
Can SnapDOM generate OG images on a Node server?
No. SnapDOM requires a real browser DOM. Use a browser worker or a server-side renderer for build-time generation.
What size should an Open Graph card be?
A common target is 1200 by 630 pixels, but verify the requirements of each destination.