Capture HTML to PNG in Vue 3
Use a Vue template ref and nextTick() to capture the current DOM, then download the image or display a preview.
Bind a template ref with ref="cardRef", install @zumer/snapdom, and call snapdom.toPng(cardRef.value) from a button handler. SnapDOM returns an HTMLImageElement ready to append.
1. Install
npm install @zumer/snapdom@latest
Zero runtime dependencies and an ES module entry. In Nuxt, keep imports and capture calls inside the browser boundary.
2. Minimal capture component (Composition API)
<script setup>
import { nextTick, ref } from 'vue';
import { snapdom } from '@zumer/snapdom';
const cardRef = ref(null);
async function capture() {
await nextTick();
if (!cardRef.value) return;
const img = await snapdom.toPng(cardRef.value, { scale: 2 });
document.body.appendChild(img);
}
</script>
<template>
<div>
<div ref="cardRef" class="card">
<h3>Hello SnapDOM</h3>
<p>This whole div will be captured.</p>
</div>
<button @click="capture">Capture</button>
</div>
</template>
The template ref name (cardRef) must match the variable declared in script setup. nextTick() waits for pending Vue DOM updates; wait separately for application data and layout-changing assets.
Try it live
This plain HTML demo captures a rendered card. In Vue, cardRef.value supplies the element to the same SnapDOM call.
Hello SnapDOM
This whole div will be captured.
3. Download instead of appending
async function download() {
await nextTick();
if (!cardRef.value) return;
const result = await snapdom(cardRef.value);
await result.download({ format: 'png', filename: 'card' });
}
4. Render a live preview
Capture to a Blob and create an object URL to use as the preview <img> source:
<script setup>
import { nextTick, onUnmounted, ref } from 'vue';
import { snapdom } from '@zumer/snapdom';
const cardRef = ref(null);
const previewUrl = ref('');
onUnmounted(() => {
if (previewUrl.value) URL.revokeObjectURL(previewUrl.value);
});
async function capture() {
await nextTick();
if (!cardRef.value) return;
const blob = await snapdom.toBlob(cardRef.value, { format: 'png' });
if (previewUrl.value) URL.revokeObjectURL(previewUrl.value);
previewUrl.value = URL.createObjectURL(blob);
}
</script>
<template>
<div ref="cardRef">…</div>
<button @click="capture">Capture</button>
<img v-if="previewUrl" :src="previewUrl" alt="preview" />
</template>
The handler releases the previous preview URL; onUnmounted releases the final one.
5. Reusable composable
// useSnapdom.js
import { nextTick, ref } from 'vue';
import { snapdom } from '@zumer/snapdom';
export function useSnapdom(options = {}) {
const target = ref(null);
async function capture(format = 'png') {
await nextTick();
if (!target.value) return null;
const result = await snapdom(target.value, options);
return result.to(format);
}
return { target, capture };
}
<script setup> import { useSnapdom } from './useSnapdom'; const { target, capture } = useSnapdom({ scale: 2, embedFonts: true }); </script> <template> <div ref="target">…</div> <button @click="capture('png')">Capture</button> </template>
Choose the Vue export target
Keep the ref on the visible card, invoice or dashboard, and keep the export controls outside that root. Use the invoice export recipe for a printable report or the dashboard recipe for several widgets. After nextTick() and asset readiness, the PNG Blob recipe provides a file for your upload endpoint.
Common gotchas
The captured image is blank
Make sure you trigger the capture from a user event (click) or inside onMounted — never directly inside setup(). The element ref is only assigned after the component has been mounted.
Also confirm the captured element isn't v-if'd off the page. SnapDOM needs the element to be in the live DOM tree.
Web fonts aren't being captured
SnapDOM defaults to embedFonts: 'auto' and embeds used web fonts when their files are accessible. Wait for application data and fonts to settle before capture if they can change the layout.
How do I use SnapDOM with the Options API?
Bind ref="cardRef" in the template and access this.$refs.cardRef in a method:
const img = await snapdom.toPng(this.$refs.cardRef);
Does SnapDOM work with Nuxt?
Capture in a browser event handler. In Nuxt, load SnapDOM with a dynamic import inside that handler or inside onMounted; <ClientOnly> can keep the capture UI out of server rendering.
Cross-origin images don't render
Use the useProxy option pointing to a CORS proxy: { useProxy: 'https://your-proxy.example.com/' }.