pdfmake 0.2.4 → 0.3.0-beta.1

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