@bpmn-io/svg-to-image 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026-present Camunda Services GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @bpmn-io/svg-to-image
2
+
3
+ [![CI](https://github.com/bpmn-io/svg-to-image/actions/workflows/CI.yml/badge.svg)](https://github.com/bpmn-io/svg-to-image/actions/workflows/CI.yml)
4
+
5
+ Converts an SVG to an image with decent quality.
6
+
7
+ ## Usage
8
+
9
+ ```javascript
10
+ import { svgToImage } from 'svg-to-image';
11
+
12
+ const svg = '<svg>...</svg>';
13
+
14
+ // Generate PNG
15
+ const result = await svgToImage(svg, {
16
+ imageType: 'png',
17
+ outputFormat: 'blob'
18
+ });
19
+ ```
20
+
21
+ ## Build and Run
22
+
23
+ ```
24
+ # install dependencies
25
+ npm install
26
+
27
+ # run project, executing all tasks
28
+ npm run all
29
+ ```
30
+
31
+ ## How it works
32
+
33
+ This package uses [canvg](https://github.com/canvg/canvg) to render SVG elements onto a canvas and then exports them as image data URLs or [image blobs](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
34
+
35
+ ## License
36
+
37
+ MIT
package/lib/index.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Turn an SVG into an image, returning the image data URL.
3
+ *
4
+ * @param svg
5
+ * @param options
6
+ *
7
+ * @returns
8
+ */
9
+ export function svgToImage(svg: string, options?: {
10
+ imageType?: "png" | "jpeg" = "png";
11
+ outputFormat: "dataUrl";
12
+ }): Promise<string>;
13
+
14
+ /**
15
+ * Turn an SVG into an image, returning the image {@link Blob}.
16
+ *
17
+ * @param svg
18
+ * @param options
19
+ *
20
+ * @returns
21
+ */
22
+ export function svgToImage(svg: string, options?: {
23
+ imageType: "png" | "jpeg" = "png";
24
+ outputFormat: "blob";
25
+ }): Promise<Blob>;
package/lib/index.js ADDED
@@ -0,0 +1,122 @@
1
+ import { Canvg } from 'canvg';
2
+
3
+ // list of defined encodings
4
+ const ENCODINGS = [
5
+ 'image/png',
6
+ 'image/jpeg'
7
+ ];
8
+
9
+ const OUTPUT_FORMATS = [
10
+ 'dataUrl',
11
+ 'blob'
12
+ ];
13
+
14
+ const INITIAL_SCALE = 3;
15
+ const FINAL_SCALE = 1;
16
+ const SCALE_STEP = 1;
17
+
18
+ const DATA_URL_REGEX = /^data:((?:\w+\/(?:(?!;).)+)?)((?:;[\w\W]*?[^;])*),(.+)$/;
19
+
20
+ /**
21
+ * Turn an SVG into an image, returning the image data URL.
22
+ *
23
+ * @overload
24
+ * @param {string} svg
25
+ * @param {{ imageType?: 'png'|'jpeg', outputFormat: 'dataUrl' }} [options]
26
+ *
27
+ * @returns {Promise<string>}
28
+ */
29
+ /**
30
+ * Turn an SVG into an image, returning the image {@link Blob}.
31
+ *
32
+ * @overload
33
+ * @param {string} svg
34
+ * @param {{ imageType?: 'png'|'jpeg', outputFormat: 'blob' }} [options]
35
+ *
36
+ * @returns {Promise<Blob>}
37
+ */
38
+ /**
39
+ * Turn an SVG into an image.
40
+ *
41
+ * @param {string} svg
42
+ * @param {{ imageType?: 'png'|'jpeg', outputFormat?: 'dataUrl'|'blob' }} [options]
43
+ *
44
+ * @returns {Promise<string|Blob>}
45
+ */
46
+ export async function svgToImage(svg, options = {}) {
47
+ const { imageType = 'png', outputFormat = 'dataUrl' } = options;
48
+ const encoding = 'image/' + imageType;
49
+
50
+ if (OUTPUT_FORMATS.indexOf(outputFormat) === -1) {
51
+ throw new Error('<' + outputFormat + '> is not supported output format for converting svg to image');
52
+ }
53
+
54
+ if (ENCODINGS.indexOf(encoding) === -1) {
55
+ throw new Error('<' + imageType + '> is not supported type for converting svg to image');
56
+ }
57
+
58
+ const initialSVG = svg;
59
+
60
+ for (let scale = INITIAL_SCALE; scale >= FINAL_SCALE; scale -= SCALE_STEP) {
61
+ try {
62
+ let canvas = document.createElement('canvas');
63
+
64
+ svg = scaleSvg(initialSVG, scale);
65
+
66
+ const context = canvas.getContext('2d');
67
+
68
+ const canvg = Canvg.fromString(context, svg);
69
+ await canvg.render();
70
+
71
+ // make the background white for every format
72
+ context.globalCompositeOperation = 'destination-over';
73
+ context.fillStyle = 'white';
74
+
75
+ context.fillRect(0, 0, canvas.width, canvas.height);
76
+
77
+ if (outputFormat === 'dataUrl') {
78
+ const dataUrl = canvas.toDataURL(encoding);
79
+
80
+ if (DATA_URL_REGEX.test(dataUrl)) {
81
+ return dataUrl;
82
+ }
83
+ } else {
84
+ const blob = await new Promise(resolve => {
85
+ canvas.toBlob(result => resolve(result), encoding);
86
+ });
87
+
88
+ if (blob) {
89
+ return blob;
90
+ }
91
+ }
92
+ } catch (error) {
93
+
94
+ // If rendering or export fails for this scale, try again with a smaller scale.
95
+ continue;
96
+ }
97
+ }
98
+
99
+ throw new Error('Could not convert SVG to image. Diagram size is too big?');
100
+ }
101
+
102
+ function scaleSvg(svg, scale) {
103
+ return svg
104
+ .replace(/width="([^"]+)"/, function(match, widthStr) {
105
+ const width = parseFloat(widthStr);
106
+
107
+ if (Number.isNaN(width)) {
108
+ return match;
109
+ }
110
+
111
+ return `width="${width * scale}"`;
112
+ })
113
+ .replace(/height="([^"]+)"/, function(match, heightStr) {
114
+ const height = parseFloat(heightStr);
115
+
116
+ if (Number.isNaN(height)) {
117
+ return match;
118
+ }
119
+
120
+ return `height="${height * scale}"`;
121
+ });
122
+ }
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@bpmn-io/svg-to-image",
3
+ "version": "1.0.0",
4
+ "description": "Utility for generating images from SVG markup",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "files": [
9
+ "lib"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "default": "./lib/index.js",
14
+ "types": "./lib/index.d.ts"
15
+ },
16
+ "./package.json": "./package.json"
17
+ },
18
+ "scripts": {
19
+ "all": "run-s lint test",
20
+ "lint": "eslint .",
21
+ "test": "karma start karma.config.cjs",
22
+ "dev": "npm test -- --auto-watch --no-single-run"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/bpmn-io/svg-to-image.git"
27
+ },
28
+ "keywords": [
29
+ "svg",
30
+ "image",
31
+ "png",
32
+ "jpeg"
33
+ ],
34
+ "author": "bpmn.io contributors",
35
+ "license": "MIT",
36
+ "bugs": {
37
+ "url": "https://github.com/bpmn-io/svg-to-image/issues"
38
+ },
39
+ "homepage": "https://github.com/bpmn-io/svg-to-image#readme",
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "contributors": [
44
+ {
45
+ "name": "bpmn.io contributors",
46
+ "url": "https://github.com/bpmn-io"
47
+ }
48
+ ],
49
+ "devDependencies": {
50
+ "@babel/core": "^7.26.10",
51
+ "babel-loader": "^10.0.0",
52
+ "babel-plugin-istanbul": "^7.0.1",
53
+ "chai": "^6.2.2",
54
+ "eslint": "^9.39.2",
55
+ "eslint-plugin-bpmn-io": "^2.2.0",
56
+ "karma": "^6.4.4",
57
+ "karma-chrome-launcher-2": "^3.3.0",
58
+ "karma-coverage": "^2.2.1",
59
+ "karma-debug-launcher": "0.0.5",
60
+ "karma-env-preprocessor": "^0.1.1",
61
+ "karma-mocha": "^2.0.1",
62
+ "karma-webpack": "^5.0.1",
63
+ "mocha": "^11.7.5",
64
+ "npm-run-all2": "^8.0.4",
65
+ "puppeteer": "^24.37.2",
66
+ "raw-loader": "^4.0.2",
67
+ "webpack": "^5.105.0"
68
+ },
69
+ "dependencies": {
70
+ "canvg": "^4.0.3"
71
+ }
72
+ }