Skip to content

Instantly share code, notes, and snippets.

Last active December 17, 2023 21:17
Show Gist options
  • Save mbostock/6466603 to your computer and use it in GitHub Desktop.
Save mbostock/6466603 to your computer and use it in GitHub Desktop.
Save SVG as PNG
license: gpl-3.0

Say you have an SVG built dynamically on the client by D3. Now say you want to save it to a static PNG as a fallback for old browsers. There are lots of ways to do this using alternative renderers (such as Batik and PhantomJS), but what if you want your browser to do it, so that you the static PNG matches the dynamic SVG exactly, down to the subpixel, including transparency?

Here’s how.

1. Save as SVG.

Use the SVG Crowbar bookmarklet to save the SVG to a file. Call this file “fallback.svg”. Open the SVG in your browser to verify that it looks right.

SVG Crowbar helpfully inlines any stylesheets you may have on the page, but you might find you need to edit a few of the styles by hand to get things to look right. For example, the font-family “sans-serif” won’t work in an SVG image, even though it works when the SVG is built client-side; you’ll have to make the font name explicit, such as “Helvetica” or “Arial”.

2. Convert SVG to PNG.

Create and save the following HTML file as “fallback.html”:

<!DOCTYPE html>
<meta charset="utf-8">
<canvas width="640" height="3250"></canvas>

var canvas = document.querySelector("canvas"),
    context = canvas.getContext("2d");

var image = new Image;
image.src = "fallback.svg";
image.onload = function() {
  context.drawImage(image, 0, 0);

  var a = document.createElement("a"); = "fallback.png";
  a.href = canvas.toDataURL("image/png");;


Now start a local webserver in the same directory as your fallback.html file. For example:

python -m SimpleHTTPServer 8008 &

Lastly, visit http://localhost:8008/fallback.html, and it will automatically download fallback.png! Hooray.

Note: if you are using Chrome or Safari, you’ll need to download Chrome Canary due to this recently-fixed WebKit bug.

Copy link

I just made a similar one:
Wouldn't it be better to use the image dimensions for the canvas and to assign the image src after the onload function?

Copy link

FYI: I have created an online SVG converter using this code:

Copy link

@runemadsen exactly what I was looking for 👍

Copy link

I'm running Win10Prox64 on my laptop with Apache2 server (httpd) installed and had no problems with the Chrome browser.

Copy link

Super useful. Thanks a lot!

Copy link

Wonderful Magic.

Copy link

cmullaparthi commented May 12, 2018


I'm trying to use the technique above with the latest Chrome. I've tried it on Ubuntu, Centos and OS X and I get the following error from chrome and the png is not produced. The web server logs show that it is downloading the HTML and the SVG file, but no PNG is produced. Any hints on how to troubleshoot this?

$ chrome --headless --remote-debugging-port=9222 --disable-gpu http://localhost:8008/fallback.html

[0512/] Lost UI shared context.

DevTools listening on ws://

Copy link

Finally, I can properly render the svg to a png! Thank You!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment