1.4.17 • Published 5 years ago

save-svg-as-png v1.4.17

Weekly downloads
22,805
License
MIT
Repository
github
Last release
5 years ago

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:

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:

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:

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:

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

Compatible with browserify and requirejs.

If you want to use TypeScript, necessary type definitions are available in Typings public 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

npm test

Support

Chrome limits data URIs to 2MB, so you may have trouble generating PNGs beyod a certain size.

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

pheatmap.js@symblight/react-stock-charts@riffcreative/lsw-eui-generatorspider-chartsreact-svg-diagram@infinitebrahmanuniverse/nolb-sav@everything-registry/sub-chunk-2711whalestats-stockchartsspecdrawvt-chartvision-react-stock-chartvisualdlv-regexpvue-declarative-plots@dep-spy/viewjschartjun_0214_react-stockchartsjupyter-sankey-widgetphylotree-visualization-demoreact-stockcharts-micodestock-react-charttjcds-front-commonu-report-uitr-react-stockcharts@aqaurius6666/phylotree-visualization@anonymity94/d3-force-graph@asymme1/phylotree-visualization-demo@alexsers/react-stockcharts@alokagr07/react-stock-charts@coinspot-official/react-stockcharts@dickyindra/react-stockcharts@datagrok/sequence-translatorvue-nice-avatar@eaglus/react-stockcharts@hn3000/react-stockcharts@giap/phylotree@mwater/visualization@mr.online7446/react-stockcharts@kees_s/photo-share-component@jaketb/react-stockcharts@khaitd0340/phylotree-visualization@jsdirgodel/react-stockcharts@ima-worldhealth/dhis2-crawler@quality_miao/knowledgegraph@politicalwatch/tipi-uikit@pka420/react-stockcharts@oskarer/react-stockcharts@pareext/cli-servicems-vue-eletms-vue-npmmetro-web-frontreact-mutation-plotrt-chart-stockreact-cryptochartsreact-cryptocharts-v2react-finchartreact-markettrendsmediacenterwidgetsave-html-as-image-enhancedsave-html-as-imageremestreact-img-markdownreact-img-markupng-custom-avatar-generatorphoto-share-componentqe-react-qrcodeprintscratchprintscratch.2.0psychodz1react-stockchartsreact-stockcharts-efreact-stockcharts-ferdibiflatorreact-stockcharts-fxgreact-stockcharts-lxreact-stockcharts-relativereact-stock-graphreact-stockcharts-akahanreact-stockcharts-rollupgormm-chartsigno-reportsigno-linecharthyphy-visiongrnsighthc-heatmapinsight-stage-custom1make-a-vis@wlchn/react-stockcharts@youngox9/common-module@zevit/core-visualizationsalmost-jointalmost-joint-ifml-layoutalignment.jsancestryalexmattson-react-stockchartschart-widgetchartbuildercbioportal-frontend-commonscefc-reactstockchartscefc-stockchartscli-service-depscleo-one-stockcharts
1.4.17

5 years ago

1.4.16

5 years ago

1.4.15

5 years ago

1.4.14

6 years ago

1.4.13

6 years ago

1.4.12

6 years ago

1.4.11

6 years ago

1.4.10

6 years ago

1.4.9

6 years ago

1.4.8

6 years ago

1.4.7

6 years ago

1.4.6

7 years ago

1.4.5

7 years ago

1.4.4

7 years ago

1.4.3

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.3

9 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago