svg-render v2.0.0
svg-render
Render high-quality PNG images from an SVG
CLI
The easiest way to convert your SVG to a PNG is on the command line:
npx svg-render < input.svg > output.pngYou can also resive your SVG, since it is scalable after all:
npx svg-render --width 512 < input.svg > output.pngSee npx svg-render --help for more info.
If you use this often, you might benefit from installing it on your path directly:
npm install --global svg-render
svg-render < input.svg > output.pngThe generated images are unoptimized. You can optimize them with something like pngmin-cli:
npx svg-render < input.svg | npx pngmin-cli > output.pngAPI
You can require this module in your script as well:
const { promises: fs } = require('fs');
const render = require('svg-render');
(async () => {
const outputBuffer = await render({
buffer: await fs.readFile('/path/to/my/input.svg'),
width: 512
});
await fs.writeFile('./output.png', outputBuffer);
})();Options:
buffer<Buffer>: Required. The SVG being rendered.width<Number>: Optional. The desired width of the output PNG. Defaults to the width defined in the SVG (or proportionally scaled based onheightwhen defined).height<Number>: Optional. The desired height of the output PNG. Defaults to the height defined in the SVG (or proportionally scaled based onwidthwhen defined).
Note: both width and height are optional, but they work together. If neither is provided, the PNG will be the original size of the SVG. If only one of the properties is provided, the other will automatically be scaled proportionally to the original SVG size. If both are provided, the PNG will be stretched to that exact size, regardless of proportions.