I’d like to convert a simple dynamic svg snippet into a PNG data URI on the server. Something like this:
var svg = \'
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.