Convert SVG code into PNG data URI in node.js

前端 未结 2 1412
一向
一向 2021-02-07 14:48

I’d like to convert a simple dynamic svg snippet into a PNG data URI on the server. Something like this:

var svg = \'<         


        
2条回答
  •  隐瞒了意图╮
    2021-02-07 15:32

    Take a look at svg-to-img.

    It relies on headless Chrome under the hood to do the conversion.

    To use svg-to-img in your project, run:

    npm install svg-to-img
    

    Here's how to convert a svg to a base64-encoded png:

    const svgToImg = require("svg-to-img");
    
    (async () => {
      const svg = "";
      const png = await svgToImg.from(svg).toPng({ encoding: "base64" });
    
      console.log("data:image/png;base64," + png);
    })();
    

    Disclaimer: I'm the author of svg-to-img.

提交回复
热议问题