@flowaccount/pdfmake 0.2.20-staging.2

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.
@@ -0,0 +1,434 @@
1
+ 'use strict';
2
+
3
+ var Line = require('./line');
4
+ var isNumber = require('./helpers').isNumber;
5
+ var pack = require('./helpers').pack;
6
+ var offsetVector = require('./helpers').offsetVector;
7
+ var DocumentContext = require('./documentContext');
8
+
9
+ /**
10
+ * Creates an instance of ElementWriter - a line/vector writer, which adds
11
+ * elements to current page and sets their positions based on the context
12
+ */
13
+ function ElementWriter(context, tracker) {
14
+ this.context = context;
15
+ this.contextStack = [];
16
+ this.tracker = tracker;
17
+ }
18
+
19
+ function addPageItem(page, item, index) {
20
+ if (index === null || index === undefined || index < 0 || index > page.items.length) {
21
+ page.items.push(item);
22
+ } else {
23
+ page.items.splice(index, 0, item);
24
+ }
25
+ }
26
+
27
+ ElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, index) {
28
+ var height = line.getHeight();
29
+ var context = this.context;
30
+ var page = context.getCurrentPage(),
31
+ position = this.getCurrentPositionOnPage();
32
+
33
+ if (context.availableHeight < height || !page) {
34
+ return false;
35
+ }
36
+
37
+ line.x = context.x + (line.x || 0);
38
+ line.y = context.y + (line.y || 0);
39
+
40
+ this.alignLine(line);
41
+
42
+ addPageItem(page, {
43
+ type: 'line',
44
+ item: line
45
+ }, index);
46
+ this.tracker.emit('lineAdded', line);
47
+
48
+ if (!dontUpdateContextPosition) {
49
+ context.moveDown(height);
50
+ }
51
+
52
+ return position;
53
+ };
54
+
55
+ ElementWriter.prototype.alignLine = function (line) {
56
+ var width = this.context.availableWidth;
57
+ var lineWidth = line.getWidth();
58
+
59
+ var alignment = line.inlines && line.inlines.length > 0 && line.inlines[0].alignment;
60
+
61
+ var offset = 0;
62
+ switch (alignment) {
63
+ case 'right':
64
+ offset = width - lineWidth;
65
+ break;
66
+ case 'center':
67
+ offset = (width - lineWidth) / 2;
68
+ break;
69
+ }
70
+
71
+ if (offset) {
72
+ line.x = (line.x || 0) + offset;
73
+ }
74
+
75
+ if (alignment === 'justify' &&
76
+ !line.newLineForced &&
77
+ !line.lastLineInParagraph &&
78
+ line.inlines.length > 1) {
79
+ var additionalSpacing = (width - lineWidth) / (line.inlines.length - 1);
80
+
81
+ for (var i = 1, l = line.inlines.length; i < l; i++) {
82
+ offset = i * additionalSpacing;
83
+
84
+ line.inlines[i].x += offset;
85
+ line.inlines[i].justifyShift = additionalSpacing;
86
+ }
87
+ }
88
+ };
89
+
90
+ ElementWriter.prototype.addImage = function (image, index, type) {
91
+ var context = this.context;
92
+ var page = context.getCurrentPage(),
93
+ position = this.getCurrentPositionOnPage();
94
+
95
+ if (!page || (image.absolutePosition === undefined && context.availableHeight < image._height && page.items.length > 0)) {
96
+ return false;
97
+ }
98
+
99
+ if (image._x === undefined) {
100
+ image._x = image.x || 0;
101
+ }
102
+
103
+ image.x = context.x + image._x;
104
+ image.y = context.y;
105
+
106
+ this.alignImage(image);
107
+
108
+ addPageItem(page, {
109
+ type: type || 'image',
110
+ item: image
111
+ }, index);
112
+
113
+ context.moveDown(image._height);
114
+
115
+ return position;
116
+ };
117
+
118
+ ElementWriter.prototype.addSVG = function (image, index) {
119
+ return this.addImage(image, index, 'svg');
120
+ };
121
+
122
+ ElementWriter.prototype.addQr = function (qr, index) {
123
+ var context = this.context;
124
+ var page = context.getCurrentPage(),
125
+ position = this.getCurrentPositionOnPage();
126
+
127
+ if (!page || (qr.absolutePosition === undefined && context.availableHeight < qr._height)) {
128
+ return false;
129
+ }
130
+
131
+ if (qr._x === undefined) {
132
+ qr._x = qr.x || 0;
133
+ }
134
+
135
+ qr.x = context.x + qr._x;
136
+ qr.y = context.y;
137
+
138
+ this.alignImage(qr);
139
+
140
+ for (var i = 0, l = qr._canvas.length; i < l; i++) {
141
+ var vector = qr._canvas[i];
142
+ vector.x += qr.x;
143
+ vector.y += qr.y;
144
+ this.addVector(vector, true, true, index);
145
+ }
146
+
147
+ context.moveDown(qr._height);
148
+
149
+ return position;
150
+ };
151
+
152
+ ElementWriter.prototype.alignImage = function (image) {
153
+ var width = this.context.availableWidth;
154
+ var imageWidth = image._minWidth;
155
+ var offset = 0;
156
+ switch (image._alignment) {
157
+ case 'right':
158
+ offset = width - imageWidth;
159
+ break;
160
+ case 'center':
161
+ offset = (width - imageWidth) / 2;
162
+ break;
163
+ }
164
+
165
+ if (offset) {
166
+ image.x = (image.x || 0) + offset;
167
+ }
168
+ };
169
+
170
+ ElementWriter.prototype.alignCanvas = function (node) {
171
+ var width = this.context.availableWidth;
172
+ var canvasWidth = node._minWidth;
173
+ var offset = 0;
174
+ switch (node._alignment) {
175
+ case 'right':
176
+ offset = width - canvasWidth;
177
+ break;
178
+ case 'center':
179
+ offset = (width - canvasWidth) / 2;
180
+ break;
181
+ }
182
+ if (offset) {
183
+ node.canvas.forEach(function (vector) {
184
+ offsetVector(vector, offset, 0);
185
+ });
186
+ }
187
+ };
188
+
189
+ ElementWriter.prototype.addVector = function (vector, ignoreContextX, ignoreContextY, index, forcePage) {
190
+ var context = this.context;
191
+ var page = context.getCurrentPage();
192
+ if (isNumber(forcePage)) {
193
+ page = context.pages[forcePage];
194
+ }
195
+ var position = this.getCurrentPositionOnPage();
196
+
197
+ if (page) {
198
+ offsetVector(vector, ignoreContextX ? 0 : context.x, ignoreContextY ? 0 : context.y);
199
+ addPageItem(page, {
200
+ type: 'vector',
201
+ item: vector
202
+ }, index);
203
+ return position;
204
+ }
205
+ };
206
+
207
+ ElementWriter.prototype.beginClip = function (width, height) {
208
+ var ctx = this.context;
209
+ var page = ctx.getCurrentPage();
210
+ page.items.push({
211
+ type: 'beginClip',
212
+ item: { x: ctx.x, y: ctx.y, width: width, height: height }
213
+ });
214
+ return true;
215
+ };
216
+
217
+ ElementWriter.prototype.endClip = function () {
218
+ var ctx = this.context;
219
+ var page = ctx.getCurrentPage();
220
+ page.items.push({
221
+ type: 'endClip'
222
+ });
223
+ return true;
224
+ };
225
+
226
+ function cloneLine(line) {
227
+ var result = new Line(line.maxWidth);
228
+
229
+ for (var key in line) {
230
+ if (line.hasOwnProperty(key)) {
231
+ result[key] = line[key];
232
+ }
233
+ }
234
+
235
+ return result;
236
+ }
237
+
238
+ ElementWriter.prototype.addFragment = function (block, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition, isFooter) {
239
+ var ctx = this.context;
240
+ var page = ctx.getCurrentPage();
241
+
242
+ if (!useBlockXOffset && block.height > ctx.availableHeight) {
243
+ return false;
244
+ }
245
+
246
+ if (isFooter && block._footerGapOption && block._footerGapOption.enabled) {
247
+ var gapHeight = ctx.availableHeight - block.height;
248
+ if (gapHeight > 0) {
249
+ var gapTopY = ctx.y;
250
+ var colSpec = block._footerGapOption.columns || null;
251
+ if (colSpec) {
252
+ var rawWidths = colSpec.content.vLines || [];
253
+
254
+ if (rawWidths && rawWidths.length > 1) {
255
+ var span = rawWidths[rawWidths.length - 1];
256
+ if (span <= 0) span = 1;
257
+ var scale = ctx.availableWidth / span;
258
+ var style = (colSpec.style || {});
259
+ var lw = style.lineWidth != null ? style.lineWidth : 0.5;
260
+ var lc = style.color || '#000000';
261
+ var dashCfg = style.dash;
262
+ var includeOuter = colSpec.includeOuter !== false;
263
+ var startIndex = includeOuter ? 0 : 1;
264
+ var endIndex = includeOuter ? rawWidths.length : rawWidths.length - 1;
265
+
266
+ for (var ci = startIndex; ci < endIndex; ci++) {
267
+ var xGuide = ctx.x + rawWidths[ci] - 0.25;
268
+ page.items.push({
269
+ type: 'vector',
270
+ item: {
271
+ type: 'line',
272
+ x1: xGuide,
273
+ y1: gapTopY,
274
+ x2: xGuide,
275
+ y2: gapTopY + gapHeight,
276
+ lineWidth: lw,
277
+ lineColor: lc,
278
+ dash: dashCfg ? {
279
+ length: dashCfg.length,
280
+ space: dashCfg.space != null ? dashCfg.space : dashCfg.gap
281
+ } : undefined,
282
+ _footerGuideLine: true
283
+ }
284
+ });
285
+ }
286
+ }
287
+ }
288
+ ctx.moveDown(gapHeight);
289
+ }
290
+ } else if(isFooter){
291
+ ctx.moveDown(ctx.availableHeight - block.height);
292
+ }
293
+
294
+ block.items.forEach(function (item) {
295
+ switch (item.type) {
296
+ case 'line':
297
+ var l = cloneLine(item.item);
298
+
299
+ if (l._node) {
300
+ l._node.positions[0].pageNumber = ctx.page + 1;
301
+ }
302
+ l.x = (l.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
303
+ l.y = (l.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
304
+
305
+ page.items.push({
306
+ type: 'line',
307
+ item: l
308
+ });
309
+ break;
310
+
311
+ case 'vector':
312
+ var v = pack(item.item);
313
+
314
+ offsetVector(v, useBlockXOffset ? (block.xOffset || 0) : ctx.x, useBlockYOffset ? (block.yOffset || 0) : ctx.y);
315
+ if (v._isFillColorFromUnbreakable) {
316
+ // If the item is a fillColor from an unbreakable block
317
+ // We have to add it at the beginning of the items body array of the page
318
+ delete v._isFillColorFromUnbreakable;
319
+ const endOfBackgroundItemsIndex = ctx.backgroundLength[ctx.page];
320
+ page.items.splice(endOfBackgroundItemsIndex, 0, {
321
+ type: 'vector',
322
+ item: v
323
+ });
324
+ } else {
325
+ page.items.push({
326
+ type: 'vector',
327
+ item: v
328
+ });
329
+ }
330
+ break;
331
+
332
+ case 'image':
333
+ case 'svg':
334
+ case 'beginVerticalAlign':
335
+ case 'endVerticalAlign':
336
+ case 'beginClip':
337
+ case 'endClip':
338
+ var it = pack(item.item);
339
+ it.x = (it.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
340
+ it.y = (it.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
341
+
342
+ page.items.push({
343
+ type: item.type,
344
+ item: it
345
+ });
346
+ break;
347
+ }
348
+ });
349
+
350
+ if (!dontUpdateContextPosition) {
351
+ ctx.moveDown(block.height);
352
+ }
353
+
354
+ return true;
355
+ };
356
+
357
+ /**
358
+ * Pushes the provided context onto the stack or creates a new one
359
+ *
360
+ * pushContext(context) - pushes the provided context and makes it current
361
+ * pushContext(width, height) - creates and pushes a new context with the specified width and height
362
+ * pushContext() - creates a new context for unbreakable blocks (with current availableWidth and full-page-height)
363
+ */
364
+ ElementWriter.prototype.pushContext = function (contextOrWidth, height) {
365
+ if (contextOrWidth === undefined) {
366
+ height = this.context.getCurrentPage().height - this.context.pageMargins.top - this.context.pageMargins.bottom;
367
+ contextOrWidth = this.context.availableWidth;
368
+ }
369
+
370
+ if (isNumber(contextOrWidth)) {
371
+ if (this.context._footerGapOption) {
372
+ contextOrWidth = new DocumentContext({width: contextOrWidth, height: height}, {left: 0, right: 0, top: 0, bottom: 0}, this.context._footerGapOption);
373
+ } else {
374
+ contextOrWidth = new DocumentContext({ width: contextOrWidth, height: height }, { left: 0, right: 0, top: 0, bottom: 0 });
375
+ }
376
+ }
377
+
378
+ this.contextStack.push(this.context);
379
+ this.context = contextOrWidth;
380
+ };
381
+
382
+ ElementWriter.prototype.popContext = function () {
383
+ this.context = this.contextStack.pop();
384
+ };
385
+
386
+ ElementWriter.prototype.getCurrentPositionOnPage = function () {
387
+ return (this.contextStack[0] || this.context).getCurrentPosition();
388
+ };
389
+
390
+ ElementWriter.prototype.beginClip = function (width, height) {
391
+ var ctx = this.context;
392
+ var page = ctx.getCurrentPage();
393
+ var item = {
394
+ type: 'beginClip',
395
+ item: { x: ctx.x, y: ctx.y, width: width, height: height }
396
+ };
397
+ page.items.push(item);
398
+ return item;
399
+ };
400
+
401
+ ElementWriter.prototype.endClip = function () {
402
+ var ctx = this.context;
403
+ var page = ctx.getCurrentPage();
404
+ var item = {
405
+ type: 'endClip'
406
+ };
407
+ page.items.push(item);
408
+ return item;
409
+ };
410
+
411
+ ElementWriter.prototype.beginVerticalAlign = function (verticalAlign) {
412
+ var ctx = this.context;
413
+ var page = ctx.getCurrentPage();
414
+ var item = {
415
+ type: 'beginVerticalAlign',
416
+ item: { verticalAlign: verticalAlign }
417
+ };
418
+ page.items.push(item);
419
+ return item;
420
+ };
421
+
422
+ ElementWriter.prototype.endVerticalAlign = function (verticalAlign) {
423
+ var ctx = this.context;
424
+ var page = ctx.getCurrentPage();
425
+ var item = {
426
+ type: 'endVerticalAlign',
427
+ item: { verticalAlign: verticalAlign }
428
+ };
429
+ page.items.push(item);
430
+ return item;
431
+ };
432
+
433
+
434
+ module.exports = ElementWriter;
@@ -0,0 +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;
package/src/helpers.js ADDED
@@ -0,0 +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
+ };
@@ -0,0 +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;