HTML to PNG Quote Card Designer with html-to-image — Free JavaScript Snippet

HTML to PNG Quote Card Designer (html-to-image) · Tools · Plain HTML, CSS & JS · Live preview

What's included

Features

DOM to PNG
html-to-image via SVG foreignObject.
Works in sandboxed iframes
No helper iframe, unlike html2canvas.
Embedded web fonts
CORS-mode stylesheet so rules are readable.
Exact output sizes
pixelRatio from target and on-screen width.
Social presets
1080 × 1080, 1080 × 1350 and 1200 × 630.
Transparent corners
Pixels outside the rounded card stay clear.
Font readiness
Waits for document.fonts.ready.
In-page result
Preview, dimensions and file size.

About this UI Snippet

HTML to PNG in the Browser — html-to-image, Web Fonts and Exact Sizes

Screenshot of the HTML to PNG Quote Card Designer (html-to-image) snippet rendered live

Designing an image with HTML and CSS is much faster than drawing it on a canvas by hand: layout, typography and gradients come for free. Turning that element into a PNG is the missing step. This snippet uses html-to-image to export a quote card at exact social-media sizes.

How html-to-image works

It clones the element, inlines its computed styles, embeds web fonts and images as data URLs, wraps the result in an SVG <foreignObject>, loads that SVG as an image and draws it onto a canvas. Because the browser itself renders the HTML inside foreignObject, modern CSS generally comes out as it looks on screen.

Why not html2canvas here?

html2canvas takes a different approach: it copies the whole document into a hidden iframe and repaints it with the Canvas API. In a sandboxed iframe without allow-same-origin — like this site's live preview, many CodePen-style embeds and some CMS previews — the page has an opaque origin and is not allowed to read that helper iframe, so html2canvas fails with "Blocked a frame with origin null". html-to-image never creates an iframe, so it works in the same sandbox.

Web fonts need CORS

To embed a web font, html-to-image reads the font stylesheet's cssRules. Browsers only expose rules of a cross-origin stylesheet if it was loaded in CORS mode, so the Google Fonts <link> is added with crossOrigin = 'anonymous' (Google Fonts sends the required headers). Without it, export still works but falls back to a default font and logs errors. document.fonts.ready is awaited first so the fonts are actually loaded.

Exact output size

The card is about 420 px wide on screen; a square post is 1080 px. pixelRatio: 1080 / cardWidth renders the PNG at exactly the target size, and the card's aspect-ratio follows the chosen preset.

Seeing the result

The exported PNG appears below the design with its dimensions and size, so the export is visible even where downloads are blocked.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this snippet into an AI assistant like Claude and ask it to compare the foreignObject approach with html2canvas's repaint approach, including the sandbox issue. Ask it to add an uploaded background photo, a logo, automatic text contrast, exporting several quotes as a ZIP, or copying the image to the clipboard with ClipboardItem. It can also help debug fonts that don't appear in Safari.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a quote card designer that exports PNG images with html-to-image (from a CDN) in plain HTML, CSS and JavaScript, loading Google Fonts (Fraunces italic for the quote, Inter for UI) through a link element added in JavaScript with crossOrigin set to anonymous.

Requirements:
- Controls for quote text, author, handle, four gradient themes (swatch buttons with aria-pressed) and three size presets: 1080×1080, 1080×1350 and 1200×630.
- A live card about 420 px wide whose aspect ratio follows the chosen size, with a large decorative quote mark, the quote text (shrinking for long quotes), the author and handle, a translucent circle decoration, rounded corners and a shadow.
- Export: await document.fonts.ready, then call htmlToImage.toPng on the card with pixelRatio = target width ÷ on-screen width and cache busting.
- Show the exported PNG below the design with its natural dimensions, approximate file size and a download link, and report errors in a status line.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

Requires
<div class="hc">
  <aside class="hc-panel">
    <h2>Quote card</h2>
    <label>Quote <textarea id="hcQuote" rows="4" maxlength="220">Design is not just what it looks like and feels like. Design is how it works.</textarea></label>
    <label>Author <input id="hcAuthor" value="Steve Jobs" maxlength="40"></label>
    <label>Handle <input id="hcHandle" value="@northwind.studio" maxlength="30"></label>
    <fieldset>
      <legend>Theme</legend>
      <div class="hc-themes" id="hcThemes"></div>
    </fieldset>
    <label>Size <select id="hcSize">
      <option value="1080x1080">Square post · 1080 × 1080</option>
      <option value="1080x1350">Portrait post · 1080 × 1350</option>
      <option value="1200x630">Link preview · 1200 × 630</option>
    </select></label>
    <button type="button" id="hcExport">Export PNG</button>
    <p class="hc-status" id="hcStatus" role="status"></p>
  </aside>
  <main class="hc-stage">
    <div class="hc-label">Design (live DOM)</div>
    <div class="hc-card" id="hcCard">
      <div class="hc-mark">“</div>
      <p class="hc-text" id="hcText"></p>
      <div class="hc-foot"><span class="hc-author" id="hcAuthorOut"></span><span class="hc-handle" id="hcHandleOut"></span></div>
    </div>
    <div class="hc-label" id="hcResultLabel" hidden>Exported PNG</div>
    <div class="hc-result" id="hcResult"></div>
  </main>
</div>

Step by step

How to Use

  1. 1
    Edit the quoteChange the text, author and handle; long quotes shrink to fit.
  2. 2
    Pick a theme and sizeFour gradients; square, portrait or link-preview proportions.
  3. 3
    Export PNGRendered at the exact pixel size and shown below.
  4. 4
    Check the dimensionsThe result lists width, height and file size.
  5. 5
    DownloadUse the link under the image (in its own tab if the sandbox blocks it).

Real-world uses

Common Use Cases

Social media tools
Quote, tip and announcement cards.
Open Graph images
Generate link previews from HTML.
Shareable achievements
Badges and certificates as images.
Embedded widgets
Export inside sandboxed or CMS previews.
Marketing sites
Let visitors share branded cards.
Related: jsPDF Certificate Generator
Vector output instead of pixels: jsPDF Certificate Generator with Custom Script Font.
Related: Social Post Card
A post-style card layout: Social Post Card.

Got questions?

Frequently Asked Questions

With html-to-image, call htmlToImage.toPng(element, options), which resolves to a PNG data URL. Use it as an image src or as a download link.

html2canvas copies the document into a helper iframe and then reads it. In an iframe sandboxed without allow-same-origin the page has an opaque origin, and reading the helper frame is blocked as cross-origin access. html-to-image doesn't use a helper iframe.

The font wasn't loaded yet, or its stylesheet was cross-origin without CORS. Await document.fonts.ready and load the font CSS with crossorigin="anonymous" so its rules can be read and the font embedded.

Set pixelRatio to the target width divided by the element's rendered width, and keep the element's aspect ratio equal to the target's. html-to-image also accepts canvasWidth and canvasHeight.

It works well in Chromium and Firefox. Safari has had issues with fonts and images inside foreignObject, so test your design there, sometimes calling toPng twice helps the first render load resources.