Skip to content

Commit da36925

Browse files
committed
added support SVGElement object for svg node #1936
1 parent fb8a38f commit da36925

3 files changed

Lines changed: 52 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Added auto page height for multiple pages (for `section` or after custom page break)
66
- Added type validation for parameters in method `createPdf`
7+
- Added support `SVGElement` object for `svg` node (`SVGElement` object is available only in browser)
78
- Fixed a bug in the write method where it did not wait for the file write operation to complete
89
- Fixed SVG loading
910
- Fixed rendering SVG without viewBox

src/Renderer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import TextDecorator from './TextDecorator';
22
import TextInlines from './TextInlines';
3-
import { isNumber } from './helpers/variableType';
3+
import { isNumber, isString } from './helpers/variableType';
44
import SVGtoPDF from './3rd-party/svg-to-pdfkit';
55

66
const findFont = (fonts, requiredFonts, defaultFont) => {
@@ -329,7 +329,13 @@ class Renderer {
329329
}
330330

331331
renderSVG(svg) {
332-
let options = Object.assign({ width: svg._width, height: svg._height, assumePt: true }, svg.options);
332+
let options = {
333+
width: svg._width,
334+
height: svg._height,
335+
assumePt: true,
336+
...svg.options,
337+
useCSS: !isString(svg.svg)
338+
};
333339
options.fontCallback = (family, bold, italic) => {
334340
let fontsFamily = family.split(',').map(f => f.trim().replace(/('|")/g, ''));
335341
let font = findFont(this.pdfDocument.fonts, fontsFamily, svg.font || 'Roboto');

src/SVGMeasure.js

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { XmlDocument } from "xmldoc";
2+
import { isString } from './helpers/variableType';
23

34
/**
45
* Strip unit postfix, parse number, but return undefined instead of NaN for bad input
@@ -26,11 +27,11 @@ const parseSVG = (svgString) => {
2627
try {
2728
doc = new XmlDocument(svgString);
2829
} catch (err) {
29-
throw new Error('SVGMeasure: ' + err);
30+
throw new Error('Invalid svg document (' + err + ')');
3031
}
3132

3233
if (doc.name !== "svg") {
33-
throw new Error('SVGMeasure: expected <svg> document');
34+
throw new Error('Invalid svg document (expected <svg>)');
3435
}
3536

3637
return doc;
@@ -41,16 +42,30 @@ class SVGMeasure {
4142

4243
}
4344

44-
measureSVG(svgString) {
45-
let doc = parseSVG(svgString);
45+
measureSVG(svg) {
46+
let width, height, viewBox;
4647

47-
let docWidth = stripUnits(doc.attr.width);
48-
let docHeight = stripUnits(doc.attr.height);
48+
if (isString(svg)) {
49+
let doc = parseSVG(svg);
4950

50-
if ((docWidth === undefined || docHeight === undefined) && typeof doc.attr.viewBox === 'string') {
51-
let viewBoxParts = doc.attr.viewBox.split(/[,\s]+/);
51+
width = doc.attr.width;
52+
height = doc.attr.height;
53+
viewBox = doc.attr.viewBox;
54+
} else if (typeof SVGElement !== 'undefined' && svg instanceof SVGElement && typeof getComputedStyle === 'function') {
55+
width = svg.getAttribute("width");
56+
height = svg.getAttribute("height");
57+
viewBox = svg.getAttribute("viewBox");
58+
} else {
59+
throw new Error('Invalid SVG document');
60+
}
61+
62+
let docWidth = stripUnits(width);
63+
let docHeight = stripUnits(height);
64+
65+
if ((docWidth === undefined || docHeight === undefined) && typeof viewBox === 'string') {
66+
let viewBoxParts = viewBox.split(/[,\s]+/);
5267
if (viewBoxParts.length !== 4) {
53-
throw new Error("Unexpected svg viewbox format, should have 4 entries but found: '" + doc.attr.viewBox + "'");
68+
throw new Error("Unexpected svg viewBox format, should have 4 entries but found: '" + viewBox + "'");
5469
}
5570
if (docWidth === undefined) {
5671
docWidth = stripUnits(viewBoxParts[2]);
@@ -66,17 +81,28 @@ class SVGMeasure {
6681
};
6782
}
6883

69-
writeDimensions(svgString, dimensions) {
70-
let doc = parseSVG(svgString);
71-
72-
if (typeof doc.attr.viewBox !== 'string') {
73-
doc.attr.viewBox = `0 0 ${stripUnits(doc.attr.width)} ${stripUnits(doc.attr.height)}`;
84+
writeDimensions(svg, dimensions) {
85+
if (isString(svg)) {
86+
let doc = parseSVG(svg);
87+
88+
if (typeof doc.attr.viewBox !== 'string') {
89+
doc.attr.viewBox = `0 0 ${stripUnits(doc.attr.width)} ${stripUnits(doc.attr.height)}`;
90+
}
91+
92+
doc.attr.width = "" + dimensions.width;
93+
doc.attr.height = "" + dimensions.height;
94+
95+
return doc.toString();
96+
}
97+
98+
if (!svg.hasAttribute('viewBox')) {
99+
svg.setAttribute('viewBox', `0 0 ${stripUnits(svg.getAttribute('width'))} ${stripUnits(svg.getAttribute('height'))}`);
74100
}
75101

76-
doc.attr.width = "" + dimensions.width;
77-
doc.attr.height = "" + dimensions.height;
102+
svg.setAttribute('width', "" + dimensions.width);
103+
svg.setAttribute('height', "" + dimensions.height);
78104

79-
return doc.toString();
105+
return svg;
80106
}
81107
}
82108

0 commit comments

Comments
 (0)