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