@wirabyte/w-pdfmaker 0.0.0-stage → 1.0.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 (142) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/LICENSE +22 -0
  3. package/NOTICE.md +39 -0
  4. package/README.md +106 -2
  5. package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
  6. package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
  7. package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  8. package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
  9. package/build/fonts/Roboto.js +27 -0
  10. package/build/pdfmake.js +103161 -0
  11. package/build/pdfmake.js.map +1 -0
  12. package/build/pdfmake.min.js +3 -0
  13. package/build/pdfmake.min.js.map +1 -0
  14. package/build/standard-fonts/Courier.js +9 -0
  15. package/build/standard-fonts/Helvetica.js +9 -0
  16. package/build/standard-fonts/Symbol.js +9 -0
  17. package/build/standard-fonts/Times.js +9 -0
  18. package/build/standard-fonts/ZapfDingbats.js +9 -0
  19. package/build/vfs_fonts.js +6 -0
  20. package/build-vfs.js +44 -0
  21. package/fonts/Roboto/Roboto-Italic.ttf +0 -0
  22. package/fonts/Roboto/Roboto-Medium.ttf +0 -0
  23. package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  24. package/fonts/Roboto/Roboto-Regular.ttf +0 -0
  25. package/fonts/Roboto.js +8 -0
  26. package/fonts/THSarabunIT9/LICENSE.txt +7 -0
  27. package/fonts/THSarabunIT9/THSarabunIT9-Bold.ttf +0 -0
  28. package/fonts/THSarabunIT9/THSarabunIT9-BoldItalic.ttf +0 -0
  29. package/fonts/THSarabunIT9/THSarabunIT9-Italic.ttf +0 -0
  30. package/fonts/THSarabunIT9/THSarabunIT9.ttf +0 -0
  31. package/fonts/THSarabunNew/COPYING.GPLv2 +339 -0
  32. package/fonts/THSarabunNew/LICENSE.txt +13 -0
  33. package/fonts/THSarabunNew/THSarabunNew-Bold.ttf +0 -0
  34. package/fonts/THSarabunNew/THSarabunNew-BoldItalic.ttf +0 -0
  35. package/fonts/THSarabunNew/THSarabunNew-Italic.ttf +0 -0
  36. package/fonts/THSarabunNew/THSarabunNew.ttf +0 -0
  37. package/index.d.ts +167 -0
  38. package/interfaces.d.ts +2779 -0
  39. package/js/DocMeasure.js +695 -0
  40. package/js/DocPreprocessor.js +263 -0
  41. package/js/DocumentContext.js +563 -0
  42. package/js/ElementWriter.js +488 -0
  43. package/js/LayoutBuilder.js +1525 -0
  44. package/js/Line.js +105 -0
  45. package/js/OutputDocument.js +73 -0
  46. package/js/OutputDocumentServer.js +34 -0
  47. package/js/PDFDocument.js +322 -0
  48. package/js/PageElementWriter.js +302 -0
  49. package/js/PageSize.js +74 -0
  50. package/js/Printer.js +275 -0
  51. package/js/Renderer.js +439 -0
  52. package/js/SVGMeasure.js +94 -0
  53. package/js/StyleContextStack.js +191 -0
  54. package/js/TableProcessor.js +658 -0
  55. package/js/TextBreaker.js +164 -0
  56. package/js/TextDecorator.js +164 -0
  57. package/js/TextInlines.js +212 -0
  58. package/js/URLResolver.js +122 -0
  59. package/js/base.js +125 -0
  60. package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
  61. package/js/browser-extensions/fonts/Roboto.js +38 -0
  62. package/js/browser-extensions/fonts/thai.js +28 -0
  63. package/js/browser-extensions/index.js +88 -0
  64. package/js/browser-extensions/pageUrls.js +79 -0
  65. package/js/browser-extensions/pdfMake.js +3 -0
  66. package/js/browser-extensions/standard-fonts/Courier.js +20 -0
  67. package/js/browser-extensions/standard-fonts/Helvetica.js +20 -0
  68. package/js/browser-extensions/standard-fonts/Symbol.js +17 -0
  69. package/js/browser-extensions/standard-fonts/Times.js +20 -0
  70. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
  71. package/js/browser-extensions/virtual-fs-cjs.js +3 -0
  72. package/js/columnCalculator.js +159 -0
  73. package/js/helpers/node.js +122 -0
  74. package/js/helpers/tools.js +46 -0
  75. package/js/helpers/variableType.js +59 -0
  76. package/js/imageFallback.js +23 -0
  77. package/js/index.js +45 -0
  78. package/js/mergeStamp.js +194 -0
  79. package/js/pageNumbering.js +48 -0
  80. package/js/qrEnc.js +721 -0
  81. package/js/standardPageSizes.js +56 -0
  82. package/js/tableLayouts.js +98 -0
  83. package/js/thai.js +201 -0
  84. package/js/virtual-fs.js +82 -0
  85. package/package.json +93 -4
  86. package/skills/w-pdfmaker/SKILL.md +171 -0
  87. package/skills/w-pdfmaker/references/browser.md +36 -0
  88. package/skills/w-pdfmaker/references/document-definition.md +241 -0
  89. package/skills/w-pdfmaker/references/images.md +90 -0
  90. package/skills/w-pdfmaker/references/merge-stamp.md +94 -0
  91. package/skills/w-pdfmaker/references/thai.md +67 -0
  92. package/src/DocMeasure.js +780 -0
  93. package/src/DocPreprocessor.js +293 -0
  94. package/src/DocumentContext.js +605 -0
  95. package/src/ElementWriter.js +550 -0
  96. package/src/LayoutBuilder.js +1689 -0
  97. package/src/Line.js +114 -0
  98. package/src/OutputDocument.js +75 -0
  99. package/src/OutputDocumentServer.js +38 -0
  100. package/src/PDFDocument.js +352 -0
  101. package/src/PageElementWriter.js +343 -0
  102. package/src/PageSize.js +53 -0
  103. package/src/Printer.js +289 -0
  104. package/src/Renderer.js +468 -0
  105. package/src/SVGMeasure.js +109 -0
  106. package/src/StyleContextStack.js +208 -0
  107. package/src/TableProcessor.js +719 -0
  108. package/src/TextBreaker.js +166 -0
  109. package/src/TextDecorator.js +191 -0
  110. package/src/TextInlines.js +229 -0
  111. package/src/URLResolver.js +122 -0
  112. package/src/base.js +143 -0
  113. package/src/browser-extensions/OutputDocumentBrowser.js +80 -0
  114. package/src/browser-extensions/fonts/Roboto.js +27 -0
  115. package/src/browser-extensions/fonts/thai.js +23 -0
  116. package/src/browser-extensions/index.js +91 -0
  117. package/src/browser-extensions/pageUrls.js +67 -0
  118. package/src/browser-extensions/pdfMake.js +1 -0
  119. package/src/browser-extensions/standard-fonts/Courier.js +25 -0
  120. package/src/browser-extensions/standard-fonts/Helvetica.js +25 -0
  121. package/src/browser-extensions/standard-fonts/Symbol.js +17 -0
  122. package/src/browser-extensions/standard-fonts/Times.js +25 -0
  123. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
  124. package/src/browser-extensions/virtual-fs-cjs.js +1 -0
  125. package/src/columnCalculator.js +160 -0
  126. package/src/helpers/node.js +134 -0
  127. package/src/helpers/tools.js +44 -0
  128. package/src/helpers/variableType.js +50 -0
  129. package/src/imageFallback.js +18 -0
  130. package/src/index.js +45 -0
  131. package/src/mergeStamp.js +132 -0
  132. package/src/pageNumbering.js +45 -0
  133. package/src/qrEnc.js +796 -0
  134. package/src/standardPageSizes.js +52 -0
  135. package/src/tableLayouts.js +100 -0
  136. package/src/thai.js +170 -0
  137. package/src/virtual-fs.js +92 -0
  138. package/standard-fonts/Courier.js +8 -0
  139. package/standard-fonts/Helvetica.js +8 -0
  140. package/standard-fonts/Symbol.js +5 -0
  141. package/standard-fonts/Times.js +8 -0
  142. package/standard-fonts/ZapfDingbats.js +5 -0
@@ -0,0 +1,468 @@
1
+ import TextDecorator from './TextDecorator';
2
+ import TextInlines from './TextInlines';
3
+ import { isNumber, isString } from './helpers/variableType';
4
+ import SVGtoPDF from '@pdfmake/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
+ let 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
+ this.outlineMap = [];
45
+ }
46
+
47
+ renderPages(pages) {
48
+ this.pdfDocument._pdfMakePages = pages; // TODO: Why?
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
+ this.pdfDocument.addPage({ size: [pages[i].pageSize.width, pages[i].pageSize.height] });
61
+
62
+ let page = pages[i];
63
+ for (let ii = 0, il = page.items.length; ii < il; ii++) {
64
+ let item = page.items[ii];
65
+ switch (item.type) {
66
+ case 'vector':
67
+ this.renderVector(item.item);
68
+ break;
69
+ case 'line':
70
+ this.renderLine(item.item, item.item.x, item.item.y);
71
+ break;
72
+ case 'image':
73
+ this.renderImage(item.item);
74
+ break;
75
+ case 'svg':
76
+ this.renderSVG(item.item);
77
+ break;
78
+ case 'attachment':
79
+ this.renderAttachment(item.item);
80
+ break;
81
+ case 'beginClip':
82
+ this.beginClip(item.item);
83
+ break;
84
+ case 'endClip':
85
+ this.endClip();
86
+ break;
87
+ case 'beginVerticalAlignment':
88
+ this.beginVerticalAlignment(item.item);
89
+ break;
90
+ case 'endVerticalAlignment':
91
+ this.endVerticalAlignment(item.item);
92
+ break;
93
+ }
94
+ renderedItems++;
95
+ if (this.progressCallback) {
96
+ this.progressCallback(renderedItems / totalItems);
97
+ }
98
+ }
99
+ if (page.watermark) {
100
+ this.renderWatermark(page);
101
+ }
102
+ }
103
+ }
104
+
105
+ renderLine(line, x, y) {
106
+ const pages = this.pdfDocument._pdfMakePages;
107
+ function preparePageNodeRefLine(_pageNodeRef, inline) {
108
+ let newWidth;
109
+ let diffWidth;
110
+ let textInlines = new TextInlines(null);
111
+
112
+ if (_pageNodeRef.positions === undefined) {
113
+ throw new Error('Page reference id not found');
114
+ }
115
+
116
+ let pageNumber = _pageNodeRef.positions[0].pageNumber;
117
+ pageNumber = pages?.[pageNumber - 1]?.pageLabel ?? pageNumber.toString(); // w-pdfmaker: section page numbering
118
+
119
+ inline.text = pageNumber;
120
+ newWidth = textInlines.widthOfText(inline.text, inline);
121
+ diffWidth = inline.width - newWidth;
122
+ inline.width = newWidth;
123
+
124
+ switch (inline.alignment) {
125
+ case 'right':
126
+ inline.x += diffWidth;
127
+ break;
128
+ case 'center':
129
+ inline.x += diffWidth / 2;
130
+ break;
131
+ }
132
+ }
133
+
134
+ if (line._outline) {
135
+ let parentOutline = this.pdfDocument.outline;
136
+ if (line._outline.parentId && this.outlineMap[line._outline.parentId]) {
137
+ parentOutline = this.outlineMap[line._outline.parentId];
138
+ }
139
+
140
+ let outline = parentOutline.addItem(line._outline.text, { expanded: line._outline.expanded });
141
+ if (line._outline.id) {
142
+ this.outlineMap[line._outline.id] = outline;
143
+ }
144
+ }
145
+
146
+ if (line._pageNodeRef) {
147
+ preparePageNodeRefLine(line._pageNodeRef, line.inlines[0]);
148
+ }
149
+
150
+ x = x || 0;
151
+ y = y || 0;
152
+
153
+ let lineHeight = line.getHeight();
154
+ let ascenderHeight = line.getAscenderHeight();
155
+ let descent = lineHeight - ascenderHeight;
156
+
157
+ const textDecorator = new TextDecorator(this.pdfDocument);
158
+
159
+ textDecorator.drawBackground(line, x, y);
160
+
161
+ //TODO: line.optimizeInlines();
162
+ //TODO: lines without differently styled inlines should be written to pdf as one stream
163
+ for (let i = 0, l = line.inlines.length; i < l; i++) {
164
+ let inline = line.inlines[i];
165
+ let shiftToBaseline = lineHeight - ((inline.font.ascender / 1000) * inline.fontSize) - descent;
166
+
167
+ if (inline._pageNodeRef) {
168
+ preparePageNodeRefLine(inline._pageNodeRef, inline);
169
+ }
170
+
171
+ let options = {
172
+ lineBreak: false,
173
+ textWidth: inline.width,
174
+ characterSpacing: inline.characterSpacing,
175
+ wordCount: 1,
176
+ link: inline.link
177
+ };
178
+
179
+ if (inline.linkToDestination) {
180
+ options.goTo = inline.linkToDestination;
181
+ }
182
+
183
+ if (line.id && i === 0) {
184
+ options.destination = line.id;
185
+ }
186
+
187
+ if (inline.fontFeatures) {
188
+ options.features = inline.fontFeatures;
189
+ }
190
+
191
+ let opacity = isNumber(inline.opacity) ? inline.opacity : 1;
192
+ this.pdfDocument.opacity(opacity);
193
+ this.pdfDocument.fill(this.pdfDocument.resolveColor(inline.color, 'black'));
194
+
195
+ this.pdfDocument._font = inline.font;
196
+ this.pdfDocument.fontSize(inline.fontSize);
197
+
198
+ let shiftedY = offsetText(y + shiftToBaseline, inline);
199
+ this.pdfDocument.text(inline.text, x + inline.x, shiftedY, options);
200
+
201
+ if (inline.linkToPage) {
202
+ this.pdfDocument.ref({ Type: 'Action', S: 'GoTo', D: [inline.linkToPage, 0, 0] }).end();
203
+ this.pdfDocument.annotate(x + inline.x, shiftedY, inline.width, inline.height, { Subtype: 'Link', Dest: [inline.linkToPage - 1, 'XYZ', null, null, null] });
204
+ }
205
+ }
206
+
207
+ // TODO: Decorations won't draw correctly for superscript
208
+ textDecorator.drawDecorations(line, x, y);
209
+ }
210
+
211
+ renderVector(vector) {
212
+ //TODO: pdf optimization (there's no need to write all properties everytime)
213
+ this.pdfDocument.lineWidth(vector.lineWidth || 1);
214
+ if (vector.dash) {
215
+ this.pdfDocument.dash(vector.dash.length, { space: vector.dash.space || vector.dash.length, phase: vector.dash.phase || 0 });
216
+ } else {
217
+ this.pdfDocument.undash();
218
+ }
219
+ this.pdfDocument.lineJoin(vector.lineJoin || 'miter');
220
+ this.pdfDocument.lineCap(vector.lineCap || 'butt');
221
+
222
+ //TODO: clipping
223
+
224
+ let gradient = null;
225
+
226
+ switch (vector.type) {
227
+ case 'ellipse':
228
+ this.pdfDocument.ellipse(vector.x, vector.y, vector.r1, vector.r2);
229
+
230
+ if (vector.linearGradient) {
231
+ gradient = this.pdfDocument.linearGradient(vector.x - vector.r1, vector.y, vector.x + vector.r1, vector.y);
232
+ }
233
+ break;
234
+ case 'rect':
235
+ if (vector.r) {
236
+ this.pdfDocument.roundedRect(vector.x, vector.y, vector.w, vector.h, vector.r);
237
+ } else {
238
+ this.pdfDocument.rect(vector.x, vector.y, vector.w, vector.h);
239
+ }
240
+
241
+ if (vector.linearGradient) {
242
+ gradient = this.pdfDocument.linearGradient(vector.x, vector.y, vector.x + vector.w, vector.y);
243
+ }
244
+ break;
245
+ case 'line':
246
+ this.pdfDocument.moveTo(vector.x1, vector.y1);
247
+ this.pdfDocument.lineTo(vector.x2, vector.y2);
248
+ break;
249
+ case 'polyline':
250
+ if (vector.points.length === 0) {
251
+ break;
252
+ }
253
+
254
+ this.pdfDocument.moveTo(vector.points[0].x, vector.points[0].y);
255
+ for (let i = 1, l = vector.points.length; i < l; i++) {
256
+ this.pdfDocument.lineTo(vector.points[i].x, vector.points[i].y);
257
+ }
258
+
259
+ if (vector.points.length > 1) {
260
+ let p1 = vector.points[0];
261
+ let pn = vector.points[vector.points.length - 1];
262
+
263
+ if (vector.closePath || p1.x === pn.x && p1.y === pn.y) {
264
+ this.pdfDocument.closePath();
265
+ }
266
+ }
267
+ break;
268
+ case 'path':
269
+ this.pdfDocument.path(vector.d);
270
+ break;
271
+ }
272
+
273
+ if (vector.linearGradient && gradient) {
274
+ let step = 1 / (vector.linearGradient.length - 1);
275
+
276
+ for (let i = 0; i < vector.linearGradient.length; i++) {
277
+ gradient.stop(i * step, vector.linearGradient[i]);
278
+ }
279
+
280
+ vector.color = gradient;
281
+ }
282
+
283
+ let patternColor = this.pdfDocument.providePattern(vector.color);
284
+ if (patternColor !== null) {
285
+ vector.color = patternColor;
286
+ }
287
+
288
+ let fillOpacity = isNumber(vector.fillOpacity) ? vector.fillOpacity : 1;
289
+ let strokeOpacity = isNumber(vector.strokeOpacity) ? vector.strokeOpacity : 1;
290
+
291
+ if (vector.color && vector.lineColor) {
292
+ this.pdfDocument.fillColor(this.pdfDocument.resolveColor(vector.color, 'black'), fillOpacity);
293
+ this.pdfDocument.strokeColor(this.pdfDocument.resolveColor(vector.lineColor, 'black'), strokeOpacity);
294
+ this.pdfDocument.fillAndStroke();
295
+ } else if (vector.color) {
296
+ this.pdfDocument.fillColor(this.pdfDocument.resolveColor(vector.color, 'black'), fillOpacity);
297
+ this.pdfDocument.fill();
298
+ } else {
299
+ this.pdfDocument.strokeColor(this.pdfDocument.resolveColor(vector.lineColor, 'black'), strokeOpacity);
300
+ this.pdfDocument.stroke();
301
+ }
302
+ }
303
+
304
+ renderImage(image) {
305
+ const source = this.pdfDocument.provideImage(image.image);
306
+ let opacity = isNumber(image.opacity) ? image.opacity : 1;
307
+ this.pdfDocument.opacity(opacity);
308
+ if (image.cover) {
309
+ const align = image.cover.align || 'center';
310
+ const valign = image.cover.valign || 'center';
311
+ const width = image.cover.width ? image.cover.width : image.width;
312
+ const height = image.cover.height ? image.cover.height : image.height;
313
+ this.pdfDocument.save();
314
+ this.pdfDocument.rect(image.x, image.y, width, height).clip();
315
+ this.pdfDocument.image(source, image.x, image.y, { cover: [width, height], align: align, valign: valign });
316
+ this.pdfDocument.restore();
317
+ } else {
318
+ this.pdfDocument.image(source, image.x, image.y, { width: image._width, height: image._height });
319
+ }
320
+ if (image.link) {
321
+ this.pdfDocument.link(image.x, image.y, image._width, image._height, image.link);
322
+ }
323
+ if (image.linkToPage) {
324
+ this.pdfDocument.ref({ Type: 'Action', S: 'GoTo', D: [image.linkToPage, 0, 0] }).end();
325
+ this.pdfDocument.annotate(image.x, image.y, image._width, image._height, { Subtype: 'Link', Dest: [image.linkToPage - 1, 'XYZ', null, null, null] });
326
+ }
327
+ if (image.linkToDestination) {
328
+ this.pdfDocument.goTo(image.x, image.y, image._width, image._height, image.linkToDestination);
329
+ }
330
+ if (image.linkToFile) {
331
+ const attachment = this.pdfDocument.provideAttachment(image.linkToFile);
332
+ this.pdfDocument.fileAnnotation(
333
+ image.x,
334
+ image.y,
335
+ image._width,
336
+ image._height,
337
+ attachment,
338
+ // add empty rectangle as file annotation appearance with the same size as the rendered image
339
+ {
340
+ AP: {
341
+ N: {
342
+ Type: 'XObject',
343
+ Subtype: 'Form',
344
+ FormType: 1,
345
+ BBox: [image.x, image.y, image._width, image._height]
346
+ }
347
+ },
348
+ }
349
+ );
350
+ }
351
+ }
352
+
353
+ renderSVG(svg) {
354
+ let options = {
355
+ width: svg._width,
356
+ height: svg._height,
357
+ assumePt: true,
358
+ useCSS: !isString(svg.svg),
359
+ ...svg.options
360
+ };
361
+ options.fontCallback = (family, bold, italic) => {
362
+ let fontsFamily = family.split(',').map(f => f.trim().replace(/('|")/g, ''));
363
+ let font = findFont(this.pdfDocument.fonts, fontsFamily, svg._font || svg.font || 'Roboto');
364
+
365
+ let loadedFont = this.pdfDocument.provideFont(font, bold, italic);
366
+ let alias = `pdfmake:${JSON.stringify([font, this.pdfDocument.getFontType(bold, italic)])}`;
367
+ this.pdfDocument._fontFamilies[alias] = loadedFont;
368
+ return alias;
369
+ };
370
+ options.imageCallback = link => {
371
+ let src = link.replace(/\s+/g, '');
372
+ this.pdfDocument.provideImage(src);
373
+
374
+ return src;
375
+ };
376
+
377
+ SVGtoPDF(this.pdfDocument, svg.svg, svg.x, svg.y, options);
378
+
379
+ if (svg.link) {
380
+ this.pdfDocument.link(svg.x, svg.y, svg._width, svg._height, svg.link);
381
+ }
382
+ if (svg.linkToPage) {
383
+ this.pdfDocument.ref({ Type: 'Action', S: 'GoTo', D: [svg.linkToPage, 0, 0] }).end();
384
+ this.pdfDocument.annotate(svg.x, svg.y, svg._width, svg._height, { Subtype: 'Link', Dest: [svg.linkToPage - 1, 'XYZ', null, null, null] });
385
+ }
386
+ if (svg.linkToDestination) {
387
+ this.pdfDocument.goTo(svg.x, svg.y, svg._width, svg._height, svg.linkToDestination);
388
+ }
389
+ }
390
+
391
+ renderAttachment(attachment) {
392
+ const file = this.pdfDocument.provideAttachment(attachment.attachment);
393
+
394
+ const options = {};
395
+ if (attachment.icon) {
396
+ options.Name = attachment.icon;
397
+ }
398
+
399
+ this.pdfDocument.fileAnnotation(attachment.x, attachment.y, attachment._width, attachment._height, file, options);
400
+ }
401
+
402
+ beginClip(rect) {
403
+ this.pdfDocument.save();
404
+ this.pdfDocument.addContent(`${rect.x} ${rect.y} ${rect.width} ${rect.height} re`);
405
+ this.pdfDocument.clip();
406
+ }
407
+
408
+ endClip() {
409
+ this.pdfDocument.restore();
410
+ }
411
+
412
+ getVerticalAlignmentOffset(item) {
413
+ if (item.isCellContentMultiPage || (item.verticalAlignment !== 'middle' && item.verticalAlignment !== 'bottom')) {
414
+ return null;
415
+ }
416
+
417
+ const nodeHeight = item.getNodeHeight();
418
+ const viewHeight = item.getViewHeight();
419
+
420
+ if (!Number.isFinite(nodeHeight) || !Number.isFinite(viewHeight) || nodeHeight < 0 || viewHeight < nodeHeight) {
421
+ return null;
422
+ }
423
+
424
+ if (item.verticalAlignment === 'middle') {
425
+ return (viewHeight - nodeHeight) / 2;
426
+ }
427
+
428
+ // bottom
429
+ return (viewHeight - nodeHeight);
430
+ }
431
+
432
+ beginVerticalAlignment(item) {
433
+ const offset = this.getVerticalAlignmentOffset(item);
434
+ if (offset !== null) {
435
+ this.pdfDocument.save();
436
+ this.pdfDocument.translate(0, offset);
437
+ }
438
+ }
439
+
440
+ endVerticalAlignment(item) {
441
+ if (this.getVerticalAlignmentOffset(item) !== null) {
442
+ this.pdfDocument.restore();
443
+ }
444
+ }
445
+
446
+ renderWatermark(page) {
447
+ let watermark = page.watermark;
448
+
449
+ this.pdfDocument.fill(this.pdfDocument.resolveColor(watermark.color, 'black'));
450
+ this.pdfDocument.opacity(watermark.opacity);
451
+
452
+ this.pdfDocument.save();
453
+
454
+ this.pdfDocument.rotate(watermark.angle, { origin: [this.pdfDocument.page.width / 2, this.pdfDocument.page.height / 2] });
455
+
456
+ let x = this.pdfDocument.page.width / 2 - watermark._size.size.width / 2;
457
+ let y = this.pdfDocument.page.height / 2 - watermark._size.size.height / 2;
458
+
459
+ this.pdfDocument._font = watermark.font;
460
+ this.pdfDocument.fontSize(watermark.fontSize);
461
+ this.pdfDocument.text(watermark.text, x, y, { lineBreak: false });
462
+
463
+ this.pdfDocument.restore();
464
+ }
465
+
466
+ }
467
+
468
+ export default Renderer;
@@ -0,0 +1,109 @@
1
+ import { XmlDocument } from "xmldoc";
2
+ import { isString } from './helpers/variableType';
3
+
4
+ /**
5
+ * Strip unit postfix, parse number, but return undefined instead of NaN for bad input
6
+ *
7
+ * @param {string} textVal
8
+ * @returns {?number}
9
+ */
10
+ const stripUnits = textVal => {
11
+ let n = parseFloat(textVal);
12
+ if (typeof n !== 'number' || isNaN(n)) {
13
+ return undefined;
14
+ }
15
+ return n;
16
+ };
17
+
18
+ /**
19
+ * Make sure it's valid XML and the root tag is <svg/>, returns xmldoc DOM
20
+ *
21
+ * @param {string} svgString
22
+ * @returns {object}
23
+ */
24
+ const parseSVG = (svgString) => {
25
+ let doc;
26
+
27
+ try {
28
+ doc = new XmlDocument(svgString);
29
+ } catch (error) {
30
+ throw new Error('Invalid svg document (' + error + ')', { cause: error });
31
+ }
32
+
33
+ if (doc.name !== "svg") {
34
+ throw new Error('Invalid svg document (expected <svg>)');
35
+ }
36
+
37
+ return doc;
38
+ };
39
+
40
+ class SVGMeasure {
41
+ constructor() {
42
+
43
+ }
44
+
45
+ measureSVG(svg) {
46
+ let width, height, viewBox;
47
+
48
+ if (isString(svg)) {
49
+ let doc = parseSVG(svg);
50
+
51
+ width = doc.attr.width;
52
+ height = doc.attr.height;
53
+ viewBox = doc.attr.viewBox;
54
+ } else if (typeof SVGElement !== 'undefined' && svg instanceof SVGElement && typeof getComputedStyle === 'function') {
55
+ width = svg.getAttribute("width");
56
+ height = svg.getAttribute("height");
57
+ viewBox = svg.getAttribute("viewBox");
58
+ } else {
59
+ throw new Error('Invalid SVG document');
60
+ }
61
+
62
+ let docWidth = stripUnits(width);
63
+ let docHeight = stripUnits(height);
64
+
65
+ if ((docWidth === undefined || docHeight === undefined) && typeof viewBox === 'string') {
66
+ let viewBoxParts = viewBox.split(/[,\s]+/);
67
+ if (viewBoxParts.length !== 4) {
68
+ throw new Error("Unexpected svg viewBox format, should have 4 entries but found: '" + viewBox + "'");
69
+ }
70
+ if (docWidth === undefined) {
71
+ docWidth = stripUnits(viewBoxParts[2]);
72
+ }
73
+ if (docHeight === undefined) {
74
+ docHeight = stripUnits(viewBoxParts[3]);
75
+ }
76
+ }
77
+
78
+ return {
79
+ width: docWidth,
80
+ height: docHeight
81
+ };
82
+ }
83
+
84
+ writeDimensions(svg, dimensions) {
85
+ if (isString(svg)) {
86
+ let doc = parseSVG(svg);
87
+
88
+ if (typeof doc.attr.viewBox !== 'string') {
89
+ doc.attr.viewBox = `0 0 ${stripUnits(doc.attr.width)} ${stripUnits(doc.attr.height)}`;
90
+ }
91
+
92
+ doc.attr.width = "" + dimensions.width;
93
+ doc.attr.height = "" + dimensions.height;
94
+
95
+ return doc.toString();
96
+ }
97
+
98
+ if (!svg.hasAttribute('viewBox')) {
99
+ svg.setAttribute('viewBox', `0 0 ${stripUnits(svg.getAttribute('width'))} ${stripUnits(svg.getAttribute('height'))}`);
100
+ }
101
+
102
+ svg.setAttribute('width', "" + dimensions.width);
103
+ svg.setAttribute('height', "" + dimensions.height);
104
+
105
+ return svg;
106
+ }
107
+ }
108
+
109
+ export default SVGMeasure;