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/src/Line.js ADDED
@@ -0,0 +1,108 @@
1
+ class Line {
2
+ /**
3
+ * @param {number} maxWidth Maximum width this line can have
4
+ */
5
+ constructor(maxWidth) {
6
+ this.maxWidth = maxWidth;
7
+ this.leadingCut = 0;
8
+ this.trailingCut = 0;
9
+ this.inlineWidths = 0;
10
+ this.inlines = [];
11
+ }
12
+
13
+ /**
14
+ * @param {object} inline
15
+ */
16
+ addInline(inline) {
17
+ if (this.inlines.length === 0) {
18
+ this.leadingCut = inline.leadingCut || 0;
19
+ }
20
+ this.trailingCut = inline.trailingCut || 0;
21
+
22
+ inline.x = this.inlineWidths - this.leadingCut;
23
+
24
+ this.inlines.push(inline);
25
+ this.inlineWidths += inline.width;
26
+
27
+ if (inline.lineEnd) {
28
+ this.newLineForced = true;
29
+ }
30
+ }
31
+
32
+ /**
33
+ * @returns {number}
34
+ */
35
+ getHeight() {
36
+ return Math.max(...this.inlines.map(item => item.height || 0));
37
+ }
38
+
39
+ /**
40
+ * @returns {number}
41
+ */
42
+ getAscenderHeight() {
43
+ let y = 0;
44
+
45
+ this.inlines.forEach(inline => {
46
+ y = Math.max(y, inline.acroform ? inline.height : inline.font.ascender / 1000 * inline.fontSize);
47
+ });
48
+
49
+ return y;
50
+ }
51
+
52
+ /**
53
+ * @returns {number}
54
+ */
55
+ getWidth() {
56
+ return this.inlineWidths - this.leadingCut - this.trailingCut;
57
+ }
58
+
59
+ /**
60
+ * @returns {number}
61
+ */
62
+ getAvailableWidth() {
63
+ return this.maxWidth - this.getWidth();
64
+ }
65
+
66
+ /**
67
+ * @param {object} inline
68
+ * @param {Array} nextInlines
69
+ * @returns {boolean}
70
+ */
71
+ hasEnoughSpaceForInline(inline, nextInlines = []) {
72
+ if (this.inlines.length === 0) {
73
+ return true;
74
+ }
75
+ if (this.newLineForced) {
76
+ return false;
77
+ }
78
+
79
+ let inlineWidth = inline.width;
80
+ let inlineTrailingCut = inline.trailingCut || 0;
81
+ if (inline.noNewLine) {
82
+ for (let i = 0, l = nextInlines.length; i < l; i++) {
83
+ let nextInline = nextInlines[i];
84
+ inlineWidth += nextInline.width;
85
+ inlineTrailingCut += nextInline.trailingCut || 0;
86
+ if (!nextInline.noNewLine) {
87
+ break;
88
+ }
89
+ }
90
+ }
91
+
92
+ return (this.inlineWidths + inlineWidth - this.leadingCut - inlineTrailingCut) <= this.maxWidth;
93
+ }
94
+
95
+ clone() {
96
+ let result = new Line(this.maxWidth);
97
+
98
+ for (let key in this) {
99
+ if (this.hasOwnProperty(key)) {
100
+ result[key] = this[key];
101
+ }
102
+ }
103
+
104
+ return result;
105
+ }
106
+ }
107
+
108
+ export default Line;
@@ -0,0 +1,64 @@
1
+ class OutputDocument {
2
+
3
+ /**
4
+ * @param {Promise<object>} pdfDocumentPromise
5
+ */
6
+ constructor(pdfDocumentPromise) {
7
+ this.bufferSize = 1073741824;
8
+ this.pdfDocumentPromise = pdfDocumentPromise;
9
+ this.bufferPromise = null;
10
+ }
11
+
12
+ /**
13
+ * @returns {Promise<object>}
14
+ */
15
+ getStream() {
16
+ return this.pdfDocumentPromise;
17
+ }
18
+
19
+ /**
20
+ * @returns {Promise<Buffer>}
21
+ */
22
+ getBuffer() {
23
+ const getBufferInternal = (async () => {
24
+ const stream = await this.getStream();
25
+ return new Promise((resolve) => {
26
+ let chunks = [];
27
+ stream.on('readable', () => {
28
+ let chunk;
29
+ while ((chunk = stream.read(this.bufferSize)) !== null) {
30
+ chunks.push(chunk);
31
+ }
32
+ });
33
+ stream.on('end', () => {
34
+ resolve(Buffer.concat(chunks));
35
+ });
36
+ stream.end();
37
+ });
38
+ });
39
+
40
+ if (this.bufferPromise === null) {
41
+ this.bufferPromise = getBufferInternal();
42
+ }
43
+ return this.bufferPromise;
44
+ }
45
+
46
+ /**
47
+ * @returns {Promise<string>}
48
+ */
49
+ async getBase64() {
50
+ const buffer = await this.getBuffer();
51
+ return buffer.toString('base64');
52
+ }
53
+
54
+ /**
55
+ * @returns {Promise<string>}
56
+ */
57
+ async getDataUrl() {
58
+ const data = await this.getBase64();
59
+ return 'data:application/pdf;base64,' + data;
60
+ }
61
+
62
+ }
63
+
64
+ export default OutputDocument;
@@ -0,0 +1,32 @@
1
+ import OutputDocument from './OutputDocument';
2
+ import fs from 'fs';
3
+
4
+ class OutputDocumentServer extends OutputDocument {
5
+
6
+ /**
7
+ * @param {string} filename
8
+ * @returns {Promise}
9
+ */
10
+ async write(filename) {
11
+ const stream = await this.getStream();
12
+ const writeStream = fs.createWriteStream(filename);
13
+
14
+ const streamEnded = new Promise((resolve, reject) => {
15
+ stream.on('end', resolve);
16
+ stream.on('error', reject);
17
+ });
18
+
19
+ const writeClosed = new Promise((resolve, reject) => {
20
+ writeStream.on('close', resolve);
21
+ writeStream.on('error', reject);
22
+ });
23
+
24
+ stream.pipe(writeStream);
25
+ stream.end();
26
+
27
+ await Promise.all([streamEnded, writeClosed]);
28
+ }
29
+
30
+ }
31
+
32
+ export default OutputDocumentServer;
@@ -0,0 +1,178 @@
1
+ import PDFKit from 'pdfkit';
2
+
3
+ const typeName = (bold, italics) => {
4
+ let type = 'normal';
5
+ if (bold && italics) {
6
+ type = 'bolditalics';
7
+ } else if (bold) {
8
+ type = 'bold';
9
+ } else if (italics) {
10
+ type = 'italics';
11
+ }
12
+ return type;
13
+ };
14
+
15
+ class PDFDocument extends PDFKit {
16
+ constructor(fonts = {}, images = {}, patterns = {}, attachments = {}, options = {}, virtualfs = null, subsetFonts = true) {
17
+ super(options);
18
+
19
+ this.fonts = {};
20
+ this.fontCache = {};
21
+ for (let font in fonts) {
22
+ if (fonts.hasOwnProperty(font)) {
23
+ let fontDef = fonts[font];
24
+
25
+ this.fonts[font] = {
26
+ normal: fontDef.normal,
27
+ bold: fontDef.bold,
28
+ italics: fontDef.italics,
29
+ bolditalics: fontDef.bolditalics
30
+ };
31
+ }
32
+ }
33
+
34
+ this.patterns = {};
35
+ for (let pattern in patterns) {
36
+ if (patterns.hasOwnProperty(pattern)) {
37
+ let patternDef = patterns[pattern];
38
+ this.patterns[pattern] = this.pattern(patternDef.boundingBox, patternDef.xStep, patternDef.yStep, patternDef.pattern, patternDef.colored);
39
+ }
40
+ }
41
+
42
+
43
+ this.images = images;
44
+ this.attachments = attachments;
45
+ this.virtualfs = virtualfs;
46
+ this.subsetFonts = subsetFonts; //TODO maybe automatically set this flag
47
+ }
48
+
49
+ getFontType(bold, italics) {
50
+ return typeName(bold, italics);
51
+ }
52
+
53
+ getFontFile(familyName, bold, italics) {
54
+ let type = this.getFontType(bold, italics);
55
+ if (!this.fonts[familyName] || !this.fonts[familyName][type]) {
56
+ return null;
57
+ }
58
+
59
+ return this.fonts[familyName][type];
60
+ }
61
+
62
+ provideFont(familyName, bold, italics) {
63
+ let type = this.getFontType(bold, italics);
64
+ if (this.getFontFile(familyName, bold, italics) === null) {
65
+ throw new Error(`Font '${familyName}' in style '${type}' is not defined in the font section of the document definition.`);
66
+ }
67
+
68
+ this.fontCache[familyName] = this.fontCache[familyName] || {};
69
+
70
+ if (!this.fontCache[familyName][type]) {
71
+ let def = this.fonts[familyName][type];
72
+ if (!Array.isArray(def)) {
73
+ def = [def];
74
+ }
75
+
76
+ if (this.virtualfs && this.virtualfs.existsSync(def[0])) {
77
+ def[0] = this.virtualfs.readFileSync(def[0]);
78
+ }
79
+
80
+
81
+ this.fontCache[familyName][type] = this.font(...def)._font;
82
+
83
+ }
84
+
85
+ return this.fontCache[familyName][type];
86
+ }
87
+
88
+ provideImage(src) {
89
+ const realImageSrc = src => {
90
+ let image = this.images[src];
91
+
92
+ if (!image) {
93
+ return src;
94
+ }
95
+
96
+ if (this.virtualfs && this.virtualfs.existsSync(image)) {
97
+ return this.virtualfs.readFileSync(image);
98
+ }
99
+
100
+ let index = image.indexOf('base64,');
101
+ if (index < 0) {
102
+ return this.images[src];
103
+ }
104
+
105
+ return Buffer.from(image.substring(index + 7), 'base64');
106
+ };
107
+
108
+ if (this._imageRegistry[src]) {
109
+ return this._imageRegistry[src];
110
+ }
111
+
112
+ let image;
113
+
114
+ try {
115
+ image = this.openImage(realImageSrc(src));
116
+ if (!image) {
117
+ throw new Error('No image');
118
+ }
119
+ } catch (error) {
120
+ throw new Error(`Invalid image: ${error.toString()}\nImages dictionary should contain dataURL entries (or local file paths in node.js)`);
121
+ }
122
+
123
+ image.embed(this);
124
+ this._imageRegistry[src] = image;
125
+
126
+ return image;
127
+ }
128
+
129
+ /**
130
+ * @param {Array} color pdfmake format: [<pattern name>, <color>]
131
+ * @returns {Array} pdfkit format: [<pattern object>, <color>]
132
+ */
133
+ providePattern(color) {
134
+ if (Array.isArray(color) && color.length === 2) {
135
+ return [this.patterns[color[0]], color[1]];
136
+ }
137
+
138
+ return null;
139
+ }
140
+
141
+ provideAttachment(src) {
142
+ const checkRequired = obj => {
143
+ if (!obj) {
144
+ throw new Error('No attachment');
145
+ }
146
+ if (!obj.src) {
147
+ throw new Error('The "src" key is required for attachments');
148
+ }
149
+
150
+ return obj;
151
+ };
152
+
153
+ if (typeof src === 'object') {
154
+ return checkRequired(src);
155
+ }
156
+
157
+ let attachment = checkRequired(this.attachments[src]);
158
+
159
+ if (this.virtualfs && this.virtualfs.existsSync(attachment.src)) {
160
+ return this.virtualfs.readFileSync(attachment.src);
161
+ }
162
+
163
+ return attachment;
164
+ }
165
+
166
+ setOpenActionAsPrint() {
167
+ let printActionRef = this.ref({
168
+ Type: 'Action',
169
+ S: 'Named',
170
+ N: 'Print'
171
+ });
172
+ this._root.data.OpenAction = printActionRef;
173
+ printActionRef.end();
174
+ }
175
+ }
176
+
177
+
178
+ export default PDFDocument;
@@ -0,0 +1,191 @@
1
+ import ElementWriter from './ElementWriter';
2
+ import { normalizePageSize, normalizePageMargin } from './PageSize';
3
+ import DocumentContext from './DocumentContext';
4
+
5
+ /**
6
+ * An extended ElementWriter which can handle:
7
+ * - page-breaks (it adds new pages when there's not enough space left),
8
+ * - repeatable fragments (like table-headers, which are repeated everytime
9
+ * a page-break occurs)
10
+ * - transactions (used for unbreakable-blocks when we want to make sure
11
+ * whole block will be rendered on the same page)
12
+ */
13
+ class PageElementWriter extends ElementWriter {
14
+
15
+ /**
16
+ * @param {DocumentContext} context
17
+ */
18
+ constructor(context) {
19
+ super(context);
20
+ this.transactionLevel = 0;
21
+ this.repeatables = [];
22
+ }
23
+
24
+ addLine(line, dontUpdateContextPosition, index) {
25
+ return this._fitOnPage(() => super.addLine(line, dontUpdateContextPosition, index));
26
+ }
27
+
28
+ addImage(image, index) {
29
+ return this._fitOnPage(() => super.addImage(image, index));
30
+ }
31
+
32
+ addCanvas(image, index) {
33
+ return this._fitOnPage(() => super.addCanvas(image, index));
34
+ }
35
+
36
+ addSVG(image, index) {
37
+ return this._fitOnPage(() => super.addSVG(image, index));
38
+ }
39
+
40
+ addQr(qr, index) {
41
+ return this._fitOnPage(() => super.addQr(qr, index));
42
+ }
43
+
44
+ addAttachment(attachment, index) {
45
+ return this._fitOnPage(() => super.addAttachment(attachment, index));
46
+ }
47
+
48
+ addAcroForm(node, index) {
49
+ return this._fitOnPage(() => super.addAcroForm(node, index));
50
+ }
51
+
52
+ addVector(vector, ignoreContextX, ignoreContextY, index, forcePage) {
53
+ return super.addVector(vector, ignoreContextX, ignoreContextY, index, forcePage);
54
+ }
55
+
56
+ beginClip(width, height) {
57
+ return super.beginClip(width, height);
58
+ }
59
+
60
+ endClip() {
61
+ return super.endClip();
62
+ }
63
+
64
+ beginVerticalAlignment(verticalAlignment) {
65
+ return super.beginVerticalAlignment(verticalAlignment);
66
+ }
67
+
68
+ endVerticalAlignment(verticalAlignment) {
69
+ return super.endVerticalAlignment(verticalAlignment);
70
+ }
71
+
72
+ addFragment(fragment, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition) {
73
+ return this._fitOnPage(() => super.addFragment(fragment, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition));
74
+ }
75
+
76
+ moveToNextPage(pageOrientation) {
77
+ let nextPage = this.context().moveToNextPage(pageOrientation);
78
+
79
+ // moveToNextPage is called multiple times for table, because is called for each column
80
+ // and repeatables are inserted only in the first time. If columns are used, is needed
81
+ // call for table in first column and then for table in the second column (is other repeatables).
82
+ this.repeatables.forEach(function (rep) {
83
+ if (rep.insertedOnPages[this.context().page] === undefined) {
84
+ rep.insertedOnPages[this.context().page] = true;
85
+ this.addFragment(rep, true);
86
+ } else {
87
+ this.context().moveDown(rep.height);
88
+ }
89
+ }, this);
90
+
91
+ this.emit('pageChanged', {
92
+ prevPage: nextPage.prevPage,
93
+ prevY: nextPage.prevY,
94
+ y: this.context().y
95
+ });
96
+ }
97
+
98
+ addPage(pageSize, pageOrientation, pageMargin, customProperties = {}) {
99
+ let prevPage = this.page;
100
+ let prevY = this.y;
101
+
102
+ this.context().addPage(normalizePageSize(pageSize, pageOrientation), normalizePageMargin(pageMargin), customProperties);
103
+
104
+ this.emit('pageChanged', {
105
+ prevPage: prevPage,
106
+ prevY: prevY,
107
+ y: this.context().y
108
+ });
109
+ }
110
+
111
+ beginUnbreakableBlock(width, height) {
112
+ if (this.transactionLevel++ === 0) {
113
+ this.originalX = this.context().x;
114
+ this.pushContext(width, height);
115
+ }
116
+ }
117
+
118
+ commitUnbreakableBlock(forcedX, forcedY) {
119
+ if (--this.transactionLevel === 0) {
120
+ let unbreakableContext = this.context();
121
+ this.popContext();
122
+
123
+ let nbPages = unbreakableContext.pages.length;
124
+ if (nbPages > 0) {
125
+ // no support for multi-page unbreakableBlocks
126
+ let fragment = unbreakableContext.pages[0];
127
+ fragment.xOffset = forcedX;
128
+ fragment.yOffset = forcedY;
129
+
130
+ //TODO: vectors can influence height in some situations
131
+ if (nbPages > 1) {
132
+ // on out-of-context blocs (headers, footers, background) height should be the whole DocumentContext height
133
+ if (forcedX !== undefined || forcedY !== undefined) {
134
+ fragment.height = unbreakableContext.getCurrentPage().pageSize.height - unbreakableContext.pageMargins.top - unbreakableContext.pageMargins.bottom;
135
+ } else {
136
+ fragment.height = this.context().getCurrentPage().pageSize.height - this.context().pageMargins.top - this.context().pageMargins.bottom;
137
+ for (let i = 0, l = this.repeatables.length; i < l; i++) {
138
+ fragment.height -= this.repeatables[i].height;
139
+ }
140
+ }
141
+ } else {
142
+ fragment.height = unbreakableContext.y;
143
+ }
144
+
145
+ if (forcedX !== undefined || forcedY !== undefined) {
146
+ super.addFragment(fragment, true, true, true);
147
+ } else {
148
+ this.addFragment(fragment);
149
+ }
150
+ }
151
+ }
152
+ }
153
+
154
+ currentBlockToRepeatable() {
155
+ let unbreakableContext = this.context();
156
+ let rep = { items: [] };
157
+
158
+ unbreakableContext.pages[0].items.forEach(item => {
159
+ rep.items.push(item);
160
+ });
161
+
162
+ rep.xOffset = this.originalX;
163
+
164
+ //TODO: vectors can influence height in some situations
165
+ rep.height = unbreakableContext.y;
166
+
167
+ rep.insertedOnPages = [];
168
+
169
+ return rep;
170
+ }
171
+
172
+ pushToRepeatables(rep) {
173
+ this.repeatables.push(rep);
174
+ }
175
+
176
+ popFromRepeatables() {
177
+ this.repeatables.pop();
178
+ }
179
+
180
+ _fitOnPage(addFct) {
181
+ let position = addFct();
182
+ if (!position) {
183
+ this.moveToNextPage();
184
+ position = addFct();
185
+ }
186
+ return position;
187
+ }
188
+
189
+ }
190
+
191
+ export default PageElementWriter;
@@ -0,0 +1,53 @@
1
+ import sizes from './standardPageSizes';
2
+ import { isString, isNumber } from './helpers/variableType';
3
+
4
+ export function normalizePageSize(pageSize, pageOrientation) {
5
+ function isNeedSwapPageSizes(pageOrientation) {
6
+ if (isString(pageOrientation)) {
7
+ pageOrientation = pageOrientation.toLowerCase();
8
+ return ((pageOrientation === 'portrait') && (size.width > size.height)) ||
9
+ ((pageOrientation === 'landscape') && (size.width < size.height));
10
+ }
11
+ return false;
12
+ }
13
+
14
+ function pageSizeToWidthAndHeight(pageSize) {
15
+ if (isString(pageSize)) {
16
+ let size = sizes[pageSize.toUpperCase()];
17
+ if (!size) {
18
+ throw new Error(`Page size ${pageSize} not recognized`);
19
+ }
20
+ return { width: size[0], height: size[1] };
21
+ }
22
+
23
+ return pageSize;
24
+ }
25
+
26
+ // if pageSize.height is set to auto, set the height to infinity so there are no page breaks.
27
+ if (pageSize && pageSize.height === 'auto') {
28
+ pageSize.height = Infinity;
29
+ }
30
+
31
+ let size = pageSizeToWidthAndHeight(pageSize || 'A4');
32
+ if (isNeedSwapPageSizes(pageOrientation)) { // swap page sizes
33
+ size = { width: size.height, height: size.width };
34
+ }
35
+ size.orientation = size.width > size.height ? 'landscape' : 'portrait';
36
+ return size;
37
+ }
38
+
39
+ export function normalizePageMargin(margin) {
40
+ if (isNumber(margin)) {
41
+ margin = { left: margin, right: margin, top: margin, bottom: margin };
42
+ } else if (Array.isArray(margin)) {
43
+ if (margin.length === 2) {
44
+ margin = { left: margin[0], top: margin[1], right: margin[0], bottom: margin[1] };
45
+ } else if (margin.length === 4) {
46
+ margin = { left: margin[0], top: margin[1], right: margin[2], bottom: margin[3] };
47
+ } else {
48
+ throw new Error('Invalid pageMargins definition');
49
+ }
50
+ }
51
+
52
+ return margin;
53
+ }