User Agents

Web browsers and everyday web tech, explained

Fix dom-to-image Security Errors on iOS Safari

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

If you're seeing security errors when using dom-to-image on iOS browsers (Safari, Chrome for iOS), the usual culprit is the canvas being tainted by cross-origin images or fonts. This guide walks you through the exact fixes to get your image export working again.

What You'll Need

  • Access to the HTML/CSS/JS source of the page that uses dom-to-image
  • A text editor or browser developer tools (Safari's Web Inspector or Chrome DevTools)
  • Basic understanding of JavaScript and CORS

Step-by-Step

  1. Identify the error message. Open your page in iOS Safari, trigger the export, and check the console (Web Inspector → Console). Common errors: SecurityError: The operation is insecure, Unable to get image data from canvas because the canvas has been tainted by cross-origin data.
  2. Check for cross-origin images. Look for any <img> tags or CSS background images that load from a different domain (e.g., a CDN). dom-to-image reads pixels from the canvas, and if any image is not CORS-enabled, the canvas becomes tainted.
  3. Add crossOrigin="anonymous" to all image elements. For each <img> that might be cross-origin, add the attribute. For dynamically created images, set img.crossOrigin = 'anonymous' before setting the src.
  4. Ensure the server sends CORS headers. The image server must respond with Access-Control-Allow-Origin: * (or your domain). If you control the server, add this header. If not, you may need to proxy the image through your own server.
  5. Handle CSS background images. For background images, you can't set crossOrigin directly. Instead, preload them with an Image object that has crossOrigin set, then use that image as a data URL in your CSS. Alternatively, replace background images with inline SVG or data URIs.
  6. Check for web fonts. Fonts loaded from Google Fonts or other CDNs can also taint the canvas. Use @font-face with src: url('font.woff2') format('woff2') and ensure the font server sends CORS headers. Or embed fonts as base64 in your CSS.
  7. Use the cacheBust option. In dom-to-image, pass { cacheBust: true } to avoid cached images that might be loaded without CORS. This forces a fresh request with the crossOrigin attribute.
  8. Test on a clean page. Create a minimal test page with just a single image and dom-to-image to isolate the issue. If it works, gradually add elements until it breaks.
  9. Consider an alternative library. If dom-to-image continues to fail, try html-to-image (a fork) or modern-screenshot, which handle CORS more robustly.

Common Mistakes

  • Setting crossOrigin after src. The crossOrigin attribute must be set before the image loads. If you set it after, it won't help.
  • Forgetting about CSS background images. Many developers only fix <img> tags and miss background images, which also taint the canvas.
  • Assuming all CDNs send CORS headers. Some do, but many don't. Always verify with the network tab.
  • Using useCORS: true incorrectly. In dom-to-image, the option is useCORS (not crossOrigin). Make sure you pass it correctly: domtoimage.toPng(node, { useCORS: true }).
  • Ignoring the iOS-specific quirks. iOS Safari is stricter than desktop browsers. Always test on a real iOS device, not just the simulator.

Tips

  • Use SVG filters for images. If you can, replace raster images with SVG elements, which don't taint the canvas.
  • Preload images before export. Use Promise.all to ensure all images are fully loaded and CORS-fetched before calling dom-to-image.
  • Add a fallback. If the export fails, show an alert and suggest the user try a different browser or update iOS.
  • Keep your dom-to-image version updated. Newer versions may have fixed iOS-specific issues.
  • Debug with a simple image. Start with a plain <img> from the same domain to confirm the library works, then add complexity.

Troubleshooting

If the error persists after adding crossOrigin:
Check the Network tab in Web Inspector. Look for the image request—if it has a crossorigin attribute but the response doesn't include Access-Control-Allow-Origin, the server isn't sending it. You'll need to proxy the image or find a CORS-enabled source.

If the exported image is blank or black:
This often happens when a font or image fails to load but doesn't throw an error. Check the console for 404s or CORS warnings. Also ensure the node you're exporting has explicit dimensions—if it's 0x0, you'll get a blank image.

If it works on desktop but not iOS:
iOS Safari has stricter same-origin policies and may block certain data URLs. Try converting the output to a blob and then to a data URL, or use toBlob instead of toPng.

If the error is about SecurityError in general:
Check if you're trying to export a node that contains an iframe or a video element. These are not supported by dom-to-image and will always cause security errors. Remove them or replace with a static placeholder.

Frequently Asked Questions

Why does dom-to-image work on Android but not iOS?

iOS Safari enforces stricter tainting rules for canvas. Android Chrome is more lenient, but the same CORS issues exist—they just might not surface as errors.

Can I use a data URL for images to avoid CORS?

Yes, if you convert images to data URLs (base64) before rendering, they are same-origin and won't taint the canvas. But be careful with large images—they can bloat the page.

Does useCORS: true automatically fix everything?

No, it only tells dom-to-image to try to fetch images with CORS. If the server doesn't send the right headers, it still fails.

What is a tainted canvas?

When a canvas contains any cross-origin image without proper CORS, the browser marks it as 'tainted' and blocks reading its pixel data for security reasons.

Can I just disable the security check?

No, browsers don't allow disabling this for security reasons. The only workaround is to ensure all resources are same-origin or CORS-enabled.

Is there a way to export without canvas?

You could use a library like html2canvas (which has similar issues) or generate SVG manually, but canvas is the standard approach.