pdfmake-acroforms 0.3.4

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