Skip to content
HTML to Image & Video

How to fix tainted canvas and CORS errors when exporting HTML to an image

If your export throws a security error or shows blank images, a cross-origin resource tainted the canvas. Here is why, and four ways to fix it.

Updated · 5 min read

You export an HTML layout to an image and either nothing downloads, or the picture has empty spaces where images should be. In the console you may see a message that tainted canvases may not be exported. This is a browser security rule, not a bug in the library you use.

Why it happens

Browsers do not let a page read pixels from a canvas if any image drawn on it came from another origin without permission. Reading those pixels could leak private data, so the browser marks the canvas as tainted and blocks export. Snapshot libraries also have to fetch your images and fonts again to inline them, and that fetch needs the other server to allow it with CORS headers.

Fix 1: Embed the image as a data URL

A Base64 data URL lives inside your HTML, so there is no cross-origin request at all. This is the most reliable fix, and it is what we recommend for exports.

function fileToDataUrl(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = () => resolve(reader.result);
    reader.onerror = reject;
    reader.readAsDataURL(file);
  });
}

Use this with a file the user picks from their device. Fetching a remote image to convert it still needs CORS permission, so it will not help for pictures on servers you do not control.

Fix 2: Allow CORS on the image server

If you control the server or bucket that hosts the images, send an Access-Control-Allow-Origin header and mark the image as cross-origin in your HTML.

<img src="https://cdn.example.com/photo.jpg" crossorigin="anonymous" alt="" />
Access-Control-Allow-Origin: https://your-site.com

Fix 3: Serve the image from your own origin

Copy the image into your project or proxy it through a route on your own domain. Same-origin images never taint the canvas.

Fix 4: Inline your fonts

Fonts loaded from another website can fail the same way and quietly fall back to a default font. Embed the font file in your CSS as a Base64 @font-face rule so the snapshot always has it.

A quick checklist

  • Open the network panel and look for blocked or failed image and font requests.
  • Replace remote images with data URLs while you test.
  • Check that every cross-origin image has crossorigin and a matching CORS header.
  • Export again at a smaller size if the error mentions memory or canvas size.

Try it: the converter warns you when an image is blocked

Open HTML to PNG

More guides