简体   繁体   中英

save-svg-as-png not loading the svg css, while downloading the svg as png

I am using save-svg-as-png module to download generated svg from d3 hierarchy tree to png.

Below is the svg generated in browser

在此处输入图像描述

But when I download it, some css are missing due to which graph is unreadable

在此处输入图像描述

Below is the code i am using to download

var saveSvgToPng = document.getElementById("download-button");
saveSvgToPng.onclick = function () {
  saveSvgAsPng(d3.select('svg').node(), "dia.png", {
    scale: 2,
  });
};

How do I resolve this issue.

Thanks in advance!

To all who are facing this issue. As suggested by @RobertLongson in the comment, it worked when I switched to inline css instead of external css.

Code with external css which was causing the issue

nodes
    .append("text")
    .attr("font-size", "12px")
    .attr("text-anchor", "middle")
    .text(function (d) {
        return `${d.data.name}`
    })

External css

text {
  text-shadow: -1px -1px 3px white, -1px 1px 3px white, 1px -1px 3px white,
      1px 1px 3px white;
  cursor: pointer;
  font-family: "Playfair Display", serif;
}

Code with inline css, which fixed the issue

nodes
    .append("text")
    .attr("font-size", "12px")
    .attr("text-anchor", "middle")
    .style(
      "text-shadow",
      `-1px -1px 3px white, -1px 1px 3px white, 1px -1px 3px white,
      1px 1px 3px white`
    )
    .style("cursor", "pointer")
    .style("font-family", `"Playfair Display", serif`)
    .text(function (d) {
        return `${d.data.name}`
    })

Hope this helps someone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM