pdfmake 0.3.0-beta.3 → 0.3.0-beta.5
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 +9 -0
- package/build/pdfmake.js +2213 -8630
- 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_fonts.js +6 -5
- package/js/3rd-party/svg-to-pdfkit/source.js +247 -920
- package/js/3rd-party/svg-to-pdfkit.js +0 -3
- package/js/DocMeasure.js +6 -139
- package/js/DocPreprocessor.js +2 -54
- package/js/DocumentContext.js +0 -40
- package/js/ElementWriter.js +2 -70
- package/js/LayoutBuilder.js +20 -165
- package/js/Line.js +6 -25
- package/js/OutputDocument.js +15 -24
- package/js/OutputDocumentServer.js +0 -6
- package/js/PDFDocument.js +1 -46
- package/js/PageElementWriter.js +7 -31
- package/js/PageSize.js +2 -16
- package/js/Printer.js +5 -46
- package/js/Renderer.js +11 -98
- package/js/SVGMeasure.js +1 -20
- package/js/StyleContextStack.js +12 -36
- package/js/TableProcessor.js +36 -117
- package/js/TextBreaker.js +2 -44
- package/js/TextDecorator.js +1 -38
- package/js/TextInlines.js +8 -49
- package/js/URLResolver.js +0 -13
- package/js/base.js +1 -17
- package/js/browser-extensions/OutputDocumentBrowser.js +5 -17
- package/js/browser-extensions/URLBrowserResolver.js +0 -17
- package/js/browser-extensions/fonts/Roboto.js +0 -4
- package/js/browser-extensions/index.js +0 -16
- package/js/browser-extensions/pdfMake.js +2 -4
- package/js/browser-extensions/standard-fonts/Courier.js +0 -4
- package/js/browser-extensions/standard-fonts/Helvetica.js +0 -4
- package/js/browser-extensions/standard-fonts/Symbol.js +0 -4
- package/js/browser-extensions/standard-fonts/Times.js +0 -4
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +0 -4
- package/js/columnCalculator.js +6 -18
- package/js/helpers/node.js +3 -27
- package/js/helpers/tools.js +0 -8
- package/js/helpers/variableType.js +4 -9
- package/js/index.js +0 -6
- package/js/qrEnc.js +126 -215
- package/js/tableLayouts.js +1 -28
- package/js/virtual-fs.js +3 -19
- package/package.json +2 -2
- package/src/OutputDocument.js +78 -78
package/js/Printer.js
CHANGED
|
@@ -2,23 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.default = void 0;
|
|
5
|
-
|
|
6
5
|
var _PDFDocument = _interopRequireDefault(require("./PDFDocument"));
|
|
7
|
-
|
|
8
6
|
var _LayoutBuilder = _interopRequireDefault(require("./LayoutBuilder"));
|
|
9
|
-
|
|
10
7
|
var _SVGMeasure = _interopRequireDefault(require("./SVGMeasure"));
|
|
11
|
-
|
|
12
8
|
var _PageSize = require("./PageSize");
|
|
13
|
-
|
|
14
9
|
var _tableLayouts = require("./tableLayouts");
|
|
15
|
-
|
|
16
10
|
var _Renderer = _interopRequireDefault(require("./Renderer"));
|
|
17
|
-
|
|
18
11
|
var _variableType = require("./helpers/variableType");
|
|
19
|
-
|
|
20
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
-
|
|
22
13
|
/**
|
|
23
14
|
* Printer which turns document definition into a pdf
|
|
24
15
|
*
|
|
@@ -45,6 +36,7 @@ class PdfPrinter {
|
|
|
45
36
|
this.virtualfs = virtualfs;
|
|
46
37
|
this.urlResolver = urlResolver;
|
|
47
38
|
}
|
|
39
|
+
|
|
48
40
|
/**
|
|
49
41
|
* Executes layout engine for the specified document and renders it into a pdfkit document
|
|
50
42
|
* ready to be saved.
|
|
@@ -53,8 +45,6 @@ class PdfPrinter {
|
|
|
53
45
|
* @param {object} options
|
|
54
46
|
* @returns {Promise<PDFDocument>} resolved promise return a pdfkit document
|
|
55
47
|
*/
|
|
56
|
-
|
|
57
|
-
|
|
58
48
|
createPdfKitDocument(docDefinition, options = {}) {
|
|
59
49
|
return new Promise((resolve, reject) => {
|
|
60
50
|
this.resolveUrls(docDefinition).then(() => {
|
|
@@ -83,28 +73,24 @@ class PdfPrinter {
|
|
|
83
73
|
embedFiles(docDefinition, this.pdfKitDoc);
|
|
84
74
|
const builder = new _LayoutBuilder.default(pageSize, (0, _PageSize.normalizePageMargin)(docDefinition.pageMargins), new _SVGMeasure.default());
|
|
85
75
|
builder.registerTableLayouts(_tableLayouts.tableLayouts);
|
|
86
|
-
|
|
87
76
|
if (options.tableLayouts) {
|
|
88
77
|
builder.registerTableLayouts(options.tableLayouts);
|
|
89
78
|
}
|
|
90
|
-
|
|
91
79
|
let pages = builder.layoutDocument(docDefinition.content, this.pdfKitDoc, docDefinition.styles || {}, docDefinition.defaultStyle || {
|
|
92
80
|
fontSize: 12,
|
|
93
81
|
font: 'Roboto'
|
|
94
82
|
}, docDefinition.background, docDefinition.header, docDefinition.footer, docDefinition.watermark, docDefinition.pageBreakBefore);
|
|
95
83
|
let maxNumberPages = docDefinition.maxPagesNumber || -1;
|
|
96
|
-
|
|
97
84
|
if ((0, _variableType.isNumber)(maxNumberPages) && maxNumberPages > -1) {
|
|
98
85
|
pages = pages.slice(0, maxNumberPages);
|
|
99
|
-
}
|
|
100
|
-
// was laid out using the height of each of the items in the page.
|
|
101
|
-
|
|
86
|
+
}
|
|
102
87
|
|
|
88
|
+
// if pageSize.height is set to Infinity, calculate the actual height of the page that
|
|
89
|
+
// was laid out using the height of each of the items in the page.
|
|
103
90
|
if (pageSize.height === Infinity) {
|
|
104
91
|
let pageHeight = calculatePageHeight(pages, docDefinition.pageMargins);
|
|
105
92
|
this.pdfKitDoc.options.size = [pageSize.width, pageHeight];
|
|
106
93
|
}
|
|
107
|
-
|
|
108
94
|
const renderer = new _Renderer.default(this.pdfKitDoc, options.progressCallback);
|
|
109
95
|
renderer.renderPages(pages);
|
|
110
96
|
resolve(this.pdfKitDoc);
|
|
@@ -116,12 +102,11 @@ class PdfPrinter {
|
|
|
116
102
|
});
|
|
117
103
|
});
|
|
118
104
|
}
|
|
105
|
+
|
|
119
106
|
/**
|
|
120
107
|
* @param {object} docDefinition
|
|
121
108
|
* @returns {Promise}
|
|
122
109
|
*/
|
|
123
|
-
|
|
124
|
-
|
|
125
110
|
resolveUrls(docDefinition) {
|
|
126
111
|
const getExtendedUrl = url => {
|
|
127
112
|
if (typeof url === 'object') {
|
|
@@ -130,18 +115,15 @@ class PdfPrinter {
|
|
|
130
115
|
headers: url.headers
|
|
131
116
|
};
|
|
132
117
|
}
|
|
133
|
-
|
|
134
118
|
return {
|
|
135
119
|
url: url,
|
|
136
120
|
headers: {}
|
|
137
121
|
};
|
|
138
122
|
};
|
|
139
|
-
|
|
140
123
|
return new Promise((resolve, reject) => {
|
|
141
124
|
if (this.urlResolver === null) {
|
|
142
125
|
resolve();
|
|
143
126
|
}
|
|
144
|
-
|
|
145
127
|
for (let font in this.fontDescriptors) {
|
|
146
128
|
if (this.fontDescriptors.hasOwnProperty(font)) {
|
|
147
129
|
if (this.fontDescriptors[font].normal) {
|
|
@@ -156,7 +138,6 @@ class PdfPrinter {
|
|
|
156
138
|
this.fontDescriptors[font].normal = url.url;
|
|
157
139
|
}
|
|
158
140
|
}
|
|
159
|
-
|
|
160
141
|
if (this.fontDescriptors[font].bold) {
|
|
161
142
|
if (Array.isArray(this.fontDescriptors[font].bold)) {
|
|
162
143
|
// TrueType Collection
|
|
@@ -169,7 +150,6 @@ class PdfPrinter {
|
|
|
169
150
|
this.fontDescriptors[font].bold = url.url;
|
|
170
151
|
}
|
|
171
152
|
}
|
|
172
|
-
|
|
173
153
|
if (this.fontDescriptors[font].italics) {
|
|
174
154
|
if (Array.isArray(this.fontDescriptors[font].italics)) {
|
|
175
155
|
// TrueType Collection
|
|
@@ -182,7 +162,6 @@ class PdfPrinter {
|
|
|
182
162
|
this.fontDescriptors[font].italics = url.url;
|
|
183
163
|
}
|
|
184
164
|
}
|
|
185
|
-
|
|
186
165
|
if (this.fontDescriptors[font].bolditalics) {
|
|
187
166
|
if (Array.isArray(this.fontDescriptors[font].bolditalics)) {
|
|
188
167
|
// TrueType Collection
|
|
@@ -197,7 +176,6 @@ class PdfPrinter {
|
|
|
197
176
|
}
|
|
198
177
|
}
|
|
199
178
|
}
|
|
200
|
-
|
|
201
179
|
if (docDefinition.images) {
|
|
202
180
|
for (let image in docDefinition.images) {
|
|
203
181
|
if (docDefinition.images.hasOwnProperty(image)) {
|
|
@@ -207,7 +185,6 @@ class PdfPrinter {
|
|
|
207
185
|
}
|
|
208
186
|
}
|
|
209
187
|
}
|
|
210
|
-
|
|
211
188
|
if (docDefinition.attachments) {
|
|
212
189
|
for (let attachment in docDefinition.attachments) {
|
|
213
190
|
if (docDefinition.attachments.hasOwnProperty(attachment) && docDefinition.attachments[attachment].src) {
|
|
@@ -217,7 +194,6 @@ class PdfPrinter {
|
|
|
217
194
|
}
|
|
218
195
|
}
|
|
219
196
|
}
|
|
220
|
-
|
|
221
197
|
if (docDefinition.files) {
|
|
222
198
|
for (let file in docDefinition.files) {
|
|
223
199
|
if (docDefinition.files.hasOwnProperty(file) && docDefinition.files[file].src) {
|
|
@@ -227,7 +203,6 @@ class PdfPrinter {
|
|
|
227
203
|
}
|
|
228
204
|
}
|
|
229
205
|
}
|
|
230
|
-
|
|
231
206
|
this.urlResolver.resolved().then(() => {
|
|
232
207
|
resolve();
|
|
233
208
|
}, result => {
|
|
@@ -235,9 +210,7 @@ class PdfPrinter {
|
|
|
235
210
|
});
|
|
236
211
|
});
|
|
237
212
|
}
|
|
238
|
-
|
|
239
213
|
}
|
|
240
|
-
|
|
241
214
|
function createMetadata(docDefinition) {
|
|
242
215
|
// PDF standard has these properties reserved: Title, Author, Subject, Keywords,
|
|
243
216
|
// Creator, Producer, CreationDate, ModDate, Trapped.
|
|
@@ -246,49 +219,39 @@ function createMetadata(docDefinition) {
|
|
|
246
219
|
function standardizePropertyKey(key) {
|
|
247
220
|
let standardProperties = ['Title', 'Author', 'Subject', 'Keywords', 'Creator', 'Producer', 'CreationDate', 'ModDate', 'Trapped'];
|
|
248
221
|
let standardizedKey = key.charAt(0).toUpperCase() + key.slice(1);
|
|
249
|
-
|
|
250
222
|
if (standardProperties.includes(standardizedKey)) {
|
|
251
223
|
return standardizedKey;
|
|
252
224
|
}
|
|
253
|
-
|
|
254
225
|
return key.replace(/\s+/g, '');
|
|
255
226
|
}
|
|
256
|
-
|
|
257
227
|
let info = {
|
|
258
228
|
Producer: 'pdfmake',
|
|
259
229
|
Creator: 'pdfmake'
|
|
260
230
|
};
|
|
261
|
-
|
|
262
231
|
if (docDefinition.info) {
|
|
263
232
|
for (let key in docDefinition.info) {
|
|
264
233
|
let value = docDefinition.info[key];
|
|
265
|
-
|
|
266
234
|
if (value) {
|
|
267
235
|
key = standardizePropertyKey(key);
|
|
268
236
|
info[key] = value;
|
|
269
237
|
}
|
|
270
238
|
}
|
|
271
239
|
}
|
|
272
|
-
|
|
273
240
|
return info;
|
|
274
241
|
}
|
|
275
|
-
|
|
276
242
|
function embedFiles(docDefinition, pdfKitDoc) {
|
|
277
243
|
if (docDefinition.files) {
|
|
278
244
|
for (const key in docDefinition.files) {
|
|
279
245
|
const file = docDefinition.files[key];
|
|
280
246
|
if (!file.src) return;
|
|
281
|
-
|
|
282
247
|
if (pdfKitDoc.virtualfs && pdfKitDoc.virtualfs.existsSync(file.src)) {
|
|
283
248
|
file.src = pdfKitDoc.virtualfs.readFileSync(file.src);
|
|
284
249
|
}
|
|
285
|
-
|
|
286
250
|
file.name = file.name || key;
|
|
287
251
|
pdfKitDoc.file(file.src, file);
|
|
288
252
|
}
|
|
289
253
|
}
|
|
290
254
|
}
|
|
291
|
-
|
|
292
255
|
function calculatePageHeight(pages, margins) {
|
|
293
256
|
function getItemHeight(item) {
|
|
294
257
|
if (typeof item.item.getHeight === 'function') {
|
|
@@ -306,19 +269,16 @@ function calculatePageHeight(pages, margins) {
|
|
|
306
269
|
return 0;
|
|
307
270
|
}
|
|
308
271
|
}
|
|
309
|
-
|
|
310
272
|
function getBottomPosition(item) {
|
|
311
273
|
let top = item.item.y || 0;
|
|
312
274
|
let height = getItemHeight(item);
|
|
313
275
|
return top + height;
|
|
314
276
|
}
|
|
315
|
-
|
|
316
277
|
let fixedMargins = (0, _PageSize.normalizePageMargin)(margins || 40);
|
|
317
278
|
let height = fixedMargins.top;
|
|
318
279
|
pages.forEach(page => {
|
|
319
280
|
page.items.forEach(item => {
|
|
320
281
|
let bottomPosition = getBottomPosition(item);
|
|
321
|
-
|
|
322
282
|
if (bottomPosition > height) {
|
|
323
283
|
height = bottomPosition;
|
|
324
284
|
}
|
|
@@ -327,6 +287,5 @@ function calculatePageHeight(pages, margins) {
|
|
|
327
287
|
height += fixedMargins.bottom;
|
|
328
288
|
return height;
|
|
329
289
|
}
|
|
330
|
-
|
|
331
290
|
var _default = PdfPrinter;
|
|
332
291
|
exports.default = _default;
|
package/js/Renderer.js
CHANGED
|
@@ -2,30 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.default = void 0;
|
|
5
|
-
|
|
6
5
|
var _TextDecorator = _interopRequireDefault(require("./TextDecorator"));
|
|
7
|
-
|
|
8
6
|
var _TextInlines = _interopRequireDefault(require("./TextInlines"));
|
|
9
|
-
|
|
10
7
|
var _variableType = require("./helpers/variableType");
|
|
11
|
-
|
|
12
8
|
var _svgToPdfkit = _interopRequireDefault(require("./3rd-party/svg-to-pdfkit"));
|
|
13
|
-
|
|
14
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
10
|
const findFont = (fonts, requiredFonts, defaultFont) => {
|
|
17
11
|
for (let i = 0; i < requiredFonts.length; i++) {
|
|
18
12
|
let requiredFont = requiredFonts[i].toLowerCase();
|
|
19
|
-
|
|
20
13
|
for (let font in fonts) {
|
|
21
14
|
if (font.toLowerCase() === requiredFont) {
|
|
22
15
|
return font;
|
|
23
16
|
}
|
|
24
17
|
}
|
|
25
18
|
}
|
|
26
|
-
|
|
27
19
|
return defaultFont;
|
|
28
20
|
};
|
|
21
|
+
|
|
29
22
|
/**
|
|
30
23
|
* Shift the "y" height of the text baseline up or down (superscript or subscript,
|
|
31
24
|
* respectively). The exact shift can / should be changed according to standard
|
|
@@ -35,146 +28,113 @@ const findFont = (fonts, requiredFonts, defaultFont) => {
|
|
|
35
28
|
* @param {object} inline
|
|
36
29
|
* @returns {number}
|
|
37
30
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
40
31
|
const offsetText = (y, inline) => {
|
|
41
32
|
var newY = y;
|
|
42
|
-
|
|
43
33
|
if (inline.sup) {
|
|
44
34
|
newY -= inline.fontSize * 0.75;
|
|
45
35
|
}
|
|
46
|
-
|
|
47
36
|
if (inline.sub) {
|
|
48
37
|
newY += inline.fontSize * 0.35;
|
|
49
38
|
}
|
|
50
|
-
|
|
51
39
|
return newY;
|
|
52
40
|
};
|
|
53
|
-
|
|
54
41
|
class Renderer {
|
|
55
42
|
constructor(pdfDocument, progressCallback) {
|
|
56
43
|
this.pdfDocument = pdfDocument;
|
|
57
44
|
this.progressCallback = progressCallback;
|
|
58
45
|
}
|
|
59
|
-
|
|
60
46
|
renderPages(pages) {
|
|
61
47
|
this.pdfDocument._pdfMakePages = pages; // TODO: Why?
|
|
62
|
-
|
|
63
48
|
this.pdfDocument.addPage();
|
|
64
49
|
let totalItems = 0;
|
|
65
|
-
|
|
66
50
|
if (this.progressCallback) {
|
|
67
51
|
pages.forEach(page => {
|
|
68
52
|
totalItems += page.items.length;
|
|
69
53
|
});
|
|
70
54
|
}
|
|
71
|
-
|
|
72
55
|
let renderedItems = 0;
|
|
73
|
-
|
|
74
56
|
for (let i = 0; i < pages.length; i++) {
|
|
75
57
|
if (i > 0) {
|
|
76
58
|
this._updatePageOrientationInOptions(pages[i]);
|
|
77
|
-
|
|
78
59
|
this.pdfDocument.addPage(this.pdfDocument.options);
|
|
79
60
|
}
|
|
80
|
-
|
|
81
61
|
let page = pages[i];
|
|
82
|
-
|
|
83
62
|
for (let ii = 0, il = page.items.length; ii < il; ii++) {
|
|
84
63
|
let item = page.items[ii];
|
|
85
|
-
|
|
86
64
|
switch (item.type) {
|
|
87
65
|
case 'vector':
|
|
88
66
|
this.renderVector(item.item);
|
|
89
67
|
break;
|
|
90
|
-
|
|
91
68
|
case 'line':
|
|
92
69
|
this.renderLine(item.item, item.item.x, item.item.y);
|
|
93
70
|
break;
|
|
94
|
-
|
|
95
71
|
case 'image':
|
|
96
72
|
this.renderImage(item.item);
|
|
97
73
|
break;
|
|
98
|
-
|
|
99
74
|
case 'svg':
|
|
100
75
|
this.renderSVG(item.item);
|
|
101
76
|
break;
|
|
102
|
-
|
|
103
77
|
case 'attachment':
|
|
104
78
|
this.renderAttachment(item.item);
|
|
105
79
|
break;
|
|
106
|
-
|
|
107
80
|
case 'beginClip':
|
|
108
81
|
this.beginClip(item.item);
|
|
109
82
|
break;
|
|
110
|
-
|
|
111
83
|
case 'endClip':
|
|
112
84
|
this.endClip();
|
|
113
85
|
break;
|
|
114
86
|
}
|
|
115
|
-
|
|
116
87
|
renderedItems++;
|
|
117
|
-
|
|
118
88
|
if (this.progressCallback) {
|
|
119
89
|
this.progressCallback(renderedItems / totalItems);
|
|
120
90
|
}
|
|
121
91
|
}
|
|
122
|
-
|
|
123
92
|
if (page.watermark) {
|
|
124
93
|
this.renderWatermark(page);
|
|
125
94
|
}
|
|
126
95
|
}
|
|
127
96
|
}
|
|
128
|
-
|
|
129
97
|
renderLine(line, x, y) {
|
|
130
98
|
function preparePageNodeRefLine(_pageNodeRef, inline) {
|
|
131
99
|
let newWidth;
|
|
132
100
|
let diffWidth;
|
|
133
101
|
let textInlines = new _TextInlines.default(null);
|
|
134
|
-
|
|
135
102
|
if (_pageNodeRef.positions === undefined) {
|
|
136
103
|
throw new Error('Page reference id not found');
|
|
137
104
|
}
|
|
138
|
-
|
|
139
105
|
let pageNumber = _pageNodeRef.positions[0].pageNumber.toString();
|
|
140
|
-
|
|
141
106
|
inline.text = pageNumber;
|
|
142
107
|
newWidth = textInlines.widthOfText(inline.text, inline);
|
|
143
108
|
diffWidth = inline.width - newWidth;
|
|
144
109
|
inline.width = newWidth;
|
|
145
|
-
|
|
146
110
|
switch (inline.alignment) {
|
|
147
111
|
case 'right':
|
|
148
112
|
inline.x += diffWidth;
|
|
149
113
|
break;
|
|
150
|
-
|
|
151
114
|
case 'center':
|
|
152
115
|
inline.x += diffWidth / 2;
|
|
153
116
|
break;
|
|
154
117
|
}
|
|
155
118
|
}
|
|
156
|
-
|
|
157
119
|
if (line._pageNodeRef) {
|
|
158
120
|
preparePageNodeRefLine(line._pageNodeRef, line.inlines[0]);
|
|
159
121
|
}
|
|
160
|
-
|
|
161
122
|
x = x || 0;
|
|
162
123
|
y = y || 0;
|
|
163
124
|
let lineHeight = line.getHeight();
|
|
164
125
|
let ascenderHeight = line.getAscenderHeight();
|
|
165
126
|
let descent = lineHeight - ascenderHeight;
|
|
166
127
|
const textDecorator = new _TextDecorator.default(this.pdfDocument);
|
|
167
|
-
textDecorator.drawBackground(line, x, y);
|
|
168
|
-
//TOOD: lines without differently styled inlines should be written to pdf as one stream
|
|
128
|
+
textDecorator.drawBackground(line, x, y);
|
|
169
129
|
|
|
130
|
+
//TODO: line.optimizeInlines();
|
|
131
|
+
//TOOD: lines without differently styled inlines should be written to pdf as one stream
|
|
170
132
|
for (let i = 0, l = line.inlines.length; i < l; i++) {
|
|
171
133
|
let inline = line.inlines[i];
|
|
172
134
|
let shiftToBaseline = lineHeight - inline.font.ascender / 1000 * inline.fontSize - descent;
|
|
173
|
-
|
|
174
135
|
if (inline._pageNodeRef) {
|
|
175
136
|
preparePageNodeRefLine(inline._pageNodeRef, inline);
|
|
176
137
|
}
|
|
177
|
-
|
|
178
138
|
let options = {
|
|
179
139
|
lineBreak: false,
|
|
180
140
|
textWidth: inline.width,
|
|
@@ -182,19 +142,15 @@ class Renderer {
|
|
|
182
142
|
wordCount: 1,
|
|
183
143
|
link: inline.link
|
|
184
144
|
};
|
|
185
|
-
|
|
186
145
|
if (inline.linkToDestination) {
|
|
187
146
|
options.goTo = inline.linkToDestination;
|
|
188
147
|
}
|
|
189
|
-
|
|
190
148
|
if (line.id && i === 0) {
|
|
191
149
|
options.destination = line.id;
|
|
192
150
|
}
|
|
193
|
-
|
|
194
151
|
if (inline.fontFeatures) {
|
|
195
152
|
options.features = inline.fontFeatures;
|
|
196
153
|
}
|
|
197
|
-
|
|
198
154
|
let opacity = (0, _variableType.isNumber)(inline.opacity) ? inline.opacity : 1;
|
|
199
155
|
this.pdfDocument.opacity(opacity);
|
|
200
156
|
this.pdfDocument.fill(inline.color || 'black');
|
|
@@ -202,7 +158,6 @@ class Renderer {
|
|
|
202
158
|
this.pdfDocument.fontSize(inline.fontSize);
|
|
203
159
|
let shiftedY = offsetText(y + shiftToBaseline, inline);
|
|
204
160
|
this.pdfDocument.text(inline.text, x + inline.x, shiftedY, options);
|
|
205
|
-
|
|
206
161
|
if (inline.linkToPage) {
|
|
207
162
|
this.pdfDocument.ref({
|
|
208
163
|
Type: 'Action',
|
|
@@ -214,16 +169,14 @@ class Renderer {
|
|
|
214
169
|
Dest: [inline.linkToPage - 1, 'XYZ', null, null, null]
|
|
215
170
|
});
|
|
216
171
|
}
|
|
217
|
-
}
|
|
218
|
-
|
|
172
|
+
}
|
|
219
173
|
|
|
174
|
+
// Decorations won't draw correctly for superscript
|
|
220
175
|
textDecorator.drawDecorations(line, x, y);
|
|
221
176
|
}
|
|
222
|
-
|
|
223
177
|
renderVector(vector) {
|
|
224
178
|
//TODO: pdf optimization (there's no need to write all properties everytime)
|
|
225
179
|
this.pdfDocument.lineWidth(vector.lineWidth || 1);
|
|
226
|
-
|
|
227
180
|
if (vector.dash) {
|
|
228
181
|
this.pdfDocument.dash(vector.dash.length, {
|
|
229
182
|
space: vector.dash.space || vector.dash.length,
|
|
@@ -232,86 +185,66 @@ class Renderer {
|
|
|
232
185
|
} else {
|
|
233
186
|
this.pdfDocument.undash();
|
|
234
187
|
}
|
|
235
|
-
|
|
236
188
|
this.pdfDocument.lineJoin(vector.lineJoin || 'miter');
|
|
237
|
-
this.pdfDocument.lineCap(vector.lineCap || 'butt');
|
|
189
|
+
this.pdfDocument.lineCap(vector.lineCap || 'butt');
|
|
238
190
|
|
|
239
|
-
|
|
191
|
+
//TODO: clipping
|
|
240
192
|
|
|
193
|
+
let gradient = null;
|
|
241
194
|
switch (vector.type) {
|
|
242
195
|
case 'ellipse':
|
|
243
196
|
this.pdfDocument.ellipse(vector.x, vector.y, vector.r1, vector.r2);
|
|
244
|
-
|
|
245
197
|
if (vector.linearGradient) {
|
|
246
198
|
gradient = this.pdfDocument.linearGradient(vector.x - vector.r1, vector.y, vector.x + vector.r1, vector.y);
|
|
247
199
|
}
|
|
248
|
-
|
|
249
200
|
break;
|
|
250
|
-
|
|
251
201
|
case 'rect':
|
|
252
202
|
if (vector.r) {
|
|
253
203
|
this.pdfDocument.roundedRect(vector.x, vector.y, vector.w, vector.h, vector.r);
|
|
254
204
|
} else {
|
|
255
205
|
this.pdfDocument.rect(vector.x, vector.y, vector.w, vector.h);
|
|
256
206
|
}
|
|
257
|
-
|
|
258
207
|
if (vector.linearGradient) {
|
|
259
208
|
gradient = this.pdfDocument.linearGradient(vector.x, vector.y, vector.x + vector.w, vector.y);
|
|
260
209
|
}
|
|
261
|
-
|
|
262
210
|
break;
|
|
263
|
-
|
|
264
211
|
case 'line':
|
|
265
212
|
this.pdfDocument.moveTo(vector.x1, vector.y1);
|
|
266
213
|
this.pdfDocument.lineTo(vector.x2, vector.y2);
|
|
267
214
|
break;
|
|
268
|
-
|
|
269
215
|
case 'polyline':
|
|
270
216
|
if (vector.points.length === 0) {
|
|
271
217
|
break;
|
|
272
218
|
}
|
|
273
|
-
|
|
274
219
|
this.pdfDocument.moveTo(vector.points[0].x, vector.points[0].y);
|
|
275
|
-
|
|
276
220
|
for (let i = 1, l = vector.points.length; i < l; i++) {
|
|
277
221
|
this.pdfDocument.lineTo(vector.points[i].x, vector.points[i].y);
|
|
278
222
|
}
|
|
279
|
-
|
|
280
223
|
if (vector.points.length > 1) {
|
|
281
224
|
let p1 = vector.points[0];
|
|
282
225
|
let pn = vector.points[vector.points.length - 1];
|
|
283
|
-
|
|
284
226
|
if (vector.closePath || p1.x === pn.x && p1.y === pn.y) {
|
|
285
227
|
this.pdfDocument.closePath();
|
|
286
228
|
}
|
|
287
229
|
}
|
|
288
|
-
|
|
289
230
|
break;
|
|
290
|
-
|
|
291
231
|
case 'path':
|
|
292
232
|
this.pdfDocument.path(vector.d);
|
|
293
233
|
break;
|
|
294
234
|
}
|
|
295
|
-
|
|
296
235
|
if (vector.linearGradient && gradient) {
|
|
297
236
|
let step = 1 / (vector.linearGradient.length - 1);
|
|
298
|
-
|
|
299
237
|
for (let i = 0; i < vector.linearGradient.length; i++) {
|
|
300
238
|
gradient.stop(i * step, vector.linearGradient[i]);
|
|
301
239
|
}
|
|
302
|
-
|
|
303
240
|
vector.color = gradient;
|
|
304
241
|
}
|
|
305
|
-
|
|
306
242
|
let patternColor = this.pdfDocument.providePattern(vector.color);
|
|
307
|
-
|
|
308
243
|
if (patternColor !== null) {
|
|
309
244
|
vector.color = patternColor;
|
|
310
245
|
}
|
|
311
|
-
|
|
312
246
|
let fillOpacity = (0, _variableType.isNumber)(vector.fillOpacity) ? vector.fillOpacity : 1;
|
|
313
247
|
let strokeOpacity = (0, _variableType.isNumber)(vector.strokeOpacity) ? vector.strokeOpacity : 1;
|
|
314
|
-
|
|
315
248
|
if (vector.color && vector.lineColor) {
|
|
316
249
|
this.pdfDocument.fillColor(vector.color, fillOpacity);
|
|
317
250
|
this.pdfDocument.strokeColor(vector.lineColor, strokeOpacity);
|
|
@@ -324,11 +257,9 @@ class Renderer {
|
|
|
324
257
|
this.pdfDocument.stroke();
|
|
325
258
|
}
|
|
326
259
|
}
|
|
327
|
-
|
|
328
260
|
renderImage(image) {
|
|
329
261
|
let opacity = (0, _variableType.isNumber)(image.opacity) ? image.opacity : 1;
|
|
330
262
|
this.pdfDocument.opacity(opacity);
|
|
331
|
-
|
|
332
263
|
if (image.cover) {
|
|
333
264
|
const align = image.cover.align || 'center';
|
|
334
265
|
const valign = image.cover.valign || 'center';
|
|
@@ -348,11 +279,9 @@ class Renderer {
|
|
|
348
279
|
height: image._height
|
|
349
280
|
});
|
|
350
281
|
}
|
|
351
|
-
|
|
352
282
|
if (image.link) {
|
|
353
283
|
this.pdfDocument.link(image.x, image.y, image._width, image._height, image.link);
|
|
354
284
|
}
|
|
355
|
-
|
|
356
285
|
if (image.linkToPage) {
|
|
357
286
|
this.pdfDocument.ref({
|
|
358
287
|
Type: 'Action',
|
|
@@ -364,14 +293,13 @@ class Renderer {
|
|
|
364
293
|
Dest: [image.linkToPage - 1, 'XYZ', null, null, null]
|
|
365
294
|
});
|
|
366
295
|
}
|
|
367
|
-
|
|
368
296
|
if (image.linkToDestination) {
|
|
369
297
|
this.pdfDocument.goTo(image.x, image.y, image._width, image._height, image.linkToDestination);
|
|
370
298
|
}
|
|
371
|
-
|
|
372
299
|
if (image.linkToFile) {
|
|
373
300
|
const attachment = this.pdfDocument.provideAttachment(image.linkToFile);
|
|
374
|
-
this.pdfDocument.fileAnnotation(image.x, image.y, image._width, image._height, attachment,
|
|
301
|
+
this.pdfDocument.fileAnnotation(image.x, image.y, image._width, image._height, attachment,
|
|
302
|
+
// add empty rectangle as file annotation appearance with the same size as the rendered image
|
|
375
303
|
{
|
|
376
304
|
AP: {
|
|
377
305
|
N: {
|
|
@@ -384,51 +312,40 @@ class Renderer {
|
|
|
384
312
|
});
|
|
385
313
|
}
|
|
386
314
|
}
|
|
387
|
-
|
|
388
315
|
renderSVG(svg) {
|
|
389
316
|
let options = Object.assign({
|
|
390
317
|
width: svg._width,
|
|
391
318
|
height: svg._height,
|
|
392
319
|
assumePt: true
|
|
393
320
|
}, svg.options);
|
|
394
|
-
|
|
395
321
|
options.fontCallback = (family, bold, italic) => {
|
|
396
322
|
let fontsFamily = family.split(',').map(f => f.trim().replace(/('|")/g, ''));
|
|
397
323
|
let font = findFont(this.pdfDocument.fonts, fontsFamily, svg.font || 'Roboto');
|
|
398
324
|
let fontFile = this.pdfDocument.getFontFile(font, bold, italic);
|
|
399
|
-
|
|
400
325
|
if (fontFile === null) {
|
|
401
326
|
let type = this.pdfDocument.getFontType(bold, italic);
|
|
402
327
|
throw new Error(`Font '${font}' in style '${type}' is not defined in the font section of the document definition.`);
|
|
403
328
|
}
|
|
404
|
-
|
|
405
329
|
return fontFile;
|
|
406
330
|
};
|
|
407
|
-
|
|
408
331
|
(0, _svgToPdfkit.default)(this.pdfDocument, svg.svg, svg.x, svg.y, options);
|
|
409
332
|
}
|
|
410
|
-
|
|
411
333
|
renderAttachment(attachment) {
|
|
412
334
|
const file = this.pdfDocument.provideAttachment(attachment.attachment);
|
|
413
335
|
const options = {};
|
|
414
|
-
|
|
415
336
|
if (attachment.icon) {
|
|
416
337
|
options.Name = attachment.icon;
|
|
417
338
|
}
|
|
418
|
-
|
|
419
339
|
this.pdfDocument.fileAnnotation(attachment.x, attachment.y, attachment._width, attachment._height, file, options);
|
|
420
340
|
}
|
|
421
|
-
|
|
422
341
|
beginClip(rect) {
|
|
423
342
|
this.pdfDocument.save();
|
|
424
343
|
this.pdfDocument.addContent(`${rect.x} ${rect.y} ${rect.width} ${rect.height} re`);
|
|
425
344
|
this.pdfDocument.clip();
|
|
426
345
|
}
|
|
427
|
-
|
|
428
346
|
endClip() {
|
|
429
347
|
this.pdfDocument.restore();
|
|
430
348
|
}
|
|
431
|
-
|
|
432
349
|
renderWatermark(page) {
|
|
433
350
|
let watermark = page.watermark;
|
|
434
351
|
this.pdfDocument.fill(watermark.color);
|
|
@@ -446,18 +363,14 @@ class Renderer {
|
|
|
446
363
|
});
|
|
447
364
|
this.pdfDocument.restore();
|
|
448
365
|
}
|
|
449
|
-
|
|
450
366
|
_updatePageOrientationInOptions(currentPage) {
|
|
451
367
|
let previousPageOrientation = this.pdfDocument.options.size[0] > this.pdfDocument.options.size[1] ? 'landscape' : 'portrait';
|
|
452
|
-
|
|
453
368
|
if (currentPage.pageSize.orientation !== previousPageOrientation) {
|
|
454
369
|
let width = this.pdfDocument.options.size[0];
|
|
455
370
|
let height = this.pdfDocument.options.size[1];
|
|
456
371
|
this.pdfDocument.options.size = [height, width];
|
|
457
372
|
}
|
|
458
373
|
}
|
|
459
|
-
|
|
460
374
|
}
|
|
461
|
-
|
|
462
375
|
var _default = Renderer;
|
|
463
376
|
exports.default = _default;
|