1 | # saveSvgAsPng
|
2 |
|
3 | ## Installation
|
4 |
|
5 | ```
|
6 | npm install save-svg-as-png
|
7 | ```
|
8 |
|
9 | ## Prerequisites
|
10 |
|
11 | SaveSvgAsPng relies on JavaScript promises, so any browsers that don't natively support the standard `Promise` object will need to have a polyfill.
|
12 |
|
13 | ## Usage
|
14 |
|
15 | To save a PNG, include the script `saveSvgAsPng.js` in your page, then call the `saveSvgAsPng` function with an SVG node and a filename:
|
16 |
|
17 | ```javascript
|
18 | saveSvgAsPng(document.getElementById("diagram"), "diagram.png");
|
19 | ```
|
20 |
|
21 | 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.
|
22 |
|
23 | If you want to scale the image up or down, you can pass a scale factor in an options object:
|
24 |
|
25 | ```javascript
|
26 | saveSvgAsPng(document.getElementById("diagram"), "diagram.png", {scale: 0.5});
|
27 | ```
|
28 |
|
29 | Other options are documented below.
|
30 |
|
31 | If you just want a dataURI for an SVG, you can call `svgAsDataUri` with an SVG node, options, and a callback:
|
32 |
|
33 | ```javascript
|
34 | svgAsDataUri(document.getElementById("diagram"), {}, function(uri) {
|
35 | ...
|
36 | });
|
37 | ```
|
38 |
|
39 | If you want a dataURI of a PNG generated from an SVG, you can call `svgAsPngUri` with an SVG node, options, and a callback:
|
40 |
|
41 | ```javascript
|
42 | svgAsPngUri(document.getElementById("diagram"), {}, function(uri) {
|
43 | ...
|
44 | });
|
45 | ```
|
46 |
|
47 | Compatible with [browserify](http://browserify.org/) and [requirejs](http://requirejs.org).
|
48 |
|
49 | 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).
|
50 |
|
51 | ### Options
|
52 |
|
53 | - `backgroundColor` — Creates a PNG with the given background color. Defaults to transparent.
|
54 | - `canvg` - If canvg is passed in, it will be used to write svg to canvas. This will allow support for Internet Explorer
|
55 | - `encoderOptions` - A Number between 0 and 1 indicating image quality. The default is 0.8
|
56 | - `encoderType` - A DOMString indicating the image format. The default type is image/png.
|
57 | - `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.
|
58 | - `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.
|
59 | - `left` - Specify the viewbox's left position. Defaults to 0.
|
60 | - `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.
|
61 | - `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.
|
62 | - `scale` — Changes the resolution of the output PNG. Defaults to `1`, the same dimensions as the source SVG.
|
63 | - `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.
|
64 | - `top` - Specify the viewbox's top position. Defaults to 0.
|
65 | - `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.
|
66 |
|
67 | ### Testing
|
68 |
|
69 | run tests with [tape](https://www.npmjs.com/package/tape)
|
70 | ```bash
|
71 | npm test
|
72 | ```
|
73 |
|
74 | ## Support
|
75 |
|
76 | [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.
|
77 |
|
78 | 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 ensure you test the output after switching.
|