$("#download").click(function() { var svg = $('svg')[0]; var svg_xml = (new XMLSerializer).serializeToString(svg); var base64 = btoa(unescape(encodeURIComponent(svg_xml))); var data_uri = "data:image/svg+xml;base64," + base64; var image = new Image; image.src = data_uri; image.onload = function(){ var canvas = document.createElement("canvas"); canvas.width = image.width; canvas.height = image.height; var context = canvas.getContext("2d"); context.clearRect(0, 0, image.width, image.height); context.drawImage(image, 0, 0); var title = $.trim($('input[name=title]').val()); if(title.length == 0){ title = 'charts_udpwork_com'; } var a = document.createElement("a"); a.download = title + ".png"; a.href = canvas.toDataURL("image/png"); a.target = '_blank'; a.click(); }; });