Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Styles do not show up #182

Closed
marinagavr opened this issue Jul 22, 2018 · 5 comments
Closed

Styles do not show up #182

marinagavr opened this issue Jul 22, 2018 · 5 comments

Comments

@marinagavr
Copy link

marinagavr commented Jul 22, 2018

Hi Eric, I followed your guidelines trying to export a D3-generated tree into an image. It works, except the styles do not show up in the image.

var svgElem = document.querySelector('svg');
addInlineStyles(svgElem) ;
setSVGAttributes(svgElem) ;
var prependStr = '<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'		
var svgString = prependStr + new XMLSerializer().serializeToString(svgElem);
var image = new Image();
image.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(svgString))); 
image.onload = function() {
  var canvas = document.createElement('canvas');
  canvas.width = image.width;
  canvas.height = image.height;
  var context = canvas.getContext('2d');
  context.drawImage(image, 0, 0);
 
  var a = document.createElement('a');
  a.download = "tree_diagram.png";
  a.href = canvas.toDataURL('image/png');
  document.body.appendChild(a);
  a.click();
}

screenshot 22
screenshot 23

Thoughts?

@exupero
Copy link
Owner

exupero commented Jul 23, 2018

Using this library, exporting a PNG with styles should be as simple as

saveSvgAsPng(document.querySelector('svg')[0], 'tree_diagram.png');

If you're basing your code off my 2014 blog post, know that the proof-of-concept outlined in that post has grown somewhat since then, especially with regard to including CSS styles. This library will work much better, with a lot less manual effort.

@marinagavr
Copy link
Author

marinagavr commented Jul 24, 2018 via email

@gs-asachdeva
Copy link

gs-asachdeva commented Nov 21, 2018

Hi @exupero ,
I am using this library and I am facing issue in getting styles for my image. I get 'DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules' issue for my css files. I am locally bundling it using webpack and in production it is served from cdn. Is there a way I can specifically send my css file as a param to the library so that I can avoid these issues, or do you have another solution.

Thanks,
Anchal

@exupero
Copy link
Owner

exupero commented Nov 25, 2018

Hi @gs-asachdeva. This sounds like a separate issue than the one above. Would you make a new issue where we can discuss the error you're getting?

@exupero exupero closed this as completed Nov 25, 2018
@lifeinchords
Copy link

@gs-asachdeva I had a similar error using this library and another.
I wrote my fix in issue #216 using crossorigin="anonymous" on linked stylesheets

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants