# save-svg-as-png

> Convert a browser SVG to PNG or dataUri

Latest version **1.4.17** (published 2020-01-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install save-svg-as-png
pnpm add save-svg-as-png
yarn add save-svg-as-png
bun add save-svg-as-png
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.17 |
| Published | 2020-01-27 |
| First published | 2015-04-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 242.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1093 |
| Author | Eric Shull |
| Maintainers | exupero |
| Keywords | svg, png, graphics, images |

## Links

- npm: https://www.npmjs.com/package/save-svg-as-png
- Repository: https://github.com/exupero/saveSvgAsPng
- Issues: https://github.com/exupero/saveSvgAsPng/issues
- npm.io page: https://npm.io/package/save-svg-as-png

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 1.4.17 (latest) — 2020-01-27
- 1.4.16 — 2020-01-23
- 1.4.15 — 2020-01-23
- 1.4.14 — 2019-05-09
- 1.4.13 — 2019-05-05
- 1.4.12 — 2019-03-24
- 1.4.11 — 2019-02-15
- 1.4.10 — 2019-02-15
- 1.4.9 — 2019-02-10
- 1.4.8 — 2019-02-07
- 1.4.7 — 2019-01-18
- 1.4.6 — 2018-08-12
- 1.4.5 — 2018-06-07
- 1.4.4 — 2018-06-03
- 1.4.3 — 2018-05-31
- … 10 more at https://npm.io/package/save-svg-as-png/versions

## README

# saveSvgAsPng

## Installation

```
npm install save-svg-as-png
```

## Prerequisites

SaveSvgAsPng relies on JavaScript promises, so any browsers that don't natively support the standard `Promise` object will need to have a polyfill.

## Usage

To save a PNG, include the script `saveSvgAsPng.js` in your page, then call the `saveSvgAsPng` function with an SVG node and a filename:

```javascript
saveSvgAsPng(document.getElementById("diagram"), "diagram.png");
```

The filename is the preferred filename when saving the image to the file system. The browser may change the name of the file if there is already a file by that name in the target directory.

If you want to scale the image up or down, you can pass a scale factor in an options object:

```javascript
saveSvgAsPng(document.getElementById("diagram"), "diagram.png", {scale: 0.5});
```

Other options are documented below.

If you just want a dataURI for an SVG, you can call `svgAsDataUri`, which returns a promise:

```javascript
svgAsDataUri(document.getElementById("diagram"), options).then(uri => ...);
```

If you want a dataURI of a PNG generated from an SVG, you can call `svgAsPngUri`, which also returns a promise:

```javascript
svgAsPngUri(document.getElementById("diagram"), options).then(uri => ...);
```

Compatible with [browserify](http://browserify.org/) and [requirejs](http://requirejs.org).

If you want to use TypeScript, necessary [type definitions](https://github.com/martianov/typed-save-svg-as-png) are available in [Typings](https://github.com/typings/typings) [public registry](https://github.com/typings/registry).

### Options

- `backgroundColor` — Creates a PNG with the given background color. Defaults to transparent.
- `canvg` - If canvg is passed in, it will be used to write svg to canvas. This will allow support for Internet Explorer
- `encoderOptions` - A Number between 0 and 1 indicating image quality. The default is 0.8
- `encoderType` - A DOMString indicating the image format. The default type is image/png.
- `fonts` - A list of `{text, url, format}` objects the specify what fonts to inline in the SVG. Omitting this option defaults to auto-detecting font rules.
- `height` - Specify the image's height. Defaults to the viewbox's height if given, or the element's non-percentage height, or the element's bounding box's height, or the element's CSS height, or the computed style's height, or 0.
- `left` - Specify the viewbox's left position. Defaults to 0.
- `modifyCss` - A function that takes a CSS rule's selector and properties and returns a string of CSS. Supercedes `selectorRemap` and `modifyStyle`. Useful for modifying properties only for certain CSS selectors.
- `modifyStyle` - A function that takes a CSS rule's properties and returns a string of CSS. Useful for modifying properties before they're inlined into the SVG.
- `scale` — Changes the resolution of the output PNG. Defaults to `1`, the same dimensions as the source SVG.
- `selectorRemap` — A function that takes a CSS selector and produces its replacement in the CSS that's inlined into the SVG. Useful if your SVG style selectors are scoped by ancestor elements in your HTML document.
- `top` - Specify the viewbox's top position. Defaults to 0.
- `width` - Specify the image's width. Defaults to the viewbox's width if given, or the element's non-percentage width, or the element's bounding box's width, or the element's CSS width, or the computed style's width, or 0.
- `excludeUnusedCss` - Exclude CSS rules that don't match any elements in the SVG.
- `excludeCss` - Exclude all CSS rules

### Testing

run tests with [tape](https://www.npmjs.com/package/tape)
```bash
npm test
```

## Support

[Chrome limits data URIs to 2MB](http://stackoverflow.com/questions/695151/data-protocol-url-size-limitations/41755526#41755526), so you may have trouble generating PNGs beyod a certain size.

Internet Explorer will only work if [canvg](https://github.com/canvg/canvg) is passed in, otherwise it will throw a `SecurityError` when calling `toDataURL` on a canvas that's been written to. [canvg](https://github.com/canvg/canvg) may have it's own issues with SVG support, so make sure to test the output.

---
_Source: https://npm.io/package/save-svg-as-png · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
