Skip to content
SnapDOMTry SnapStudioGitHub★ 8K
How-To Recipe

Capture an iframe as an image

Include a same-origin iframe in an element capture while preserving the frame’s visible viewport and surrounding styles.

TL;DR

Capture the parent container normally. Same-origin iframe documents are accessible; cross-origin iframe contents are not.

Capture the panel with its iframe

Install @zumer/snapdom, select the rendered element, and capture it:

import { snapdom } from '@zumer/snapdom'

const panel = document.querySelector('#panel-with-frame')
const image = await snapdom.toPng(panel, {
  placeholders: true,
  embedFonts: true,
})
document.body.appendChild(image)

Same-origin frames are inlined, cross-origin frames become placeholders

SnapDOM reads an accessible iframe document, captures its visible content and places the result back into the parent snapshot. A cross-origin frame cannot be read, so it is drawn as a striped placeholder of the same size (the default) and result.warnings records iframe-cross-origin. Enable debug: true to log it; the parent layout keeps the frame's space.

Capture a same-origin iframe

The embedded srcdoc frame is same-origin and can be included in the parent capture.

SAME-ORIGIN FRAME
The captured result will appear here.

Limits and common mistakes

Wait for the frame to load before capturing its parent. The same-origin policy prevents reading third-party iframe documents; a sandboxed frame without allow-same-origin is inaccessible too.

Frequently asked questions

Can I capture a YouTube or third-party iframe?

Not directly when it is cross-origin. The browser blocks access to its document.

What happens to inaccessible frames?

They become a striped placeholder the size of the frame, so the surrounding layout is unchanged and the unavailable content is explicit. That is the default; pass placeholders: false for an invisible spacer of the same size.