pdfmake 0.2.4 → 0.3.0-beta.2

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