pdfmake 0.2.21 → 0.2.23

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.23",
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/docMeasure.js CHANGED
@@ -168,8 +168,10 @@ DocMeasure.prototype.measureImageWithDimensions = function (node, dimensions) {
168
168
  node._width = node._minWidth = node._maxWidth = node.cover.width;
169
169
  node._height = node._minHeight = node._maxHeight = node.cover.height;
170
170
  } else {
171
- node._width = node._minWidth = node._maxWidth = node.width || dimensions.width;
172
- node._height = node.height || (dimensions.height * node._width / dimensions.width);
171
+ var ratio = dimensions.width / dimensions.height;
172
+
173
+ node._width = node._minWidth = node._maxWidth = node.width || (node.height ? (node.height * ratio) : dimensions.width);
174
+ node._height = node.height || (node.width ? node.width / ratio : dimensions.height);
173
175
 
174
176
  if (isNumber(node.maxWidth) && node.maxWidth < node._width) {
175
177
  node._width = node._minWidth = node._maxWidth = node.maxWidth;
@@ -215,6 +217,15 @@ DocMeasure.prototype.measureSVG = function (node) {
215
217
 
216
218
  node.font = this.styleStack.getProperty('font');
217
219
 
220
+ // SVG requires a defined width and height
221
+ if (!isNumber(node._width) && !isNumber(node._height)) {
222
+ throw new Error('SVG is missing defined width and height.');
223
+ } else if (!isNumber(node._width)) {
224
+ throw new Error('SVG is missing defined width.');
225
+ } else if (!isNumber(node._height)) {
226
+ throw new Error('SVG is missing defined height.');
227
+ }
228
+
218
229
  // scale SVG based on final dimension
219
230
  node.svg = this.svgMeasure.writeDimensions(node.svg, {
220
231
  width: node._width,
@@ -302,7 +313,7 @@ DocMeasure.prototype.gapSizeForList = function () {
302
313
  return this.textTools.sizeOfString('9. ', this.styleStack);
303
314
  };
304
315
 
305
- DocMeasure.prototype.buildUnorderedMarker = function (styleStack, gapSize, type) {
316
+ DocMeasure.prototype.buildUnorderedMarker = function (item, styleStack, gapSize, type) {
306
317
  function buildDisc(gapSize, color) {
307
318
  // TODO: ascender-based calculations
308
319
  var radius = gapSize.fontSize / 6;
@@ -349,7 +360,7 @@ DocMeasure.prototype.buildUnorderedMarker = function (styleStack, gapSize, type)
349
360
  }
350
361
 
351
362
  var marker;
352
- var color = styleStack.getProperty('markerColor') || styleStack.getProperty('color') || 'black';
363
+ var color = TextTools.getStyleProperty(item, styleStack, 'markerColor', undefined) || styleStack.getProperty('color') || 'black';
353
364
 
354
365
  switch (type) {
355
366
  case 'circle':
@@ -376,7 +387,7 @@ DocMeasure.prototype.buildUnorderedMarker = function (styleStack, gapSize, type)
376
387
  return marker;
377
388
  };
378
389
 
379
- DocMeasure.prototype.buildOrderedMarker = function (counter, styleStack, type, separator) {
390
+ DocMeasure.prototype.buildOrderedMarker = function (item, counter, styleStack, type, separator) {
380
391
  function prepareAlpha(counter) {
381
392
  function toAlpha(num) {
382
393
  return (num >= 26 ? toAlpha((num / 26 >> 0) - 1) : '') + 'abcdefghijklmnopqrstuvwxyz'[num % 26 >> 0];
@@ -455,11 +466,11 @@ DocMeasure.prototype.buildOrderedMarker = function (counter, styleStack, type, s
455
466
  }
456
467
  }
457
468
 
458
- var textArray = { text: counterText };
459
- var markerColor = styleStack.getProperty('markerColor');
460
- if (markerColor) {
461
- textArray.color = markerColor;
462
- }
469
+ var markerColor = TextTools.getStyleProperty(item, styleStack, 'markerColor', undefined) || styleStack.getProperty('color') || 'black';
470
+ var textArray = {
471
+ text: counterText,
472
+ color: markerColor
473
+ };
463
474
 
464
475
  return { _inlines: this.textTools.buildInlines(textArray, styleStack).items };
465
476
  };
@@ -476,7 +487,7 @@ DocMeasure.prototype.measureUnorderedList = function (node) {
476
487
  var item = items[i] = this.measureNode(items[i]);
477
488
 
478
489
  if (!item.ol && !item.ul) {
479
- item.listMarker = this.buildUnorderedMarker(style, node._gapSize, item.listType || node.type);
490
+ item.listMarker = this.buildUnorderedMarker(item, style, node._gapSize, item.listType || node.type);
480
491
  }
481
492
 
482
493
  node._minWidth = Math.max(node._minWidth, items[i]._minWidth + node._gapSize.width);
@@ -505,7 +516,7 @@ DocMeasure.prototype.measureOrderedList = function (node) {
505
516
 
506
517
  if (!item.ol && !item.ul) {
507
518
  var counterValue = isNumber(item.counter) ? item.counter : counter;
508
- item.listMarker = this.buildOrderedMarker(counterValue, style, item.listType || node.type, node.separator);
519
+ item.listMarker = this.buildOrderedMarker(item, counterValue, style, item.listType || node.type, node.separator);
509
520
  if (item.listMarker._inlines) {
510
521
  node._gapSize.width = Math.max(node._gapSize.width, item.listMarker._inlines[0].width);
511
522
  }
package/src/helpers.js CHANGED
@@ -5,7 +5,7 @@ function isString(variable) {
5
5
  }
6
6
 
7
7
  function isNumber(variable) {
8
- return typeof variable === 'number' || variable instanceof Number;
8
+ return ((typeof variable === 'number') || (variable instanceof Number)) && !Number.isNaN(variable);
9
9
  }
10
10
 
11
11
  function isBoolean(variable) {
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;
package/src/textTools.js CHANGED
@@ -395,4 +395,6 @@ function widthOfString(text, font, fontSize, characterSpacing, fontFeatures) {
395
395
  return font.widthOfString(text, fontSize, fontFeatures) + ((characterSpacing || 0) * (text.length - 1));
396
396
  }
397
397
 
398
+ TextTools.getStyleProperty = getStyleProperty;
399
+
398
400
  module.exports = TextTools;