How to Convert SVG to PNG: Methods and Best Practices

When and how to rasterize vector graphics without losing sharpness.

🎨 🔄 🖼️ ✨ 📐

TL;DR: SVGs are vectors (math-based, scale infinitely). PNGs are pixels (fixed size, universal compatibility). Convert SVG to PNG when the destination does not support SVG (email, social media, app icons). Use 2x scale for retina. Always include a viewBox in your SVG for predictable output.

SVG and PNG are like two different species of image. SVG is a set of mathematical instructions ("draw a circle here, a line there"), while PNG is a grid of colored pixels. SVGs can scale to any size without getting blurry. PNGs cannot. But PNGs work everywhere, and SVGs... well, SVGs have some compatibility issues.

That is why you will regularly need to convert between the two. Let me show you when, why, and how to do it right.

SVGs are like recipes — they describe how to make the image. PNGs are like photographs of the finished dish. Both useful, very different beasts.

When to Use SVG vs. PNG

⚖️ SVG = scalable perfection. PNG = works literally everywhere.

Stick with SVG When:

Convert to PNG When:

Method 1: Browser Canvas API (How Our Tool Works)

This is the approach that powers our online converter. The browser renders the SVG onto an HTML Canvas element, then exports it as a PNG. Sounds fancy, but the code is pretty straightforward:

function svgToPng(svgElement, width, height, scale = 2) {
  return new Promise((resolve) => {
    const canvas = document.createElement('canvas');
    canvas.width = width * scale;
    canvas.height = height * scale;

    const ctx = canvas.getContext('2d');
    ctx.scale(scale, scale);

    const svgData = new XMLSerializer().serializeToString(svgElement);
    const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
    const url = URL.createObjectURL(svgBlob);

    const img = new Image();
    img.onload = () => {
      ctx.drawImage(img, 0, 0, width, height);
      URL.revokeObjectURL(url);
      canvas.toBlob(resolve, 'image/png');
    };
    img.src = url;
  });
}

That scale parameter is the secret sauce. Setting it to 2 gives you a @2x image that looks razor sharp on retina screens. Bump it to 3 or 4 for even higher resolution output (perfect for print).

Method 2: Command Line with Inkscape

For batch processing (like generating 50 icon sizes from one SVG), the command line is your best friend. Inkscape is free and excellent:

# Single file conversion
inkscape input.svg --export-type=png --export-width=1024 -o output.png

# High-DPI for print
inkscape input.svg --export-type=png --export-dpi=300 -o output.png

# Batch convert all SVGs in a folder
for f in *.svg; do
  inkscape "$f" --export-type=png --export-width=512 -o "${f%.svg}.png"
done

Method 3: Node.js with Sharp

If you need this in a build pipeline or server process, the Sharp library is fast and reliable:

const sharp = require('sharp');

// Basic conversion
await sharp('icon.svg')
  .resize(512, 512)
  .png()
  .toFile('icon.png');

// Generate multiple sizes from one SVG
const sizes = [16, 32, 64, 128, 256, 512, 1024];
for (const size of sizes) {
  await sharp('icon.svg')
    .resize(size, size)
    .png()
    .toFile(`icon-${size}.png`);
}
🏭 One SVG source file → multiple PNG sizes. The SVG is your single source of truth.

Common Pitfalls (and How to Dodge Them)

1. The Missing viewBox Problem

SVGs without a viewBox attribute can render at weird, unpredictable sizes. Always make sure your SVG includes one:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <!-- your shapes here -->
</svg>

2. Missing Fonts

If your SVG uses custom fonts and the conversion tool does not have those fonts installed, the text will fall back to some default system font and look completely wrong. The fix: convert text to paths (outlines) in your SVG editor before converting. Or embed the font as a base64 data URI inside the SVG.

3. External Resources

SVGs that reference external images, CSS files, or fonts via URLs will likely break during conversion. The Canvas API's security model blocks cross-origin resources. Solution: inline everything before converting.

4. CSS Styles Not Included

If an inline SVG on your web page relies on your external CSS stylesheet for its colors, those styles will not come along when you serialize the SVG. You need to compute the styles and inline them as attributes first.

The most frustrating bug: your SVG looks perfect in the browser but comes out blank or weird as a PNG. Nine times out of ten, it is a missing viewBox or external resource issue. Check those first.

Choosing the Right Output Size

Since SVGs scale infinitely, you need to decide what pixel dimensions your PNG should be. Think about where it will be used:

Optimizing Your Output PNG

PNGs from SVG conversion are often unoptimized and larger than they need to be. A few tricks:

# pngquant (lossy, great results)
pngquant --quality=65-80 output.png -o output-optimized.png

# optipng (lossless)
optipng -o5 output.png
💾 Always optimize your PNGs after conversion. Unoptimized PNGs are unnecessarily chunky.

SVG-to-PNG conversion is straightforward once you know the tools and the common gotchas. Use the browser Canvas API for quick one-off conversions, Inkscape or Sharp for batch jobs, and always choose your output dimensions intentionally based on where the PNG will actually be used.

Try It Yourself

Upload or paste your SVG and convert it to a high-resolution PNG instantly in your browser. No server upload required.

Open SVG to PNG Converter →