---
title: "convertSvgToPng"
description: "Convert an SVG string into a PNG Blob using a canvas, returning a promise so you can download or upload rasterized image output."
canonical_url: "https://core.mhaibaraai.cn/en/docs/helpers/file/convert-svg-to-png"
---
# convertSvgToPng

> Convert an SVG string into a PNG Blob using a canvas, returning a promise so you can download or upload rasterized image output.

## Usage

`convertSvgToPng` converts an SVG string into a PNG Blob.

> [!WARNING]
> 
> Note
> 
> - Only available in browser environments
> - Uses the Canvas API for conversion
> - Throws an error if the conversion fails

```ts
import { convertSvgToPng } from '@movk/core'

const svgString = '<svg width="100" height="100"><circle cx="50" cy="50" r="40" fill="red"/></svg>'

try {
  const pngBlob = await convertSvgToPng(svgString)
  const url = URL.createObjectURL(pngBlob)

  // Use for download or display
  const img = document.createElement('img')
  img.src = url
  document.body.appendChild(img)
}
catch (error) {
  console.error('SVG conversion failed:', error)
}
```

## API

### `convertSvgToPng(svg)`

Convert an SVG string into a PNG Blob.

### Parameters

**svg** (`string`) *required*: The SVG string to convert.

### Returns

**returns** (`Promise<Blob>`): A Blob containing the PNG image data.

## Changelog

See commit history for [src/helpers/file/convertSvgToPng.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/helpers/file/convertSvgToPng.ts).

---

- [GitHub](https://github.com/mhaibaraai/movk-core/blob/main/src/helpers/file/convertSvgToPng.ts)


## Sitemap

See the full [sitemap](https://core.mhaibaraai.cn/sitemap.md) for all pages.
