@flowaccount/pdfmake 1.0.4 → 1.0.5-staging.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.
@@ -1,68 +1,68 @@
1
- 'use strict';
2
-
3
- var isArray = require('./helpers').isArray;
4
-
5
- function typeName(bold, italics) {
6
- var type = 'normal';
7
- if (bold && italics) {
8
- type = 'bolditalics';
9
- } else if (bold) {
10
- type = 'bold';
11
- } else if (italics) {
12
- type = 'italics';
13
- }
14
- return type;
15
- }
16
-
17
- function FontProvider(fontDescriptors, pdfKitDoc) {
18
- this.fonts = {};
19
- this.pdfKitDoc = pdfKitDoc;
20
- this.fontCache = {};
21
-
22
- for (var font in fontDescriptors) {
23
- if (fontDescriptors.hasOwnProperty(font)) {
24
- var fontDef = fontDescriptors[font];
25
-
26
- this.fonts[font] = {
27
- normal: fontDef.normal,
28
- bold: fontDef.bold,
29
- italics: fontDef.italics,
30
- bolditalics: fontDef.bolditalics
31
- };
32
- }
33
- }
34
- }
35
-
36
- FontProvider.prototype.getFontType = function (bold, italics) {
37
- return typeName(bold, italics);
38
- };
39
-
40
- FontProvider.prototype.getFontFile = function (familyName, bold, italics) {
41
- var type = this.getFontType(bold, italics);
42
- if (!this.fonts[familyName] || !this.fonts[familyName][type]) {
43
- return null;
44
- }
45
-
46
- return this.fonts[familyName][type];
47
- };
48
-
49
- FontProvider.prototype.provideFont = function (familyName, bold, italics) {
50
- var type = this.getFontType(bold, italics);
51
- if (this.getFontFile(familyName, bold, italics) === null) {
52
- throw new Error('Font \'' + familyName + '\' in style \'' + type + '\' is not defined in the font section of the document definition.');
53
- }
54
-
55
- this.fontCache[familyName] = this.fontCache[familyName] || {};
56
-
57
- if (!this.fontCache[familyName][type]) {
58
- var def = this.fonts[familyName][type];
59
- if (!isArray(def)) {
60
- def = [def];
61
- }
62
- this.fontCache[familyName][type] = this.pdfKitDoc.font.apply(this.pdfKitDoc, def)._font;
63
- }
64
-
65
- return this.fontCache[familyName][type];
66
- };
67
-
68
- module.exports = FontProvider;
1
+ 'use strict';
2
+
3
+ var isArray = require('./helpers').isArray;
4
+
5
+ function typeName(bold, italics) {
6
+ var type = 'normal';
7
+ if (bold && italics) {
8
+ type = 'bolditalics';
9
+ } else if (bold) {
10
+ type = 'bold';
11
+ } else if (italics) {
12
+ type = 'italics';
13
+ }
14
+ return type;
15
+ }
16
+
17
+ function FontProvider(fontDescriptors, pdfKitDoc) {
18
+ this.fonts = {};
19
+ this.pdfKitDoc = pdfKitDoc;
20
+ this.fontCache = {};
21
+
22
+ for (var font in fontDescriptors) {
23
+ if (fontDescriptors.hasOwnProperty(font)) {
24
+ var fontDef = fontDescriptors[font];
25
+
26
+ this.fonts[font] = {
27
+ normal: fontDef.normal,
28
+ bold: fontDef.bold,
29
+ italics: fontDef.italics,
30
+ bolditalics: fontDef.bolditalics
31
+ };
32
+ }
33
+ }
34
+ }
35
+
36
+ FontProvider.prototype.getFontType = function (bold, italics) {
37
+ return typeName(bold, italics);
38
+ };
39
+
40
+ FontProvider.prototype.getFontFile = function (familyName, bold, italics) {
41
+ var type = this.getFontType(bold, italics);
42
+ if (!this.fonts[familyName] || !this.fonts[familyName][type]) {
43
+ return null;
44
+ }
45
+
46
+ return this.fonts[familyName][type];
47
+ };
48
+
49
+ FontProvider.prototype.provideFont = function (familyName, bold, italics) {
50
+ var type = this.getFontType(bold, italics);
51
+ if (this.getFontFile(familyName, bold, italics) === null) {
52
+ throw new Error('Font \'' + familyName + '\' in style \'' + type + '\' is not defined in the font section of the document definition.');
53
+ }
54
+
55
+ this.fontCache[familyName] = this.fontCache[familyName] || {};
56
+
57
+ if (!this.fontCache[familyName][type]) {
58
+ var def = this.fonts[familyName][type];
59
+ if (!isArray(def)) {
60
+ def = [def];
61
+ }
62
+ this.fontCache[familyName][type] = this.pdfKitDoc.font.apply(this.pdfKitDoc, def)._font;
63
+ }
64
+
65
+ return this.fontCache[familyName][type];
66
+ };
67
+
68
+ module.exports = FontProvider;
package/src/helpers.js CHANGED
@@ -1,138 +1,138 @@
1
- 'use strict';
2
-
3
- function isString(variable) {
4
- return typeof variable === 'string' || variable instanceof String;
5
- }
6
-
7
- function isNumber(variable) {
8
- return typeof variable === 'number' || variable instanceof Number;
9
- }
10
-
11
- function isBoolean(variable) {
12
- return typeof variable === 'boolean';
13
- }
14
-
15
- function isArray(variable) {
16
- return Array.isArray(variable);
17
- }
18
-
19
- function isFunction(variable) {
20
- return typeof variable === 'function';
21
- }
22
-
23
- function isObject(variable) {
24
- return variable !== null && typeof variable === 'object';
25
- }
26
-
27
- function isNull(variable) {
28
- return variable === null;
29
- }
30
-
31
- function isUndefined(variable) {
32
- return variable === undefined;
33
- }
34
-
35
- /**
36
- * @param {any} variable
37
- * @returns {boolean}
38
- */
39
- function isPositiveInteger(variable) {
40
- if (!isNumber(variable) || !Number.isInteger(variable) || variable <= 0) {
41
- return false;
42
- }
43
- return true;
44
- }
45
-
46
- function pack() {
47
- var result = {};
48
-
49
- for (var i = 0, l = arguments.length; i < l; i++) {
50
- var obj = arguments[i];
51
-
52
- if (obj) {
53
- for (var key in obj) {
54
- if (obj.hasOwnProperty(key)) {
55
- result[key] = obj[key];
56
- }
57
- }
58
- }
59
- }
60
-
61
- return result;
62
- }
63
-
64
- function offsetVector(vector, x, y) {
65
- switch (vector.type) {
66
- case 'ellipse':
67
- case 'rect':
68
- vector.x += x;
69
- vector.y += y;
70
- break;
71
- case 'line':
72
- vector.x1 += x;
73
- vector.x2 += x;
74
- vector.y1 += y;
75
- vector.y2 += y;
76
- break;
77
- case 'polyline':
78
- for (var i = 0, l = vector.points.length; i < l; i++) {
79
- vector.points[i].x += x;
80
- vector.points[i].y += y;
81
- }
82
- break;
83
- }
84
- }
85
-
86
- function fontStringify(key, val) {
87
- if (key === 'font') {
88
- return 'font';
89
- }
90
- return val;
91
- }
92
-
93
- function getNodeId(node) {
94
- if (node.id) {
95
- return node.id;
96
- }
97
-
98
- if (isArray(node.text)) {
99
- for (var i = 0, l = node.text.length; i < l; i++) {
100
- var n = node.text[i];
101
- var nodeId = getNodeId(n);
102
- if (nodeId) {
103
- return nodeId;
104
- }
105
- }
106
- }
107
-
108
- return null;
109
- }
110
-
111
- function isPattern(color) {
112
- return isArray(color) && color.length === 2;
113
- }
114
-
115
- // converts from a [<pattern name>, <color>] as used by pdfmake
116
- // into [<pattern object>, <color>] as used by pdfkit
117
- // (the pattern has to be registered in the doc definition of course)
118
- function getPattern(color, patterns) {
119
- return [patterns[color[0]], color[1]];
120
- }
121
-
122
- module.exports = {
123
- isString: isString,
124
- isNumber: isNumber,
125
- isBoolean: isBoolean,
126
- isArray: isArray,
127
- isFunction: isFunction,
128
- isObject: isObject,
129
- isNull: isNull,
130
- isUndefined: isUndefined,
131
- isPositiveInteger: isPositiveInteger,
132
- pack: pack,
133
- fontStringify: fontStringify,
134
- offsetVector: offsetVector,
135
- getNodeId: getNodeId,
136
- isPattern: isPattern,
137
- getPattern: getPattern
138
- };
1
+ 'use strict';
2
+
3
+ function isString(variable) {
4
+ return typeof variable === 'string' || variable instanceof String;
5
+ }
6
+
7
+ function isNumber(variable) {
8
+ return typeof variable === 'number' || variable instanceof Number;
9
+ }
10
+
11
+ function isBoolean(variable) {
12
+ return typeof variable === 'boolean';
13
+ }
14
+
15
+ function isArray(variable) {
16
+ return Array.isArray(variable);
17
+ }
18
+
19
+ function isFunction(variable) {
20
+ return typeof variable === 'function';
21
+ }
22
+
23
+ function isObject(variable) {
24
+ return variable !== null && typeof variable === 'object';
25
+ }
26
+
27
+ function isNull(variable) {
28
+ return variable === null;
29
+ }
30
+
31
+ function isUndefined(variable) {
32
+ return variable === undefined;
33
+ }
34
+
35
+ /**
36
+ * @param {any} variable
37
+ * @returns {boolean}
38
+ */
39
+ function isPositiveInteger(variable) {
40
+ if (!isNumber(variable) || !Number.isInteger(variable) || variable <= 0) {
41
+ return false;
42
+ }
43
+ return true;
44
+ }
45
+
46
+ function pack() {
47
+ var result = {};
48
+
49
+ for (var i = 0, l = arguments.length; i < l; i++) {
50
+ var obj = arguments[i];
51
+
52
+ if (obj) {
53
+ for (var key in obj) {
54
+ if (obj.hasOwnProperty(key)) {
55
+ result[key] = obj[key];
56
+ }
57
+ }
58
+ }
59
+ }
60
+
61
+ return result;
62
+ }
63
+
64
+ function offsetVector(vector, x, y) {
65
+ switch (vector.type) {
66
+ case 'ellipse':
67
+ case 'rect':
68
+ vector.x += x;
69
+ vector.y += y;
70
+ break;
71
+ case 'line':
72
+ vector.x1 += x;
73
+ vector.x2 += x;
74
+ vector.y1 += y;
75
+ vector.y2 += y;
76
+ break;
77
+ case 'polyline':
78
+ for (var i = 0, l = vector.points.length; i < l; i++) {
79
+ vector.points[i].x += x;
80
+ vector.points[i].y += y;
81
+ }
82
+ break;
83
+ }
84
+ }
85
+
86
+ function fontStringify(key, val) {
87
+ if (key === 'font') {
88
+ return 'font';
89
+ }
90
+ return val;
91
+ }
92
+
93
+ function getNodeId(node) {
94
+ if (node.id) {
95
+ return node.id;
96
+ }
97
+
98
+ if (isArray(node.text)) {
99
+ for (var i = 0, l = node.text.length; i < l; i++) {
100
+ var n = node.text[i];
101
+ var nodeId = getNodeId(n);
102
+ if (nodeId) {
103
+ return nodeId;
104
+ }
105
+ }
106
+ }
107
+
108
+ return null;
109
+ }
110
+
111
+ function isPattern(color) {
112
+ return isArray(color) && color.length === 2;
113
+ }
114
+
115
+ // converts from a [<pattern name>, <color>] as used by pdfmake
116
+ // into [<pattern object>, <color>] as used by pdfkit
117
+ // (the pattern has to be registered in the doc definition of course)
118
+ function getPattern(color, patterns) {
119
+ return [patterns[color[0]], color[1]];
120
+ }
121
+
122
+ module.exports = {
123
+ isString: isString,
124
+ isNumber: isNumber,
125
+ isBoolean: isBoolean,
126
+ isArray: isArray,
127
+ isFunction: isFunction,
128
+ isObject: isObject,
129
+ isNull: isNull,
130
+ isUndefined: isUndefined,
131
+ isPositiveInteger: isPositiveInteger,
132
+ pack: pack,
133
+ fontStringify: fontStringify,
134
+ offsetVector: offsetVector,
135
+ getNodeId: getNodeId,
136
+ isPattern: isPattern,
137
+ getPattern: getPattern
138
+ };
@@ -1,70 +1,70 @@
1
- 'use strict';
2
-
3
- var fs = require('fs');
4
-
5
- function ImageMeasure(pdfKitDoc, imageDictionary) {
6
- this.pdfKitDoc = pdfKitDoc;
7
- this.imageDictionary = imageDictionary || {};
8
- }
9
-
10
- ImageMeasure.prototype.measureImage = function (src) {
11
- var image;
12
- var that = this;
13
-
14
- if (!this.pdfKitDoc._imageRegistry[src]) {
15
- try {
16
- image = this.pdfKitDoc.openImage(realImageSrc(src));
17
- if (!image) {
18
- throw 'No image';
19
- }
20
- } catch (error) {
21
- throw 'Invalid image: ' + error.toString() + '\nImages dictionary should contain dataURL entries (or local file paths in node.js)';
22
- }
23
- image.embed(this.pdfKitDoc);
24
- this.pdfKitDoc._imageRegistry[src] = image;
25
- } else {
26
- image = this.pdfKitDoc._imageRegistry[src];
27
- }
28
-
29
- var imageSize = { width: image.width, height: image.height };
30
-
31
- // If EXIF orientation calls for it, swap width and height
32
- if (image.orientation > 4) {
33
- imageSize = { width: image.height, height: image.width };
34
- }
35
-
36
- return imageSize;
37
-
38
- function realImageSrc(src) {
39
- var img = that.imageDictionary[src];
40
-
41
- if (!img) {
42
- return src;
43
- }
44
-
45
- if (Buffer.isBuffer && Buffer.isBuffer(img)) {
46
- return img;
47
- }
48
-
49
- if (img instanceof Uint8Array) {
50
- return Buffer.from(img);
51
- }
52
-
53
- if (typeof img === 'object') {
54
- throw 'Not supported image definition: ' + JSON.stringify(img);
55
- }
56
-
57
- if (fs.existsSync(img)) {
58
- return fs.readFileSync(img);
59
- }
60
-
61
- var index = img.indexOf('base64,');
62
- if (index < 0) {
63
- return that.imageDictionary[src];
64
- }
65
-
66
- return Buffer.from(img.substring(index + 7), 'base64');
67
- }
68
- };
69
-
70
- module.exports = ImageMeasure;
1
+ 'use strict';
2
+
3
+ var fs = require('fs');
4
+
5
+ function ImageMeasure(pdfKitDoc, imageDictionary) {
6
+ this.pdfKitDoc = pdfKitDoc;
7
+ this.imageDictionary = imageDictionary || {};
8
+ }
9
+
10
+ ImageMeasure.prototype.measureImage = function (src) {
11
+ var image;
12
+ var that = this;
13
+
14
+ if (!this.pdfKitDoc._imageRegistry[src]) {
15
+ try {
16
+ image = this.pdfKitDoc.openImage(realImageSrc(src));
17
+ if (!image) {
18
+ throw 'No image';
19
+ }
20
+ } catch (error) {
21
+ throw 'Invalid image: ' + error.toString() + '\nImages dictionary should contain dataURL entries (or local file paths in node.js)';
22
+ }
23
+ image.embed(this.pdfKitDoc);
24
+ this.pdfKitDoc._imageRegistry[src] = image;
25
+ } else {
26
+ image = this.pdfKitDoc._imageRegistry[src];
27
+ }
28
+
29
+ var imageSize = { width: image.width, height: image.height };
30
+
31
+ // If EXIF orientation calls for it, swap width and height
32
+ if (image.orientation > 4) {
33
+ imageSize = { width: image.height, height: image.width };
34
+ }
35
+
36
+ return imageSize;
37
+
38
+ function realImageSrc(src) {
39
+ var img = that.imageDictionary[src];
40
+
41
+ if (!img) {
42
+ return src;
43
+ }
44
+
45
+ if (Buffer.isBuffer && Buffer.isBuffer(img)) {
46
+ return img;
47
+ }
48
+
49
+ if (img instanceof Uint8Array) {
50
+ return Buffer.from(img);
51
+ }
52
+
53
+ if (typeof img === 'object') {
54
+ throw 'Not supported image definition: ' + JSON.stringify(img);
55
+ }
56
+
57
+ if (fs.existsSync(img)) {
58
+ return fs.readFileSync(img);
59
+ }
60
+
61
+ var index = img.indexOf('base64,');
62
+ if (index < 0) {
63
+ return that.imageDictionary[src];
64
+ }
65
+
66
+ return Buffer.from(img.substring(index + 7), 'base64');
67
+ }
68
+ };
69
+
70
+ module.exports = ImageMeasure;