@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,293 @@
1
+ import { isString, isNumber, isValue, isEmptyObject } from './helpers/variableType';
2
+ import { stringifyNode } from './helpers/node';
3
+ import { EMPTY_SVG } from './imageFallback';
4
+
5
+ const convertValueToString = value => {
6
+ if (isString(value)) {
7
+ return value.replace(/\t/g, ' '); // expand tab as spaces
8
+ } else if (isNumber(value) || typeof value === 'boolean') {
9
+ return value.toString();
10
+ } else if (!isValue(value) || isEmptyObject(value)) {
11
+ return '';
12
+ }
13
+
14
+ // TODO: throw exception ?
15
+
16
+ return value;
17
+ };
18
+
19
+ class DocPreprocessor {
20
+ preprocessDocument(docStructure) {
21
+ this.parentNode = null;
22
+ this.tocs = [];
23
+ this.nodeReferences = [];
24
+ return this.preprocessNode(docStructure, true);
25
+ }
26
+
27
+ preprocessBlock(node) {
28
+ this.parentNode = null;
29
+ this.tocs = [];
30
+ this.nodeReferences = [];
31
+ return this.preprocessNode(node);
32
+ }
33
+
34
+ preprocessNode(node, isSectionAllowed = false) {
35
+ // expand shortcuts and casting values
36
+ if (Array.isArray(node)) {
37
+ node = { stack: node };
38
+ } else if (isString(node) || isNumber(node) || typeof node === 'boolean' || !isValue(node) || isEmptyObject(node)) { // text node defined as value
39
+ node = { text: convertValueToString(node) };
40
+ } else if ('text' in node) { // cast value in text property
41
+ node.text = convertValueToString(node.text);
42
+ }
43
+
44
+ if (node.section) {
45
+ if (!isSectionAllowed) {
46
+ throw new Error(`Incorrect document structure, section node is only allowed at the root level of document structure: ${stringifyNode(node)}`);
47
+ }
48
+
49
+ return this.preprocessSection(node);
50
+ } else if (node.columns) {
51
+ return this.preprocessColumns(node);
52
+ } else if (node.stack) {
53
+ return this.preprocessVerticalContainer(node, isSectionAllowed);
54
+ } else if (node.ul) {
55
+ return this.preprocessList(node);
56
+ } else if (node.ol) {
57
+ return this.preprocessList(node);
58
+ } else if (node.table) {
59
+ return this.preprocessTable(node);
60
+ } else if (node.text !== undefined) {
61
+ return this.preprocessText(node);
62
+ } else if (node.toc) {
63
+ return this.preprocessToc(node);
64
+ } else if (node.fallback && !node.image && !node.svg && ('image' in node || 'svg' in node)) {
65
+ // w-pdfmaker: an image without a source (e.g. no signature) is drawn as nothing with `fallback`
66
+ node.svg = EMPTY_SVG;
67
+ delete node.image;
68
+ return this.preprocessSVG(node);
69
+ } else if (node.image) {
70
+ return this.preprocessImage(node);
71
+ } else if (node.svg) {
72
+ return this.preprocessSVG(node);
73
+ } else if (node.canvas) {
74
+ return this.preprocessCanvas(node);
75
+ } else if (node.qr) {
76
+ return this.preprocessQr(node);
77
+ } else if (node.attachment) {
78
+ return this.preprocessAttachment(node);
79
+ } else if (node.pageReference || node.textReference) {
80
+ return this.preprocessText(node);
81
+ } else {
82
+ throw new Error(`Unrecognized document structure: ${stringifyNode(node)}`);
83
+ }
84
+ }
85
+
86
+ preprocessSection(node) {
87
+ node.section = this.preprocessNode(node.section);
88
+
89
+ return node;
90
+ }
91
+
92
+ preprocessColumns(node) {
93
+ let columns = node.columns;
94
+
95
+ for (let i = 0, l = columns.length; i < l; i++) {
96
+ columns[i] = this.preprocessNode(columns[i]);
97
+ }
98
+
99
+ return node;
100
+ }
101
+
102
+ preprocessVerticalContainer(node, isSectionAllowed) {
103
+ let items = node.stack;
104
+
105
+ for (let i = 0, l = items.length; i < l; i++) {
106
+ items[i] = this.preprocessNode(items[i], isSectionAllowed);
107
+ }
108
+
109
+ return node;
110
+ }
111
+
112
+ preprocessList(node) {
113
+ let items = node.ul || node.ol;
114
+
115
+ for (let i = 0, l = items.length; i < l; i++) {
116
+ items[i] = this.preprocessNode(items[i]);
117
+ }
118
+
119
+ return node;
120
+ }
121
+
122
+ preprocessTable(node) {
123
+ let col;
124
+ let row;
125
+ let cols;
126
+ let rows;
127
+
128
+ for (col = 0, cols = node.table.body[0].length; col < cols; col++) {
129
+ for (row = 0, rows = node.table.body.length; row < rows; row++) {
130
+ let rowData = node.table.body[row];
131
+ let data = rowData[col];
132
+ if (data !== undefined) {
133
+ if (data === null) { // transform to object
134
+ data = '';
135
+ }
136
+ if (!data._span) {
137
+ rowData[col] = this.preprocessNode(data);
138
+ }
139
+ }
140
+ }
141
+ }
142
+
143
+ return node;
144
+ }
145
+
146
+ preprocessText(node) {
147
+ if (node.tocItem) {
148
+ if (!Array.isArray(node.tocItem)) {
149
+ node.tocItem = [node.tocItem];
150
+ }
151
+
152
+ for (let i = 0, l = node.tocItem.length; i < l; i++) {
153
+ if (!isString(node.tocItem[i])) {
154
+ node.tocItem[i] = '_default_';
155
+ }
156
+
157
+ let tocItemId = node.tocItem[i];
158
+
159
+ if (!this.tocs[tocItemId]) {
160
+ this.tocs[tocItemId] = { toc: { _items: [], _pseudo: true } };
161
+ }
162
+
163
+ if (!node.id) {
164
+ node.id = `toc-${tocItemId}-${this.tocs[tocItemId].toc._items.length}`;
165
+ }
166
+
167
+ let tocItemRef = {
168
+ _nodeRef: this._getNodeForNodeRef(node),
169
+ _textNodeRef: node
170
+ };
171
+ this.tocs[tocItemId].toc._items.push(tocItemRef);
172
+ }
173
+ }
174
+
175
+ if (node.id) {
176
+ if (this.nodeReferences[node.id]) {
177
+ if (!this.nodeReferences[node.id]._pseudo) {
178
+ throw new Error(`Node id '${node.id}' already exists`);
179
+ }
180
+
181
+ this.nodeReferences[node.id]._nodeRef = this._getNodeForNodeRef(node);
182
+ this.nodeReferences[node.id]._textNodeRef = node;
183
+ this.nodeReferences[node.id]._pseudo = false;
184
+ } else {
185
+ this.nodeReferences[node.id] = {
186
+ _nodeRef: this._getNodeForNodeRef(node),
187
+ _textNodeRef: node
188
+ };
189
+ }
190
+ }
191
+
192
+ if (node.pageReference) {
193
+ if (!this.nodeReferences[node.pageReference]) {
194
+ this.nodeReferences[node.pageReference] = {
195
+ _nodeRef: {},
196
+ _textNodeRef: {},
197
+ _pseudo: true
198
+ };
199
+ }
200
+ node.text = '00000';
201
+ node.linkToDestination = node.pageReference;
202
+ node._pageRef = this.nodeReferences[node.pageReference];
203
+ }
204
+
205
+ if (node.textReference) {
206
+ if (!this.nodeReferences[node.textReference]) {
207
+ this.nodeReferences[node.textReference] = { _nodeRef: {}, _pseudo: true };
208
+ }
209
+
210
+ node.text = '';
211
+ node.linkToDestination = node.textReference;
212
+ node._textRef = this.nodeReferences[node.textReference];
213
+ }
214
+
215
+ if (node.text && node.text.text) {
216
+ node.text = [this.preprocessNode(node.text)];
217
+ } else if (Array.isArray(node.text)) {
218
+ let isSetParentNode = false;
219
+ if (this.parentNode === null) {
220
+ this.parentNode = node;
221
+ isSetParentNode = true;
222
+ }
223
+
224
+ for (let i = 0, l = node.text.length; i < l; i++) {
225
+ node.text[i] = this.preprocessNode(node.text[i]);
226
+ }
227
+
228
+ if (isSetParentNode) {
229
+ this.parentNode = null;
230
+ }
231
+ }
232
+
233
+ return node;
234
+ }
235
+
236
+ preprocessToc(node) {
237
+ if (!node.toc.id) {
238
+ node.toc.id = '_default_';
239
+ }
240
+
241
+ node.toc.title = node.toc.title ? this.preprocessNode(node.toc.title) : null;
242
+ node.toc._items = [];
243
+
244
+ if (this.tocs[node.toc.id]) {
245
+ if (!this.tocs[node.toc.id].toc._pseudo) {
246
+ throw new Error(`TOC '${node.toc.id}' already exists`);
247
+ }
248
+
249
+ node.toc._items = this.tocs[node.toc.id].toc._items;
250
+ }
251
+
252
+ this.tocs[node.toc.id] = node;
253
+
254
+ return node;
255
+ }
256
+
257
+ preprocessImage(node) {
258
+ if ((node.image.type !== undefined) && (node.image.data !== undefined) && (node.image.type === 'Buffer') && Array.isArray(node.image.data)) {
259
+ node.image = Buffer.from(node.image.data);
260
+ }
261
+ return node;
262
+ }
263
+
264
+ preprocessCanvas(node) {
265
+ return node;
266
+ }
267
+
268
+ preprocessSVG(node) {
269
+ return node;
270
+ }
271
+
272
+ preprocessQr(node) {
273
+ return node;
274
+ }
275
+
276
+ preprocessAttachment(node) {
277
+ let src = node.attachment.src;
278
+ if (src && src.type === 'Buffer' && Array.isArray(src.data)) {
279
+ node.attachment.src = Buffer.from(src.data);
280
+ }
281
+ return node;
282
+ }
283
+
284
+ _getNodeForNodeRef(node) {
285
+ if (this.parentNode) {
286
+ return this.parentNode;
287
+ }
288
+
289
+ return node;
290
+ }
291
+ }
292
+
293
+ export default DocPreprocessor;