pdfmake 0.3.0-beta.6 → 0.3.0-beta.8

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