Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

html2canvas leaves out images from another domain or taints the canvas

Your error

Tainted canvases may not be exported.2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The image server does not send Access-Control-Allow-Origin

Allow GET from your site's origin on the bucket or server that holds the images.

Fix
[
  {
    "AllowedHeaders": [],
    "AllowedMethods": ["GET"],
    "AllowedOrigins": ["https://app.example.com"],
    "ExposeHeaders": []
  }
]
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

html2canvas can only copy an image from another domain into the canvas when that server sends Access-Control-Allow-Origin and you pass useCORS: true. Without both, the image is skipped, or with allowTaint: true it taints the canvas and toDataURL throws a SecurityError. Add the header on the image host, or route images through a proxy (html2canvas 1.4.1).

  • Applies to html2canvas 1.4.1 (current npm release), Amazon S3 bucket CORS rules
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Ask the image host whether it allows your page's origin. No Access-Control-Allow-Origin line means the server side is missing (cause 1). If the header comes back, the html2canvas options in your code decide between cause 2 and cause 3.

Run this first
curl -sI -H "Origin: https://app.example.com" https://cdn.example.com/images/logo.png | grep -i access-control-allow-origin

Then find the line you see in this list and open its fix:

  1. 1The image server does not send Access-Control-Allow-OriginThe curl check prints nothing, and the console shows a CORS block for the image URL once useCORS is on
  2. 2useCORS is still false, so html2canvas never asks for CORSThe header is present, but cross-origin images are blank in the output and no error is thrown
  3. 3allowTaint: true lets the image in but blocks the exportThe image is drawn, then toDataURL, toBlob or a PDF export built on the canvas fails with Tainted canvases may not be exported.

What does the error look like?

  • Tainted canvases may not be exported.
  • Reason: CORS header 'Access-Control-Allow-Origin' missing
  • No 'Access-Control-Allow-Origin' header is present on the requested resource.

Cause 1 of 3

The image server does not send Access-Control-Allow-Origin

You seeThe curl check prints nothing, and the console shows a CORS block for the image URL once useCORS is on

Check

A CORS error from html2canvas on external images usually means the host never approved cross-origin reads. MDN explains that a canvas can only use a foreign image as if it were same-origin when the image is requested with crossorigin and the server answers with a suitable Access-Control-Allow-Origin header.

Check
curl -sI -H "Origin: https://app.example.com" https://cdn.example.com/images/logo.png

Fix

Allow GET from your site's origin on the bucket or server that holds the images. On Amazon S3 this is a CORS rule in the bucket's permissions; on your own server, add the header to image responses.

Fix
[
  {
    "AllowedHeaders": [],
    "AllowedMethods": ["GET"],
    "AllowedOrigins": ["https://app.example.com"],
    "ExposeHeaders": []
  }
]

Verify

The curl check now prints access-control-allow-origin with your origin, and the image appears in the rendered canvas.

Verify
curl -sI -H "Origin: https://app.example.com" https://cdn.example.com/images/logo.png | grep -i access-control-allow-origin

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

useCORS is still false, so html2canvas never asks for CORS

You seeThe header is present, but cross-origin images are blank in the output and no error is thrown

Check

html2canvas defaults useCORS to false and allowTaint to false. In that state it checks each image, sees that a foreign one would taint the canvas, and simply does not draw it. A correct server header changes nothing until the library is told to request the image with CORS.

Check
grep -rn "html2canvas(" src/ -A4

Fix

Turn on useCORS and leave allowTaint at its default. html2canvas then loads the image in CORS mode, and the server's header lets it into the canvas without tainting it.

Fix
import html2canvas from "html2canvas";

const canvas = await html2canvas(document.querySelector("#report"), {
  useCORS: true,
});
const png = canvas.toDataURL("image/png");

Verify

The external image shows in the canvas and toDataURL returns a data:image/png string.

Verify
console.log(png.slice(0, 22));

Confirmed by html2canvas.hertzen.com

Did this fix it?

Cause 3 of 3

allowTaint: true lets the image in but blocks the export

You seeThe image is drawn, then toDataURL, toBlob or a PDF export built on the canvas fails with Tainted canvases may not be exported.

Check

Setting allowTaint draws foreign images without CORS approval, which taints the canvas. MDN lists getImageData(), toBlob(), toDataURL() and captureStream() as calls that throw a SecurityError on a tainted canvas, so the screenshot can be shown but never saved.

Check
grep -rn "allowTaint" src/

Fix

Remove allowTaint and use useCORS with the header from cause 1. When you cannot change the image host, pass the proxy option so html2canvas fetches foreign images through a server on your own origin.

Fix
const canvas = await html2canvas(element, {
  useCORS: true,
  proxy: "https://app.example.com/html2canvas-proxy",
});

Verify

Saving the image or PDF works, and the console shows no SecurityError.

Verify
canvas.toBlob((blob) => console.log(blob.type, blob.size));

Confirmed by html2canvas.hertzen.com

Did this fix it?

How do I stop it coming back?

  • Serve images that end up in screenshots or PDFs from your own domain or a CDN with a CORS rule set up for it.
  • Keep useCORS: true in one shared html2canvas options object and never add allowTaint when the result has to be exported.
  • Add the curl Origin check for each image host to your deploy checklist, so a new bucket does not ship without its CORS rule.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.