pdfmake 0.2.21 → 0.2.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdfmake",
3
- "version": "0.2.21",
3
+ "version": "0.2.22",
4
4
  "description": "Client/server side PDF printing in pure JavaScript",
5
5
  "main": "src/printer.js",
6
6
  "browser": "build/pdfmake.js",
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var isFunction = require('../helpers').isFunction;
4
+ var isObject = require('../helpers').isObject;
4
5
  var isUndefined = require('../helpers').isUndefined;
5
6
  //var isNull = require('../helpers').isNull;
6
7
  var pack = require('../helpers').pack;
@@ -321,6 +322,10 @@ Document.prototype.getStream = function (options, cb) {
321
322
 
322
323
  module.exports = {
323
324
  createPdf: function (docDefinition, tableLayouts, fonts, vfs) {
325
+ if (!isObject(docDefinition)) {
326
+ throw new Error("Parameter 'docDefinition' has an invalid type. Object expected.");
327
+ }
328
+
324
329
  if (!canCreatePdf()) {
325
330
  throw 'Your browser does not provide the level of support needed';
326
331
  }
package/src/printer.js CHANGED
@@ -15,6 +15,7 @@ var isNumber = require('./helpers').isNumber;
15
15
  var isBoolean = require('./helpers').isBoolean;
16
16
  var isArray = require('./helpers').isArray;
17
17
  var isUndefined = require('./helpers').isUndefined;
18
+ var isObject = require('./helpers').isObject;
18
19
  var isPattern = require('./helpers').isPattern;
19
20
  var getPattern = require('./helpers').getPattern;
20
21
  var SVGtoPDF = require('./3rd-party/svg-to-pdfkit');
@@ -111,6 +112,14 @@ function PdfPrinter(fontDescriptors) {
111
112
  PdfPrinter.prototype.createPdfKitDocument = function (docDefinition, options) {
112
113
  options = options || {};
113
114
 
115
+ if (!isObject(docDefinition)) {
116
+ throw new Error("Parameter 'docDefinition' has an invalid type. Object expected.");
117
+ }
118
+
119
+ if (!isObject(options)) {
120
+ throw new Error("Parameter 'options' has an invalid type. Object expected.");
121
+ }
122
+
114
123
  docDefinition.version = docDefinition.version || '1.3';
115
124
  docDefinition.subset = docDefinition.subset || undefined;
116
125
  docDefinition.tagged = typeof docDefinition.tagged === 'boolean' ? docDefinition.tagged : false;
@@ -677,7 +686,7 @@ function renderImage(image, x, y, pdfKitDoc) {
677
686
  }
678
687
 
679
688
  function renderSVG(svg, x, y, pdfKitDoc, fontProvider) {
680
- var options = Object.assign({ width: svg._width, height: svg._height, assumePt: true }, svg.options);
689
+ var options = Object.assign({ width: svg._width, height: svg._height, assumePt: true, useCSS: !isString(svg.svg) }, svg.options);
681
690
  options.fontCallback = function (family, bold, italic) {
682
691
  var fontsFamily = family.split(',').map(function (f) { return f.trim().replace(/('|")/g, ''); });
683
692
  var font = findFont(fontProvider.fonts, fontsFamily, svg.font || 'Roboto');
package/src/svgMeasure.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var xmldoc = require('xmldoc');
4
+ var isString = require('./helpers').isString;
4
5
 
5
6
  /** Strip unit postfix, parse number, but return undefined instead of NaN for bad input */
6
7
  function stripUnits(textVal) {
@@ -18,11 +19,11 @@ function parseSVG(svgString) {
18
19
  try {
19
20
  doc = new xmldoc.XmlDocument(svgString);
20
21
  } catch (err) {
21
- throw new Error('SVGMeasure: ' + err);
22
+ throw new Error('Invalid svg document (' + err + ')');
22
23
  }
23
24
 
24
25
  if (doc.name !== "svg") {
25
- throw new Error('SVGMeasure: expected <svg> document');
26
+ throw new Error('Invalid svg document (expected <svg>)');
26
27
  }
27
28
 
28
29
  return doc;
@@ -31,17 +32,30 @@ function parseSVG(svgString) {
31
32
  function SVGMeasure() {
32
33
  }
33
34
 
34
- SVGMeasure.prototype.measureSVG = function (svgString) {
35
-
36
- var doc = parseSVG(svgString);
35
+ SVGMeasure.prototype.measureSVG = function (svg) {
36
+ var width, height, viewBox;
37
+
38
+ if (isString(svg)) {
39
+ var doc = parseSVG(svg);
40
+
41
+ width = doc.attr.width;
42
+ height = doc.attr.height;
43
+ viewBox = doc.attr.viewBox;
44
+ } else if (typeof SVGElement !== 'undefined' && svg instanceof SVGElement && typeof getComputedStyle === 'function') {
45
+ width = svg.getAttribute("width");
46
+ height = svg.getAttribute("height");
47
+ viewBox = svg.getAttribute("viewBox");
48
+ } else {
49
+ throw new Error('Invalid SVG document');
50
+ }
37
51
 
38
- var docWidth = stripUnits(doc.attr.width);
39
- var docHeight = stripUnits(doc.attr.height);
52
+ var docWidth = stripUnits(width);
53
+ var docHeight = stripUnits(height);
40
54
 
41
- if ((docWidth == undefined || docHeight == undefined) && typeof doc.attr.viewBox == 'string') {
42
- var viewBoxParts = doc.attr.viewBox.split(/[,\s]+/);
55
+ if ((docWidth == undefined || docHeight == undefined) && typeof viewBox == 'string') {
56
+ var viewBoxParts = viewBox.split(/[,\s]+/);
43
57
  if (viewBoxParts.length !== 4) {
44
- throw new Error("Unexpected svg viewbox format, should have 4 entries but found: '" + doc.attr.viewBox + "'");
58
+ throw new Error("Unexpected svg viewBox format, should have 4 entries but found: '" + viewBox + "'");
45
59
  }
46
60
  if (docWidth == undefined) {
47
61
  docWidth = stripUnits(viewBoxParts[2]);
@@ -57,14 +71,28 @@ SVGMeasure.prototype.measureSVG = function (svgString) {
57
71
  };
58
72
  };
59
73
 
60
- SVGMeasure.prototype.writeDimensions = function (svgString, dimensions) {
74
+ SVGMeasure.prototype.writeDimensions = function (svg, dimensions) {
75
+ if (isString(svg)) {
76
+ var doc = parseSVG(svg);
77
+
78
+ if (typeof doc.attr.viewBox !== 'string') {
79
+ doc.attr.viewBox = `0 0 ${stripUnits(doc.attr.width)} ${stripUnits(doc.attr.height)}`;
80
+ }
81
+
82
+ doc.attr.width = "" + dimensions.width;
83
+ doc.attr.height = "" + dimensions.height;
84
+
85
+ return doc.toString();
86
+ }
61
87
 
62
- var doc = parseSVG(svgString);
88
+ if (!svg.hasAttribute('viewBox')) {
89
+ svg.setAttribute('viewBox', `0 0 ${stripUnits(svg.getAttribute('width'))} ${stripUnits(svg.getAttribute('height'))}`);
90
+ }
63
91
 
64
- doc.attr.width = "" + dimensions.width;
65
- doc.attr.height = "" + dimensions.height;
92
+ svg.setAttribute('width', "" + dimensions.width);
93
+ svg.setAttribute('height', "" + dimensions.height);
66
94
 
67
- return doc.toString();
95
+ return svg;
68
96
  };
69
97
 
70
98
  module.exports = SVGMeasure;