Styling errors when converting embedded SVG to png

My high level goal is to convert an element <div>containing several embedded svg images to a png file. All operations must be performed in the client browser using JavaScript. I tried:

+4
1

PNG CSS, Rickshaw ( D3). , , :

  • DIVs, SVG,
  • DIV ( SVG) html2canvas
  • CSS SVG; @thirdcreed JavaScript D3 : Rickshaw CSS/Axes JSDOM - CSS .
  • SVG ,

    var imgsrc = 'data:image/svg+xml;base64,'+ btoa(html2);
    var img = '<img src="'+imgsrc+'">';      
    var canvas = document.querySelector("canvas"),
    context = canvas.getContext("2d");      
    var image = new Image;
    image.src = imgsrc;
    image.onload = function() {
      context.drawImage(image, 0, 0);
    }
    
  • , ,
  • , :

    var canvasdata = canvas.toDataURL("image/png");
    var pngimg = '<img src="'+canvasdata+'">'; 
    d3.select("#pngdataurl").html(pngimg); // contains selector from D3, adjust if you don't use D3
    var a = document.getElementById("some_anchor"); // Fix for Firefox: supply an anchor-tag here that is 'display:none' in your document, otherwise download won't work
    a.download = "sample.png";
    a.href = canvasdata;
    a.click();
    

, , Internet Explorer, SVG xmlns.

+2

All Articles