Export SVG to PNG, JPEG, or WEBP in the browser
Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️
npm i svg-browser-export
import { exportSvg } from 'svg-browser-export'
const svgString = '<svg>...</svg>'
const exportedFileUrl = await exportSvg(svgString, 'png')
// => data:image/png;base64,....
Select the DOM element (eg. using querySelector) and access .innerHTML
for the SVG string.
const svgElement = document.querySelector('.svg-selector')
const exportedFileUrl = await exportSvg(svgElement.innerHTML, 'png')
Use it with downloadjs to automatically download the exported asset.
import { exportSvg } from 'svg-browser-export'
import download from 'downloadjs'
...
const exportedFileUrl = await exportSvg(svgString, 'png')
download(exportedFileUrl, 'anyFileName.png')
Returns a promise that resolves to the exported file as a Base64 data URL.
Type: string
The SVG string to convert.
Type: 'png' | 'jpeg' | 'bmp' | 'ico' | 'webp'
The format to export the SVG to.
Type: number
Optional
If you want to scale the SVG. For example, passing in 2
would double the dimensions of the SVG.