In browser conversion of svg to png image (cross browser including IE)

Liviu Boboia

I am using Highcharts and i need to convert all the charts on the page to an image that i can send to my server so i can merge it to a master export excel which already contains some tables and pivot grids. This is my code so far

var svg = Highcharts.getSVG(charts);
img = new Image();
img.src = "data:image/svg+xml," + encodeURIComponent(svg);
mycanvas = document.createElement('canvas');
mycanvas.width = 1000
mycanvas.height = 1000
ctx = mycanvas.getContext("2d");
ctx.drawImage(img, 0, 0);
$("body").append("<image src='" + mycanvas.toDataURL("image/png") + "'/>");
$.ajax({
    type: "POST",
    url: '@Url.Action("getfile")',
    data: JSON.stringify({ file: mycanvas.toDataURL("image/png").replace("data:image/png;base64,", "") }),
    contentType: 'application/json; charset=utf-8'
});

This is the c# code where i tested if i get the data correctly from the client(i just write the base64 string to a file). Firefox and Chrome write the image correctly, IE just give me a black image

public void getfile(string file)
{
    System.IO.File.WriteAllBytes(@"c:\yourfile.png", Convert.FromBase64String(file));
}

the entire code you can find here http://jsfiddle.net/gd7bB/2291/

The image seems to be generated fine in Firefox and Chrome, but not IE(here i just get a black image). I am using a canvas which from what i gathered IE supports IE9 Support for HTML5 Canvas tag.

If you could help me figure out what i'm doing wrong, or maybe you can point the way of a better solution i would much appreciate it.

Jerome WAGNER

IE9 seem to indeed support the canvas element as per https://msdn.microsoft.com/fr-fr/library/ff975241(v=vs.85).aspx

The following sample/test code for toDataURL works for me in IE11 : http://samples.msdn.microsoft.com/Workshop/samples/canvas/todataurl.html

The fiddle you gave does not work in IE11 ; looking at the console errors, it leads to the fact that you cannot call drawImage before the image has finished its rendering. IE Throws a

call to unsupported method or attributes of a method

Adding a setTimeout allow IE to render the dynamic image and bypass this error.

But it then leads to a

Security Error

because it seems that IE has tainted the canvas because of some "cross origin" issue

SecurityError
The img or video element is not of the same origin or domain
as the document that owns the canvas element. Versions earlier
than Internet Explorer 10 use SECURITY_ERR.

IE seems to be tainting all images filled with SVG for security reasons - SVG tained canvas IE security error toDataURL

I managed to have a working version using canvg, which transforms an SVG file into canvas instructions - https://github.com/gabelerner/canvg

the solution boils down to

var svg = Highcharts.getSVG(charts);
var mycanvas = document.createElement('canvas');
canvg(mycanvas, svg)
console.log(mycanvas.toDataURL("image/png"))

check this fiddle http://jsfiddle.net/6bxqbaeb/1/ which works in IE11

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Save svg to disk as png image - browser discrepancy

From Dev

Multiple browser versions on OS X including IE

From Dev

pixels to meter conversion on browser image

From Dev

Cross browser compatibility issue of Internet Explorer 6, 7 and 8 for png/jpg image used as background of an html element

From Dev

Cross-browser still image camera capture

From Dev

Cross-Browser Image Preview Effect

From Dev

Is there a cross browser way to make an image shrink to fit?

From Dev

Cross browser Server-sent Events, or alternative, including Microsoft browsers

From Dev

SVG embedded image displays in Browser ONLY

From Dev

httphandler generated png not displaying in older browser (IE8)

From Dev

Is there any way to remove cross symbol from input fields in IE browser?

From Dev

How to align a text vertically using SVG with cross-browser compatibility?

From Dev

How to make background SVG stretch 100% with cross browser support?

From Dev

SVG CSS Transform animation, cross-browser issues

From Dev

How to make background SVG stretch 100% with cross browser support?

From Dev

Cross-browser way to force SVG fit inside div

From Dev

SVG nav-link in a Bootstrap navbar, cross-browser

From Dev

Flot renders png image with a different background color than it does in browser

From Dev

php - show png/image in browser works in localhost but not on aws

From Dev

How can I display png data as an image inside in the browser?

From Dev

Flot renders png image with a different background color than it does in browser

From Dev

header('Content-type: image/png'); makes the browser all black

From Dev

Most browser-supported method to change the color of a simple PNG image?

From Dev

SVG to PNG Conversion in javascript

From Dev

100% width background image with an 'auto' height (2015) - Cross browser

From Dev

Cross-browser replacement of HTML checkbox with highlighted image

From Dev

Cross Browser perspective solution?

From Dev

Cross Browser Instance in Chrome

From Dev

Cross browser function identity

Related Related

  1. 1

    Save svg to disk as png image - browser discrepancy

  2. 2

    Multiple browser versions on OS X including IE

  3. 3

    pixels to meter conversion on browser image

  4. 4

    Cross browser compatibility issue of Internet Explorer 6, 7 and 8 for png/jpg image used as background of an html element

  5. 5

    Cross-browser still image camera capture

  6. 6

    Cross-Browser Image Preview Effect

  7. 7

    Is there a cross browser way to make an image shrink to fit?

  8. 8

    Cross browser Server-sent Events, or alternative, including Microsoft browsers

  9. 9

    SVG embedded image displays in Browser ONLY

  10. 10

    httphandler generated png not displaying in older browser (IE8)

  11. 11

    Is there any way to remove cross symbol from input fields in IE browser?

  12. 12

    How to align a text vertically using SVG with cross-browser compatibility?

  13. 13

    How to make background SVG stretch 100% with cross browser support?

  14. 14

    SVG CSS Transform animation, cross-browser issues

  15. 15

    How to make background SVG stretch 100% with cross browser support?

  16. 16

    Cross-browser way to force SVG fit inside div

  17. 17

    SVG nav-link in a Bootstrap navbar, cross-browser

  18. 18

    Flot renders png image with a different background color than it does in browser

  19. 19

    php - show png/image in browser works in localhost but not on aws

  20. 20

    How can I display png data as an image inside in the browser?

  21. 21

    Flot renders png image with a different background color than it does in browser

  22. 22

    header('Content-type: image/png'); makes the browser all black

  23. 23

    Most browser-supported method to change the color of a simple PNG image?

  24. 24

    SVG to PNG Conversion in javascript

  25. 25

    100% width background image with an 'auto' height (2015) - Cross browser

  26. 26

    Cross-browser replacement of HTML checkbox with highlighted image

  27. 27

    Cross Browser perspective solution?

  28. 28

    Cross Browser Instance in Chrome

  29. 29

    Cross browser function identity

HotTag

Archive