pdfmake 0.3.0-beta.8 → 0.3.0-beta.9

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 (56) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/build/pdfmake.js +74624 -74535
  3. package/build/pdfmake.js.map +1 -1
  4. package/build/pdfmake.min.js +2 -2
  5. package/build/pdfmake.min.js.map +1 -1
  6. package/js/3rd-party/svg-to-pdfkit/source.js +3626 -0
  7. package/js/3rd-party/svg-to-pdfkit.js +7 -0
  8. package/js/DocMeasure.js +626 -0
  9. package/js/DocPreprocessor.js +238 -0
  10. package/js/DocumentContext.js +258 -0
  11. package/js/ElementWriter.js +331 -0
  12. package/js/LayoutBuilder.js +744 -0
  13. package/js/Line.js +105 -0
  14. package/js/OutputDocument.js +76 -0
  15. package/js/OutputDocumentServer.js +27 -0
  16. package/js/PDFDocument.js +144 -0
  17. package/js/PageElementWriter.js +140 -0
  18. package/js/PageSize.js +74 -0
  19. package/js/Printer.js +291 -0
  20. package/js/Renderer.js +375 -0
  21. package/js/SVGMeasure.js +69 -0
  22. package/js/StyleContextStack.js +164 -0
  23. package/js/TableProcessor.js +522 -0
  24. package/js/TextBreaker.js +139 -0
  25. package/js/TextDecorator.js +143 -0
  26. package/js/TextInlines.js +206 -0
  27. package/js/URLResolver.js +73 -0
  28. package/js/base.js +52 -0
  29. package/js/browser-extensions/OutputDocumentBrowser.js +118 -0
  30. package/js/browser-extensions/URLBrowserResolver.js +76 -0
  31. package/js/browser-extensions/fonts/Roboto.js +38 -0
  32. package/js/browser-extensions/index.js +53 -0
  33. package/js/browser-extensions/pdfMake.js +15 -0
  34. package/js/browser-extensions/standard-fonts/Courier.js +38 -0
  35. package/js/browser-extensions/standard-fonts/Helvetica.js +38 -0
  36. package/js/browser-extensions/standard-fonts/Symbol.js +23 -0
  37. package/js/browser-extensions/standard-fonts/Times.js +38 -0
  38. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +23 -0
  39. package/js/browser-extensions/virtual-fs-cjs.js +3 -0
  40. package/js/columnCalculator.js +148 -0
  41. package/js/helpers/node.js +98 -0
  42. package/js/helpers/tools.js +40 -0
  43. package/js/helpers/variableType.js +59 -0
  44. package/js/index.js +15 -0
  45. package/js/qrEnc.js +721 -0
  46. package/js/standardPageSizes.js +56 -0
  47. package/js/tableLayouts.js +98 -0
  48. package/js/virtual-fs.js +60 -0
  49. package/package.json +1 -1
  50. package/src/DocMeasure.js +7 -6
  51. package/src/DocumentContext.js +6 -13
  52. package/src/LayoutBuilder.js +52 -2
  53. package/src/StyleContextStack.js +3 -44
  54. package/src/TableProcessor.js +22 -6
  55. package/src/columnCalculator.js +24 -3
  56. package/src/helpers/variableType.js +11 -0
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _variableType = require("./helpers/variableType");
6
+ var _node = require("./helpers/node");
7
+ const convertValueToString = value => {
8
+ if ((0, _variableType.isString)(value)) {
9
+ return value.replace(/\t/g, ' '); // expand tab as spaces
10
+ } else if ((0, _variableType.isNumber)(value) || typeof value === 'boolean') {
11
+ return value.toString();
12
+ } else if (!(0, _variableType.isValue)(value) || (0, _variableType.isEmptyObject)(value)) {
13
+ return '';
14
+ }
15
+
16
+ // TODO: throw exception ?
17
+
18
+ return value;
19
+ };
20
+ class DocPreprocessor {
21
+ preprocessDocument(docStructure) {
22
+ this.parentNode = null;
23
+ this.tocs = [];
24
+ this.nodeReferences = [];
25
+ return this.preprocessNode(docStructure);
26
+ }
27
+ preprocessNode(node) {
28
+ // expand shortcuts and casting values
29
+ if (Array.isArray(node)) {
30
+ node = {
31
+ stack: node
32
+ };
33
+ } else if ((0, _variableType.isString)(node) || (0, _variableType.isNumber)(node) || typeof node === 'boolean' || !(0, _variableType.isValue)(node) || (0, _variableType.isEmptyObject)(node)) {
34
+ // text node defined as value
35
+ node = {
36
+ text: convertValueToString(node)
37
+ };
38
+ } else if ('text' in node) {
39
+ // cast value in text property
40
+ node.text = convertValueToString(node.text);
41
+ }
42
+ if (node.columns) {
43
+ return this.preprocessColumns(node);
44
+ } else if (node.stack) {
45
+ return this.preprocessVerticalContainer(node);
46
+ } else if (node.ul) {
47
+ return this.preprocessList(node);
48
+ } else if (node.ol) {
49
+ return this.preprocessList(node);
50
+ } else if (node.table) {
51
+ return this.preprocessTable(node);
52
+ } else if (node.text !== undefined) {
53
+ return this.preprocessText(node);
54
+ } else if (node.toc) {
55
+ return this.preprocessToc(node);
56
+ } else if (node.image) {
57
+ return this.preprocessImage(node);
58
+ } else if (node.svg) {
59
+ return this.preprocessSVG(node);
60
+ } else if (node.canvas) {
61
+ return this.preprocessCanvas(node);
62
+ } else if (node.qr) {
63
+ return this.preprocessQr(node);
64
+ } else if (node.attachment) {
65
+ return this.preprocessAttachment(node);
66
+ } else if (node.pageReference || node.textReference) {
67
+ return this.preprocessText(node);
68
+ } else {
69
+ throw new Error(`Unrecognized document structure: ${(0, _node.stringifyNode)(node)}`);
70
+ }
71
+ }
72
+ preprocessColumns(node) {
73
+ let columns = node.columns;
74
+ for (let i = 0, l = columns.length; i < l; i++) {
75
+ columns[i] = this.preprocessNode(columns[i]);
76
+ }
77
+ return node;
78
+ }
79
+ preprocessVerticalContainer(node) {
80
+ let items = node.stack;
81
+ for (let i = 0, l = items.length; i < l; i++) {
82
+ items[i] = this.preprocessNode(items[i]);
83
+ }
84
+ return node;
85
+ }
86
+ preprocessList(node) {
87
+ let items = node.ul || node.ol;
88
+ for (let i = 0, l = items.length; i < l; i++) {
89
+ items[i] = this.preprocessNode(items[i]);
90
+ }
91
+ return node;
92
+ }
93
+ preprocessTable(node) {
94
+ let col;
95
+ let row;
96
+ let cols;
97
+ let rows;
98
+ for (col = 0, cols = node.table.body[0].length; col < cols; col++) {
99
+ for (row = 0, rows = node.table.body.length; row < rows; row++) {
100
+ let rowData = node.table.body[row];
101
+ let data = rowData[col];
102
+ if (data !== undefined) {
103
+ if (data === null) {
104
+ // transform to object
105
+ data = '';
106
+ }
107
+ if (!data._span) {
108
+ rowData[col] = this.preprocessNode(data);
109
+ }
110
+ }
111
+ }
112
+ }
113
+ return node;
114
+ }
115
+ preprocessText(node) {
116
+ if (node.tocItem) {
117
+ if (!Array.isArray(node.tocItem)) {
118
+ node.tocItem = [node.tocItem];
119
+ }
120
+ for (let i = 0, l = node.tocItem.length; i < l; i++) {
121
+ if (!(0, _variableType.isString)(node.tocItem[i])) {
122
+ node.tocItem[i] = '_default_';
123
+ }
124
+ let tocItemId = node.tocItem[i];
125
+ if (!this.tocs[tocItemId]) {
126
+ this.tocs[tocItemId] = {
127
+ toc: {
128
+ _items: [],
129
+ _pseudo: true
130
+ }
131
+ };
132
+ }
133
+ if (!node.id) {
134
+ node.id = `toc-${tocItemId}-${this.tocs[tocItemId].toc._items.length}`;
135
+ }
136
+ let tocItemRef = {
137
+ _nodeRef: this._getNodeForNodeRef(node),
138
+ _textNodeRef: node
139
+ };
140
+ this.tocs[tocItemId].toc._items.push(tocItemRef);
141
+ }
142
+ }
143
+ if (node.id) {
144
+ if (this.nodeReferences[node.id]) {
145
+ if (!this.nodeReferences[node.id]._pseudo) {
146
+ throw new Error(`Node id '${node.id}' already exists`);
147
+ }
148
+ this.nodeReferences[node.id]._nodeRef = this._getNodeForNodeRef(node);
149
+ this.nodeReferences[node.id]._textNodeRef = node;
150
+ this.nodeReferences[node.id]._pseudo = false;
151
+ } else {
152
+ this.nodeReferences[node.id] = {
153
+ _nodeRef: this._getNodeForNodeRef(node),
154
+ _textNodeRef: node
155
+ };
156
+ }
157
+ }
158
+ if (node.pageReference) {
159
+ if (!this.nodeReferences[node.pageReference]) {
160
+ this.nodeReferences[node.pageReference] = {
161
+ _nodeRef: {},
162
+ _textNodeRef: {},
163
+ _pseudo: true
164
+ };
165
+ }
166
+ node.text = '00000';
167
+ node.linkToDestination = node.pageReference;
168
+ node._pageRef = this.nodeReferences[node.pageReference];
169
+ }
170
+ if (node.textReference) {
171
+ if (!this.nodeReferences[node.textReference]) {
172
+ this.nodeReferences[node.textReference] = {
173
+ _nodeRef: {},
174
+ _pseudo: true
175
+ };
176
+ }
177
+ node.text = '';
178
+ node.linkToDestination = node.textReference;
179
+ node._textRef = this.nodeReferences[node.textReference];
180
+ }
181
+ if (node.text && node.text.text) {
182
+ node.text = [this.preprocessNode(node.text)];
183
+ } else if (Array.isArray(node.text)) {
184
+ let isSetParentNode = false;
185
+ if (this.parentNode === null) {
186
+ this.parentNode = node;
187
+ isSetParentNode = true;
188
+ }
189
+ for (let i = 0, l = node.text.length; i < l; i++) {
190
+ node.text[i] = this.preprocessNode(node.text[i]);
191
+ }
192
+ if (isSetParentNode) {
193
+ this.parentNode = null;
194
+ }
195
+ }
196
+ return node;
197
+ }
198
+ preprocessToc(node) {
199
+ if (!node.toc.id) {
200
+ node.toc.id = '_default_';
201
+ }
202
+ node.toc.title = node.toc.title ? this.preprocessNode(node.toc.title) : null;
203
+ node.toc._items = [];
204
+ if (this.tocs[node.toc.id]) {
205
+ if (!this.tocs[node.toc.id].toc._pseudo) {
206
+ throw new Error(`TOC '${node.toc.id}' already exists`);
207
+ }
208
+ node.toc._items = this.tocs[node.toc.id].toc._items;
209
+ }
210
+ this.tocs[node.toc.id] = node;
211
+ return node;
212
+ }
213
+ preprocessImage(node) {
214
+ if (node.image.type !== undefined && node.image.data !== undefined && node.image.type === 'Buffer' && Array.isArray(node.image.data)) {
215
+ node.image = Buffer.from(node.image.data);
216
+ }
217
+ return node;
218
+ }
219
+ preprocessCanvas(node) {
220
+ return node;
221
+ }
222
+ preprocessSVG(node) {
223
+ return node;
224
+ }
225
+ preprocessQr(node) {
226
+ return node;
227
+ }
228
+ preprocessAttachment(node) {
229
+ return node;
230
+ }
231
+ _getNodeForNodeRef(node) {
232
+ if (this.parentNode) {
233
+ return this.parentNode;
234
+ }
235
+ return node;
236
+ }
237
+ }
238
+ var _default = exports.default = DocPreprocessor;
@@ -0,0 +1,258 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _variableType = require("./helpers/variableType");
6
+ var _events = require("events");
7
+ /**
8
+ * A store for current x, y positions and available width/height.
9
+ * It facilitates column divisions and vertical sync
10
+ */
11
+ class DocumentContext extends _events.EventEmitter {
12
+ constructor(pageSize, pageMargins) {
13
+ super();
14
+ this.pages = [];
15
+ this.pageMargins = pageMargins;
16
+ this.x = pageMargins.left;
17
+ this.availableWidth = pageSize.width - pageMargins.left - pageMargins.right;
18
+ this.availableHeight = 0;
19
+ this.page = -1;
20
+ this.snapshots = [];
21
+ this.backgroundLength = [];
22
+ this.addPage(pageSize);
23
+ }
24
+ beginColumnGroup() {
25
+ this.snapshots.push({
26
+ x: this.x,
27
+ y: this.y,
28
+ availableHeight: this.availableHeight,
29
+ availableWidth: this.availableWidth,
30
+ page: this.page,
31
+ bottomMost: {
32
+ x: this.x,
33
+ y: this.y,
34
+ availableHeight: this.availableHeight,
35
+ availableWidth: this.availableWidth,
36
+ page: this.page
37
+ },
38
+ lastColumnWidth: this.lastColumnWidth
39
+ });
40
+ this.lastColumnWidth = 0;
41
+ }
42
+ beginColumn(width, offset, endingCell) {
43
+ let saved = this.snapshots[this.snapshots.length - 1];
44
+ this.calculateBottomMost(saved, endingCell);
45
+ this.page = saved.page;
46
+ this.x = this.x + this.lastColumnWidth + (offset || 0);
47
+ this.y = saved.y;
48
+ this.availableWidth = width; //saved.availableWidth - offset;
49
+ this.availableHeight = saved.availableHeight;
50
+ this.lastColumnWidth = width;
51
+ }
52
+ calculateBottomMost(destContext, endingCell) {
53
+ if (endingCell) {
54
+ this.saveContextInEndingCell(endingCell);
55
+ } else {
56
+ destContext.bottomMost = bottomMostContext(this, destContext.bottomMost);
57
+ }
58
+ }
59
+ markEnding(endingCell) {
60
+ this.page = endingCell._columnEndingContext.page;
61
+ this.x = endingCell._columnEndingContext.x;
62
+ this.y = endingCell._columnEndingContext.y;
63
+ this.availableWidth = endingCell._columnEndingContext.availableWidth;
64
+ this.availableHeight = endingCell._columnEndingContext.availableHeight;
65
+ this.lastColumnWidth = endingCell._columnEndingContext.lastColumnWidth;
66
+ }
67
+ saveContextInEndingCell(endingCell) {
68
+ endingCell._columnEndingContext = {
69
+ page: this.page,
70
+ x: this.x,
71
+ y: this.y,
72
+ availableHeight: this.availableHeight,
73
+ availableWidth: this.availableWidth,
74
+ lastColumnWidth: this.lastColumnWidth
75
+ };
76
+ }
77
+ completeColumnGroup(height, endingCell) {
78
+ let saved = this.snapshots.pop();
79
+ this.calculateBottomMost(saved, endingCell);
80
+ this.x = saved.x;
81
+ let y = saved.bottomMost.y;
82
+ if (height) {
83
+ if (saved.page === saved.bottomMost.page) {
84
+ if (saved.y + height > y) {
85
+ y = saved.y + height;
86
+ }
87
+ } else {
88
+ y += height;
89
+ }
90
+ }
91
+ this.y = y;
92
+ this.page = saved.bottomMost.page;
93
+ this.availableWidth = saved.availableWidth;
94
+ this.availableHeight = saved.bottomMost.availableHeight;
95
+ if (height) {
96
+ this.availableHeight -= y - saved.bottomMost.y;
97
+ }
98
+ this.lastColumnWidth = saved.lastColumnWidth;
99
+ }
100
+ addMargin(left, right) {
101
+ this.x += left;
102
+ this.availableWidth -= left + (right || 0);
103
+ }
104
+ moveDown(offset) {
105
+ this.y += offset;
106
+ this.availableHeight -= offset;
107
+ return this.availableHeight > 0;
108
+ }
109
+ initializePage() {
110
+ this.y = this.pageMargins.top;
111
+ this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
112
+ this.pageSnapshot().availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right;
113
+ }
114
+ pageSnapshot() {
115
+ if (this.snapshots[0]) {
116
+ return this.snapshots[0];
117
+ } else {
118
+ return this;
119
+ }
120
+ }
121
+ moveTo(x, y) {
122
+ if (x !== undefined && x !== null) {
123
+ this.x = x;
124
+ this.availableWidth = this.getCurrentPage().pageSize.width - this.x - this.pageMargins.right;
125
+ }
126
+ if (y !== undefined && y !== null) {
127
+ this.y = y;
128
+ this.availableHeight = this.getCurrentPage().pageSize.height - this.y - this.pageMargins.bottom;
129
+ }
130
+ }
131
+ moveToRelative(x, y) {
132
+ if (x !== undefined && x !== null) {
133
+ this.x = this.x + x;
134
+ }
135
+ if (y !== undefined && y !== null) {
136
+ this.y = this.y + y;
137
+ }
138
+ }
139
+ beginDetachedBlock() {
140
+ this.snapshots.push({
141
+ x: this.x,
142
+ y: this.y,
143
+ availableHeight: this.availableHeight,
144
+ availableWidth: this.availableWidth,
145
+ page: this.page,
146
+ lastColumnWidth: this.lastColumnWidth
147
+ });
148
+ }
149
+ endDetachedBlock() {
150
+ let saved = this.snapshots.pop();
151
+ this.x = saved.x;
152
+ this.y = saved.y;
153
+ this.availableWidth = saved.availableWidth;
154
+ this.availableHeight = saved.availableHeight;
155
+ this.page = saved.page;
156
+ this.lastColumnWidth = saved.lastColumnWidth;
157
+ }
158
+ moveToNextPage(pageOrientation) {
159
+ let nextPageIndex = this.page + 1;
160
+ let prevPage = this.page;
161
+ let prevY = this.y;
162
+ let createNewPage = nextPageIndex >= this.pages.length;
163
+ if (createNewPage) {
164
+ let currentAvailableWidth = this.availableWidth;
165
+ let currentPageOrientation = this.getCurrentPage().pageSize.orientation;
166
+ let pageSize = getPageSize(this.getCurrentPage(), pageOrientation);
167
+ this.addPage(pageSize);
168
+ if (currentPageOrientation === pageSize.orientation) {
169
+ this.availableWidth = currentAvailableWidth;
170
+ }
171
+ } else {
172
+ this.page = nextPageIndex;
173
+ this.initializePage();
174
+ }
175
+ return {
176
+ newPageCreated: createNewPage,
177
+ prevPage: prevPage,
178
+ prevY: prevY,
179
+ y: this.y
180
+ };
181
+ }
182
+ addPage(pageSize) {
183
+ let page = {
184
+ items: [],
185
+ pageSize: pageSize
186
+ };
187
+ this.pages.push(page);
188
+ this.backgroundLength.push(0);
189
+ this.page = this.pages.length - 1;
190
+ this.initializePage();
191
+ this.emit('pageAdded');
192
+ return page;
193
+ }
194
+ getCurrentPage() {
195
+ if (this.page < 0 || this.page >= this.pages.length) {
196
+ return null;
197
+ }
198
+ return this.pages[this.page];
199
+ }
200
+ getCurrentPosition() {
201
+ let pageSize = this.getCurrentPage().pageSize;
202
+ let innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom;
203
+ let innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right;
204
+ return {
205
+ pageNumber: this.page + 1,
206
+ pageOrientation: pageSize.orientation,
207
+ pageInnerHeight: innerHeight,
208
+ pageInnerWidth: innerWidth,
209
+ left: this.x,
210
+ top: this.y,
211
+ verticalRatio: (this.y - this.pageMargins.top) / innerHeight,
212
+ horizontalRatio: (this.x - this.pageMargins.left) / innerWidth
213
+ };
214
+ }
215
+ }
216
+ function pageOrientation(pageOrientationString, currentPageOrientation) {
217
+ if (pageOrientationString === undefined) {
218
+ return currentPageOrientation;
219
+ } else if ((0, _variableType.isString)(pageOrientationString) && pageOrientationString.toLowerCase() === 'landscape') {
220
+ return 'landscape';
221
+ } else {
222
+ return 'portrait';
223
+ }
224
+ }
225
+ const getPageSize = (currentPage, newPageOrientation) => {
226
+ newPageOrientation = pageOrientation(newPageOrientation, currentPage.pageSize.orientation);
227
+ if (newPageOrientation !== currentPage.pageSize.orientation) {
228
+ return {
229
+ orientation: newPageOrientation,
230
+ width: currentPage.pageSize.height,
231
+ height: currentPage.pageSize.width
232
+ };
233
+ } else {
234
+ return {
235
+ orientation: currentPage.pageSize.orientation,
236
+ width: currentPage.pageSize.width,
237
+ height: currentPage.pageSize.height
238
+ };
239
+ }
240
+ };
241
+ function bottomMostContext(c1, c2) {
242
+ let r;
243
+ if (c1.page > c2.page) {
244
+ r = c1;
245
+ } else if (c2.page > c1.page) {
246
+ r = c2;
247
+ } else {
248
+ r = c1.y > c2.y ? c1 : c2;
249
+ }
250
+ return {
251
+ page: r.page,
252
+ x: r.x,
253
+ y: r.y,
254
+ availableHeight: r.availableHeight,
255
+ availableWidth: r.availableWidth
256
+ };
257
+ }
258
+ var _default = exports.default = DocumentContext;