pdfmake 0.2.0 → 0.2.4
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/CHANGELOG.md +22 -0
- package/build/pdfmake.js +19856 -18345
- package/build/pdfmake.js.map +1 -1
- package/build/pdfmake.min.js +2 -2
- package/build/pdfmake.min.js.map +1 -1
- package/build-vfs.js +1 -1
- package/package.json +8 -6
- package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -0
- package/src/3rd-party/svg-to-pdfkit/source.js +2552 -0
- package/src/3rd-party/svg-to-pdfkit.js +3 -0
- package/src/browser-extensions/pdfMake.js +8 -13
- package/src/helpers.js +14 -1
- package/src/printer.js +43 -23
- package/src/svgMeasure.js +1 -1
- package/src/tableProcessor.js +31 -12
- package/src/textDecorator.js +8 -2
|
@@ -24,20 +24,15 @@ function Document(docDefinition, tableLayouts, fonts, vfs) {
|
|
|
24
24
|
|
|
25
25
|
function canCreatePdf() {
|
|
26
26
|
// Ensure the browser provides the level of support needed
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (!Object.keys || typeof Uint16Array === 'undefined') {
|
|
38
|
-
return false;
|
|
27
|
+
try {
|
|
28
|
+
var arr = new Uint8Array(1)
|
|
29
|
+
var proto = { foo: function () { return 42 } }
|
|
30
|
+
Object.setPrototypeOf(proto, Uint8Array.prototype)
|
|
31
|
+
Object.setPrototypeOf(arr, proto)
|
|
32
|
+
return arr.foo() === 42
|
|
33
|
+
} catch (e) {
|
|
34
|
+
return false
|
|
39
35
|
}
|
|
40
|
-
return true;
|
|
41
36
|
}
|
|
42
37
|
|
|
43
38
|
Document.prototype._createDoc = function (options, cb) {
|
package/src/helpers.js
CHANGED
|
@@ -97,6 +97,17 @@ function getNodeId(node) {
|
|
|
97
97
|
return null;
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
function isPattern(color) {
|
|
101
|
+
return isArray(color) && color.length === 2;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// converts from a [<pattern name>, <color>] as used by pdfmake
|
|
105
|
+
// into [<pattern object>, <color>] as used by pdfkit
|
|
106
|
+
// (the pattern has to be registered in the doc definition of course)
|
|
107
|
+
function getPattern(color, patterns) {
|
|
108
|
+
return [patterns[color[0]], color[1]];
|
|
109
|
+
}
|
|
110
|
+
|
|
100
111
|
module.exports = {
|
|
101
112
|
isString: isString,
|
|
102
113
|
isNumber: isNumber,
|
|
@@ -109,5 +120,7 @@ module.exports = {
|
|
|
109
120
|
pack: pack,
|
|
110
121
|
fontStringify: fontStringify,
|
|
111
122
|
offsetVector: offsetVector,
|
|
112
|
-
getNodeId: getNodeId
|
|
123
|
+
getNodeId: getNodeId,
|
|
124
|
+
isPattern: isPattern,
|
|
125
|
+
getPattern: getPattern
|
|
113
126
|
};
|
package/src/printer.js
CHANGED
|
@@ -15,15 +15,9 @@ 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
|
-
|
|
19
|
-
var
|
|
20
|
-
|
|
21
|
-
// optional dependency to support svg nodes
|
|
22
|
-
return require('svg-to-pdfkit');
|
|
23
|
-
} catch (e) {
|
|
24
|
-
throw new Error('Please install svg-to-pdfkit to enable svg nodes');
|
|
25
|
-
}
|
|
26
|
-
};
|
|
18
|
+
var isPattern = require('./helpers').isPattern;
|
|
19
|
+
var getPattern = require('./helpers').getPattern;
|
|
20
|
+
var SVGtoPDF = require('./3rd-party/svg-to-pdfkit');
|
|
27
21
|
|
|
28
22
|
var findFont = function (fonts, requiredFonts, defaultFont) {
|
|
29
23
|
for (var i = 0; i < requiredFonts.length; i++) {
|
|
@@ -96,8 +90,16 @@ function PdfPrinter(fontDescriptors) {
|
|
|
96
90
|
* ],
|
|
97
91
|
* styles: {
|
|
98
92
|
* header: { fontSize: 30, bold: true }
|
|
93
|
+
* },
|
|
94
|
+
* patterns: {
|
|
95
|
+
* stripe45d: {
|
|
96
|
+
* boundingBox: [1, 1, 4, 4],
|
|
97
|
+
* xStep: 3,
|
|
98
|
+
* yStep: 3,
|
|
99
|
+
* pattern: '1 w 0 1 m 4 5 l s 2 0 m 5 3 l s'
|
|
100
|
+
* }
|
|
99
101
|
* }
|
|
100
|
-
* }
|
|
102
|
+
* };
|
|
101
103
|
*
|
|
102
104
|
* var pdfKitDoc = printer.createPdfKitDocument(docDefinition);
|
|
103
105
|
*
|
|
@@ -126,11 +128,11 @@ PdfPrinter.prototype.createPdfKitDocument = function (docDefinition, options) {
|
|
|
126
128
|
fontLayoutCache: isBoolean(options.fontLayoutCache) ? options.fontLayoutCache : true,
|
|
127
129
|
bufferPages: options.bufferPages || false,
|
|
128
130
|
autoFirstPage: false,
|
|
131
|
+
info: createMetadata(docDefinition),
|
|
129
132
|
font: null
|
|
130
133
|
};
|
|
131
134
|
|
|
132
135
|
this.pdfKitDoc = PdfKitEngine.createPdfDocument(pdfOptions);
|
|
133
|
-
setMetadata(docDefinition, this.pdfKitDoc);
|
|
134
136
|
|
|
135
137
|
this.fontProvider = new FontProvider(this.fontDescriptors, this.pdfKitDoc);
|
|
136
138
|
|
|
@@ -157,7 +159,9 @@ PdfPrinter.prototype.createPdfKitDocument = function (docDefinition, options) {
|
|
|
157
159
|
this.pdfKitDoc.options.size = [pageSize.width, pageHeight];
|
|
158
160
|
}
|
|
159
161
|
|
|
160
|
-
|
|
162
|
+
var patterns = createPatterns(docDefinition.patterns || {}, this.pdfKitDoc);
|
|
163
|
+
|
|
164
|
+
renderPages(pages, this.fontProvider, this.pdfKitDoc, patterns, options.progressCallback);
|
|
161
165
|
|
|
162
166
|
if (options.autoPrint) {
|
|
163
167
|
var printActionRef = this.pdfKitDoc.ref({
|
|
@@ -171,7 +175,7 @@ PdfPrinter.prototype.createPdfKitDocument = function (docDefinition, options) {
|
|
|
171
175
|
return this.pdfKitDoc;
|
|
172
176
|
};
|
|
173
177
|
|
|
174
|
-
function
|
|
178
|
+
function createMetadata(docDefinition) {
|
|
175
179
|
// PDF standard has these properties reserved: Title, Author, Subject, Keywords,
|
|
176
180
|
// Creator, Producer, CreationDate, ModDate, Trapped.
|
|
177
181
|
// To keep the pdfmake api consistent, the info field are defined lowercase.
|
|
@@ -187,18 +191,21 @@ function setMetadata(docDefinition, pdfKitDoc) {
|
|
|
187
191
|
return key.replace(/\s+/g, '');
|
|
188
192
|
}
|
|
189
193
|
|
|
190
|
-
|
|
191
|
-
|
|
194
|
+
var info = {
|
|
195
|
+
Producer: 'pdfmake',
|
|
196
|
+
Creator: 'pdfmake'
|
|
197
|
+
};
|
|
192
198
|
|
|
193
199
|
if (docDefinition.info) {
|
|
194
200
|
for (var key in docDefinition.info) {
|
|
195
201
|
var value = docDefinition.info[key];
|
|
196
202
|
if (value) {
|
|
197
203
|
key = standardizePropertyKey(key);
|
|
198
|
-
|
|
204
|
+
info[key] = value;
|
|
199
205
|
}
|
|
200
206
|
}
|
|
201
207
|
}
|
|
208
|
+
return info;
|
|
202
209
|
}
|
|
203
210
|
|
|
204
211
|
function calculatePageHeight(pages, margins) {
|
|
@@ -355,7 +362,7 @@ function updatePageOrientationInOptions(currentPage, pdfKitDoc) {
|
|
|
355
362
|
}
|
|
356
363
|
}
|
|
357
364
|
|
|
358
|
-
function renderPages(pages, fontProvider, pdfKitDoc, progressCallback) {
|
|
365
|
+
function renderPages(pages, fontProvider, pdfKitDoc, patterns, progressCallback) {
|
|
359
366
|
pdfKitDoc._pdfMakePages = pages;
|
|
360
367
|
pdfKitDoc.addPage();
|
|
361
368
|
|
|
@@ -381,10 +388,10 @@ function renderPages(pages, fontProvider, pdfKitDoc, progressCallback) {
|
|
|
381
388
|
var item = page.items[ii];
|
|
382
389
|
switch (item.type) {
|
|
383
390
|
case 'vector':
|
|
384
|
-
renderVector(item.item, pdfKitDoc);
|
|
391
|
+
renderVector(item.item, patterns, pdfKitDoc);
|
|
385
392
|
break;
|
|
386
393
|
case 'line':
|
|
387
|
-
renderLine(item.item, item.item.x, item.item.y, pdfKitDoc);
|
|
394
|
+
renderLine(item.item, item.item.x, item.item.y, patterns, pdfKitDoc);
|
|
388
395
|
break;
|
|
389
396
|
case 'image':
|
|
390
397
|
renderImage(item.item, item.item.x, item.item.y, pdfKitDoc);
|
|
@@ -427,7 +434,7 @@ function offsetText(y, inline) {
|
|
|
427
434
|
return newY;
|
|
428
435
|
}
|
|
429
436
|
|
|
430
|
-
function renderLine(line, x, y, pdfKitDoc) {
|
|
437
|
+
function renderLine(line, x, y, patterns, pdfKitDoc) {
|
|
431
438
|
function preparePageNodeRefLine(_pageNodeRef, inline) {
|
|
432
439
|
var newWidth;
|
|
433
440
|
var diffWidth;
|
|
@@ -465,7 +472,7 @@ function renderLine(line, x, y, pdfKitDoc) {
|
|
|
465
472
|
var ascenderHeight = line.getAscenderHeight();
|
|
466
473
|
var descent = lineHeight - ascenderHeight;
|
|
467
474
|
|
|
468
|
-
textDecorator.drawBackground(line, x, y, pdfKitDoc);
|
|
475
|
+
textDecorator.drawBackground(line, x, y, patterns, pdfKitDoc);
|
|
469
476
|
|
|
470
477
|
//TODO: line.optimizeInlines();
|
|
471
478
|
for (var i = 0, l = line.inlines.length; i < l; i++) {
|
|
@@ -539,7 +546,7 @@ function renderWatermark(page, pdfKitDoc) {
|
|
|
539
546
|
pdfKitDoc.restore();
|
|
540
547
|
}
|
|
541
548
|
|
|
542
|
-
function renderVector(vector, pdfKitDoc) {
|
|
549
|
+
function renderVector(vector, patterns, pdfKitDoc) {
|
|
543
550
|
//TODO: pdf optimization (there's no need to write all properties everytime)
|
|
544
551
|
pdfKitDoc.lineWidth(vector.lineWidth || 1);
|
|
545
552
|
if (vector.dash) {
|
|
@@ -611,6 +618,10 @@ function renderVector(vector, pdfKitDoc) {
|
|
|
611
618
|
vector.color = gradient;
|
|
612
619
|
}
|
|
613
620
|
|
|
621
|
+
if (isPattern(vector.color)) {
|
|
622
|
+
vector.color = getPattern(vector.color, patterns);
|
|
623
|
+
}
|
|
624
|
+
|
|
614
625
|
var fillOpacity = isNumber(vector.fillOpacity) ? vector.fillOpacity : 1;
|
|
615
626
|
var strokeOpacity = isNumber(vector.strokeOpacity) ? vector.strokeOpacity : 1;
|
|
616
627
|
|
|
@@ -669,7 +680,7 @@ function renderSVG(svg, x, y, pdfKitDoc, fontProvider) {
|
|
|
669
680
|
return fontFile;
|
|
670
681
|
};
|
|
671
682
|
|
|
672
|
-
|
|
683
|
+
SVGtoPDF(pdfKitDoc, svg.svg, svg.x, svg.y, options);
|
|
673
684
|
}
|
|
674
685
|
|
|
675
686
|
function beginClip(rect, pdfKitDoc) {
|
|
@@ -682,4 +693,13 @@ function endClip(pdfKitDoc) {
|
|
|
682
693
|
pdfKitDoc.restore();
|
|
683
694
|
}
|
|
684
695
|
|
|
696
|
+
function createPatterns(patternDefinitions, pdfKitDoc) {
|
|
697
|
+
var patterns = {};
|
|
698
|
+
Object.keys(patternDefinitions).forEach(function (p) {
|
|
699
|
+
var pattern = patternDefinitions[p];
|
|
700
|
+
patterns[p] = pdfKitDoc.pattern(pattern.boundingBox, pattern.xStep, pattern.yStep, pattern.pattern, pattern.colored);
|
|
701
|
+
});
|
|
702
|
+
return patterns;
|
|
703
|
+
}
|
|
704
|
+
|
|
685
705
|
module.exports = PdfPrinter;
|
package/src/svgMeasure.js
CHANGED
package/src/tableProcessor.js
CHANGED
|
@@ -426,8 +426,10 @@ TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks) {
|
|
|
426
426
|
}
|
|
427
427
|
if (!isNumber(fillOpacity)) {
|
|
428
428
|
fillOpacity = isFunction(this.layout.fillOpacity) ? this.layout.fillOpacity(rowIndex, this.tableNode, colIndex) : this.layout.fillOpacity;
|
|
429
|
-
|
|
430
|
-
|
|
429
|
+
}
|
|
430
|
+
var overlayPattern = body[rowIndex][colIndex].overlayPattern;
|
|
431
|
+
var overlayOpacity = body[rowIndex][colIndex].overlayOpacity;
|
|
432
|
+
if (fillColor || overlayPattern) {
|
|
431
433
|
var widthLeftBorder = leftCellBorder ? this.layout.vLineWidth(colIndex, this.tableNode) : 0;
|
|
432
434
|
var widthRightBorder;
|
|
433
435
|
if ((colIndex === 0 || colIndex + 1 == body[rowIndex].length) && !rightCellBorder) {
|
|
@@ -442,16 +444,33 @@ TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks) {
|
|
|
442
444
|
var y1f = this.dontBreakRows ? y1 : y1 - (hzLineOffset / 2);
|
|
443
445
|
var x2f = xs[i + 1].x + widthRightBorder;
|
|
444
446
|
var y2f = this.dontBreakRows ? y2 + this.bottomLineWidth : y2 + (this.bottomLineWidth / 2);
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
447
|
+
var bgWidth = x2f - x1f;
|
|
448
|
+
var bgHeight = y2f - y1f;
|
|
449
|
+
if (fillColor) {
|
|
450
|
+
writer.addVector({
|
|
451
|
+
type: 'rect',
|
|
452
|
+
x: x1f,
|
|
453
|
+
y: y1f,
|
|
454
|
+
w: bgWidth,
|
|
455
|
+
h: bgHeight,
|
|
456
|
+
lineWidth: 0,
|
|
457
|
+
color: fillColor,
|
|
458
|
+
fillOpacity: fillOpacity
|
|
459
|
+
}, false, true, writer.context().backgroundLength[writer.context().page]);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
if (overlayPattern) {
|
|
463
|
+
writer.addVector({
|
|
464
|
+
type: 'rect',
|
|
465
|
+
x: x1f,
|
|
466
|
+
y: y1f,
|
|
467
|
+
w: bgWidth,
|
|
468
|
+
h: bgHeight,
|
|
469
|
+
lineWidth: 0,
|
|
470
|
+
color: overlayPattern,
|
|
471
|
+
fillOpacity: overlayOpacity
|
|
472
|
+
}, false, true);
|
|
473
|
+
}
|
|
455
474
|
}
|
|
456
475
|
}
|
|
457
476
|
}
|
package/src/textDecorator.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var isArray = require('./helpers').isArray;
|
|
4
|
+
var isPattern = require('./helpers').isPattern;
|
|
5
|
+
var getPattern = require('./helpers').getPattern;
|
|
4
6
|
|
|
5
7
|
function groupDecorations(line) {
|
|
6
8
|
var groups = [], currentGroup = null;
|
|
@@ -131,15 +133,19 @@ function drawDecorations(line, x, y, pdfKitDoc) {
|
|
|
131
133
|
}
|
|
132
134
|
}
|
|
133
135
|
|
|
134
|
-
function drawBackground(line, x, y, pdfKitDoc) {
|
|
136
|
+
function drawBackground(line, x, y, patterns, pdfKitDoc) {
|
|
135
137
|
var height = line.getHeight();
|
|
136
138
|
for (var i = 0, l = line.inlines.length; i < l; i++) {
|
|
137
139
|
var inline = line.inlines[i];
|
|
138
140
|
if (!inline.background) {
|
|
139
141
|
continue;
|
|
140
142
|
}
|
|
143
|
+
var color = inline.background;
|
|
144
|
+
if (isPattern(inline.background)) {
|
|
145
|
+
color = getPattern(inline.background, patterns);
|
|
146
|
+
}
|
|
141
147
|
var justifyShift = (inline.justifyShift || 0);
|
|
142
|
-
pdfKitDoc.fillColor(
|
|
148
|
+
pdfKitDoc.fillColor(color)
|
|
143
149
|
.rect(x + inline.x - justifyShift, y, inline.width + justifyShift, height)
|
|
144
150
|
.fill();
|
|
145
151
|
}
|