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
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _linebreak = _interopRequireDefault(require("linebreak"));
6
+ var _variableType = require("./helpers/variableType");
7
+ var _StyleContextStack = _interopRequireDefault(require("./StyleContextStack"));
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
+ /**
10
+ * @param {string} text
11
+ * @param {boolean} noWrap
12
+ * @param {boolean} breakAll
13
+ * @returns {Array}
14
+ */
15
+ const splitWords = (text, noWrap, breakAll = false) => {
16
+ let words = [];
17
+ if (text === undefined || text === null) {
18
+ text = '';
19
+ } else {
20
+ text = String(text);
21
+ }
22
+ if (noWrap) {
23
+ words.push({
24
+ text: text
25
+ });
26
+ return words;
27
+ }
28
+ if (breakAll) {
29
+ return text.split('').map(c => {
30
+ if (c.match(/^\n$|^\r$/)) {
31
+ // new line
32
+ return {
33
+ text: '',
34
+ lineEnd: true
35
+ };
36
+ }
37
+ return {
38
+ text: c
39
+ };
40
+ });
41
+ }
42
+ if (text.length === 0) {
43
+ words.push({
44
+ text: ''
45
+ });
46
+ return words;
47
+ }
48
+ let breaker = new _linebreak.default(text);
49
+ let last = 0;
50
+ let bk;
51
+ while (bk = breaker.nextBreak()) {
52
+ let word = text.slice(last, bk.position);
53
+ if (bk.required || word.match(/\r?\n$|\r$/)) {
54
+ // new line
55
+ word = word.replace(/\r?\n$|\r$/, '');
56
+ words.push({
57
+ text: word,
58
+ lineEnd: true
59
+ });
60
+ } else {
61
+ words.push({
62
+ text: word
63
+ });
64
+ }
65
+ last = bk.position;
66
+ }
67
+ return words;
68
+ };
69
+
70
+ /**
71
+ * @param {Array} words
72
+ * @param {boolean} noWrap
73
+ * @returns {?string}
74
+ */
75
+ const getFirstWord = (words, noWrap) => {
76
+ let word = words[0];
77
+ if (word === undefined) {
78
+ return null;
79
+ }
80
+ if (noWrap) {
81
+ // text was not wrapped, we need only first word
82
+ let tmpWords = splitWords(word.text, false);
83
+ if (tmpWords[0] === undefined) {
84
+ return null;
85
+ }
86
+ word = tmpWords[0];
87
+ }
88
+ return word.text;
89
+ };
90
+
91
+ /**
92
+ * @param {Array} words
93
+ * @param {boolean} noWrap
94
+ * @returns {?string}
95
+ */
96
+ const getLastWord = (words, noWrap) => {
97
+ let word = words[words.length - 1];
98
+ if (word === undefined) {
99
+ return null;
100
+ }
101
+ if (word.lineEnd) {
102
+ return null;
103
+ }
104
+ if (noWrap) {
105
+ // text was not wrapped, we need only last word
106
+ let tmpWords = splitWords(word.text, false);
107
+ if (tmpWords[tmpWords.length - 1] === undefined) {
108
+ return null;
109
+ }
110
+ word = tmpWords[tmpWords.length - 1];
111
+ }
112
+ return word.text;
113
+ };
114
+ class TextBreaker {
115
+ /**
116
+ * @param {string|Array} texts
117
+ * @param {StyleContextStack} styleContextStack
118
+ * @returns {Array}
119
+ */
120
+ getBreaks(texts, styleContextStack) {
121
+ let results = [];
122
+ if (!Array.isArray(texts)) {
123
+ texts = [texts];
124
+ }
125
+ let lastWord = null;
126
+ for (let i = 0, l = texts.length; i < l; i++) {
127
+ let item = texts[i];
128
+ let style = null;
129
+ let words;
130
+ let breakAll = _StyleContextStack.default.getStyleProperty(item || {}, styleContextStack, 'wordBreak', 'normal') === 'break-all';
131
+ let noWrap = _StyleContextStack.default.getStyleProperty(item || {}, styleContextStack, 'noWrap', false);
132
+ if ((0, _variableType.isObject)(item)) {
133
+ if (item.text) {
134
+ if (item._textRef && item._textRef._textNodeRef.text) {
135
+ item.text = item._textRef._textNodeRef.text;
136
+ }
137
+ words = splitWords(item.text, noWrap, breakAll);
138
+ } else if (item.acroform) {
139
+ words = [item];
140
+ }
141
+ style = _StyleContextStack.default.copyStyle(item);
142
+ } else {
143
+ words = splitWords(item, noWrap, breakAll);
144
+ }
145
+ if (lastWord && words.length && !lastWord.acroform) {
146
+ let firstWord = getFirstWord(words, noWrap);
147
+ let wrapWords = splitWords(lastWord + firstWord, false);
148
+ if (wrapWords.length === 1) {
149
+ results[results.length - 1].noNewLine = true;
150
+ }
151
+ }
152
+ for (let i2 = 0, l2 = words.length; i2 < l2; i2++) {
153
+ let result = {};
154
+ if (words[0].acroform) {
155
+ result = words[0];
156
+ } else {
157
+ result = {
158
+ text: words[i2].text
159
+ };
160
+ }
161
+ if (words[i2].lineEnd) {
162
+ result.lineEnd = true;
163
+ }
164
+ _StyleContextStack.default.copyStyle(style, result);
165
+ results.push(result);
166
+ }
167
+ lastWord = null;
168
+ if (i + 1 < l) {
169
+ if (words[0].acroform) {
170
+ lastWord = words[0];
171
+ } else {
172
+ lastWord = getLastWord(words, noWrap);
173
+ }
174
+ }
175
+ }
176
+ return results;
177
+ }
178
+ }
179
+ var _default = exports.default = TextBreaker;
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _variableType = require("./helpers/variableType");
6
+ const groupDecorations = line => {
7
+ let groups = [];
8
+ let currentGroup = null;
9
+ for (let i = 0, l = line.inlines.length; i < l; i++) {
10
+ let inline = line.inlines[i];
11
+ let decoration = inline.decoration;
12
+ if (!decoration) {
13
+ currentGroup = null;
14
+ continue;
15
+ }
16
+ if (!Array.isArray(decoration)) {
17
+ decoration = [decoration];
18
+ }
19
+ let color = inline.decorationColor || inline.color || 'black';
20
+ let style = inline.decorationStyle || 'solid';
21
+ let thickness = (0, _variableType.isNumber)(inline.decorationThickness) ? inline.decorationThickness : null;
22
+ for (let ii = 0, ll = decoration.length; ii < ll; ii++) {
23
+ let decorationItem = decoration[ii];
24
+ if (!currentGroup || decorationItem !== currentGroup.decoration || style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor) {
25
+ currentGroup = {
26
+ line: line,
27
+ decoration: decorationItem,
28
+ decorationColor: color,
29
+ decorationStyle: style,
30
+ decorationThickness: thickness,
31
+ inlines: [inline]
32
+ };
33
+ groups.push(currentGroup);
34
+ } else {
35
+ currentGroup.inlines.push(inline);
36
+ }
37
+ }
38
+ }
39
+ return groups;
40
+ };
41
+ class TextDecorator {
42
+ constructor(pdfDocument) {
43
+ this.pdfDocument = pdfDocument;
44
+ }
45
+ drawBackground(line, x, y) {
46
+ let height = line.getHeight();
47
+ for (let i = 0, l = line.inlines.length; i < l; i++) {
48
+ let inline = line.inlines[i];
49
+ if (!inline.background) {
50
+ continue;
51
+ }
52
+ let color = inline.background;
53
+ let patternColor = this.pdfDocument.providePattern(inline.background);
54
+ if (patternColor !== null) {
55
+ color = patternColor;
56
+ }
57
+ let justifyShift = inline.justifyShift || 0;
58
+ this.pdfDocument.fillColor(color).rect(x + inline.x - justifyShift, y, inline.width + justifyShift, height).fill();
59
+ }
60
+ }
61
+ drawDecorations(line, x, y) {
62
+ let groups = groupDecorations(line);
63
+ for (let i = 0, l = groups.length; i < l; i++) {
64
+ this._drawDecoration(groups[i], x, y);
65
+ }
66
+ }
67
+ _drawDecoration(group, x, y) {
68
+ const maxInline = () => {
69
+ let max = 0;
70
+ for (let i = 0, l = group.inlines.length; i < l; i++) {
71
+ let inline = group.inlines[i];
72
+ max = inline.fontSize > max ? i : max;
73
+ }
74
+ return group.inlines[max];
75
+ };
76
+ const width = () => {
77
+ let sum = 0;
78
+ for (let i = 0, l = group.inlines.length; i < l; i++) {
79
+ let justifyShift = group.inlines[i].justifyShift || 0;
80
+ sum += group.inlines[i].width + justifyShift;
81
+ }
82
+ return sum;
83
+ };
84
+ let firstInline = group.inlines[0];
85
+ let biggerInline = maxInline();
86
+ let totalWidth = width();
87
+ let lineAscent = group.line.getAscenderHeight();
88
+ let ascent = biggerInline.font.ascender / 1000 * biggerInline.fontSize;
89
+ let height = biggerInline.height;
90
+ let descent = height - ascent;
91
+ let lw = (0, _variableType.isNumber)(group.decorationThickness) ? group.decorationThickness : 0.5 + Math.floor(Math.max(biggerInline.fontSize - 8, 0) / 2) * 0.12;
92
+ switch (group.decoration) {
93
+ case 'underline':
94
+ y += lineAscent + descent * 0.45;
95
+ break;
96
+ case 'overline':
97
+ y += lineAscent - ascent * 0.85;
98
+ break;
99
+ case 'lineThrough':
100
+ y += lineAscent - ascent * 0.25;
101
+ break;
102
+ default:
103
+ throw new Error(`Unknown decoration : ${group.decoration}`);
104
+ }
105
+ if (group.inlines[0].sup) {
106
+ y -= group.inlines[0].fontSize * 0.75;
107
+ }
108
+ if (group.inlines[0].sub) {
109
+ y += group.inlines[0].fontSize * 0.35;
110
+ }
111
+ this.pdfDocument.save();
112
+ if (group.decorationStyle === 'double') {
113
+ let gap = Math.max(0.5, lw, biggerInline.fontSize * 0.15);
114
+ this.pdfDocument.fillColor(group.decorationColor).rect(x + firstInline.x, y - lw / 2, totalWidth, lw / 2).fill().rect(x + firstInline.x, y + gap - lw / 2, totalWidth, lw / 2).fill();
115
+ } else if (group.decorationStyle === 'dashed') {
116
+ let nbDashes = Math.ceil(totalWidth / (3.96 + 2.84));
117
+ let rdx = x + firstInline.x;
118
+ this.pdfDocument.rect(rdx, y, totalWidth, lw).clip();
119
+ this.pdfDocument.fillColor(group.decorationColor);
120
+ for (let i = 0; i < nbDashes; i++) {
121
+ this.pdfDocument.rect(rdx, y - lw / 2, 3.96, lw).fill();
122
+ rdx += 3.96 + 2.84;
123
+ }
124
+ } else if (group.decorationStyle === 'dotted') {
125
+ let nbDots = Math.ceil(totalWidth / (lw * 3));
126
+ let rx = x + firstInline.x;
127
+ this.pdfDocument.rect(rx, y, totalWidth, lw).clip();
128
+ this.pdfDocument.fillColor(group.decorationColor);
129
+ for (let i = 0; i < nbDots; i++) {
130
+ this.pdfDocument.rect(rx, y - lw / 2, lw, lw).fill();
131
+ rx += lw * 3;
132
+ }
133
+ } else if (group.decorationStyle === 'wavy') {
134
+ let sh = 0.7,
135
+ sv = 1;
136
+ let nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
137
+ let rwx = x + firstInline.x - 1;
138
+ this.pdfDocument.rect(x + firstInline.x, y - sv, totalWidth, y + sv).clip();
139
+ this.pdfDocument.lineWidth(lw / 3);
140
+ this.pdfDocument.moveTo(rwx, y);
141
+ for (let i = 0; i < nbWaves; i++) {
142
+ this.pdfDocument.bezierCurveTo(rwx + sh, y - sv, rwx + sh * 2, y - sv, rwx + sh * 3, y).bezierCurveTo(rwx + sh * 4, y + sv, rwx + sh * 5, y + sv, rwx + sh * 6, y);
143
+ rwx += sh * 6;
144
+ }
145
+ this.pdfDocument.stroke(group.decorationColor);
146
+ } else {
147
+ this.pdfDocument.fillColor(group.decorationColor).rect(x + firstInline.x, y - lw / 2, totalWidth, lw).fill();
148
+ }
149
+ this.pdfDocument.restore();
150
+ }
151
+ }
152
+ var _default = exports.default = TextDecorator;
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _TextBreaker = _interopRequireDefault(require("./TextBreaker"));
6
+ var _StyleContextStack = _interopRequireDefault(require("./StyleContextStack"));
7
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
+ const LEADING = /^(\s)+/g;
9
+ const TRAILING = /(\s)+$/g;
10
+
11
+ /**
12
+ * @param {Array} array
13
+ * @returns {Array}
14
+ */
15
+ const flattenTextArray = array => {
16
+ function flatten(array) {
17
+ return array.reduce((prev, cur) => {
18
+ let current = Array.isArray(cur.text) ? flatten(cur.text) : cur;
19
+ let more = [].concat(current).some(Array.isArray);
20
+ return prev.concat(more ? flatten(current) : current);
21
+ }, []);
22
+ }
23
+ if (!Array.isArray(array)) {
24
+ array = [array];
25
+ }
26
+
27
+ // TODO: Styling in nested text (issue: https://github.com/bpampuch/pdfmake/issues/1174)
28
+
29
+ array = flatten(array);
30
+ return array;
31
+ };
32
+
33
+ /**
34
+ * Text measurement utility
35
+ */
36
+ class TextInlines {
37
+ /**
38
+ * @param {object} pdfDocument object is instance of PDFDocument
39
+ */
40
+ constructor(pdfDocument) {
41
+ this.pdfDocument = pdfDocument;
42
+ }
43
+
44
+ /**
45
+ * Converts an array of strings (or inline-definition-objects) into a collection
46
+ * of inlines and calculated minWidth/maxWidth and their min/max widths
47
+ *
48
+ * @param {Array|object} textArray an array of inline-definition-objects (or strings)
49
+ * @param {StyleContextStack} styleContextStack current style stack
50
+ * @returns {object} collection of inlines, minWidth, maxWidth
51
+ */
52
+ buildInlines(textArray, styleContextStack) {
53
+ const getTrimmedWidth = item => {
54
+ return Math.max(0, item.width - item.leadingCut - item.trailingCut);
55
+ };
56
+ let minWidth = 0;
57
+ let maxWidth = 0;
58
+ let currentLineWidth;
59
+ let flattenedTextArray = flattenTextArray(textArray);
60
+ const textBreaker = new _TextBreaker.default();
61
+ let brokenText = textBreaker.getBreaks(flattenedTextArray, styleContextStack);
62
+ let measuredText = this.measure(brokenText, styleContextStack);
63
+ measuredText.forEach(inline => {
64
+ minWidth = Math.max(minWidth, getTrimmedWidth(inline));
65
+ if (!currentLineWidth) {
66
+ currentLineWidth = {
67
+ width: 0,
68
+ leadingCut: inline.leadingCut,
69
+ trailingCut: 0
70
+ };
71
+ }
72
+ currentLineWidth.width += inline.width;
73
+ currentLineWidth.trailingCut = inline.trailingCut;
74
+ maxWidth = Math.max(maxWidth, getTrimmedWidth(currentLineWidth));
75
+ if (inline.lineEnd) {
76
+ currentLineWidth = null;
77
+ }
78
+ });
79
+ if (_StyleContextStack.default.getStyleProperty({}, styleContextStack, 'noWrap', false)) {
80
+ minWidth = maxWidth;
81
+ }
82
+ return {
83
+ items: measuredText,
84
+ minWidth: minWidth,
85
+ maxWidth: maxWidth
86
+ };
87
+ }
88
+ measure(array, styleContextStack) {
89
+ if (array.length) {
90
+ let leadingIndent = _StyleContextStack.default.getStyleProperty(array[0], styleContextStack, 'leadingIndent', 0);
91
+ if (leadingIndent) {
92
+ array[0].leadingCut = -leadingIndent;
93
+ array[0].leadingIndent = leadingIndent;
94
+ }
95
+ }
96
+ array.forEach(item => {
97
+ let font = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'font', 'Roboto');
98
+ let bold = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'bold', false);
99
+ let italics = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'italics', false);
100
+ item.font = this.pdfDocument.provideFont(font, bold, italics);
101
+ item.alignment = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'alignment', 'left');
102
+ item.fontSize = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'fontSize', 12);
103
+ item.fontFeatures = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'fontFeatures', null);
104
+ item.characterSpacing = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'characterSpacing', 0);
105
+ item.color = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'color', 'black');
106
+ item.decoration = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decoration', null);
107
+ item.decorationColor = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationColor', null);
108
+ item.decorationStyle = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationStyle', null);
109
+ item.decorationThickness = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'decorationThickness', null);
110
+ item.background = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'background', null);
111
+ item.link = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'link', null);
112
+ item.linkToPage = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'linkToPage', null);
113
+ item.linkToDestination = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'linkToDestination', null);
114
+ item.noWrap = item.acroform ? true : _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'noWrap', null);
115
+ item.opacity = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'opacity', 1);
116
+ item.sup = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'sup', false);
117
+ item.sub = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'sub', false);
118
+ if (item.sup || item.sub) {
119
+ // font size reduction taken from here: https://en.wikipedia.org/wiki/Subscript_and_superscript#Desktop_publishing
120
+ item.fontSize *= 0.58;
121
+ }
122
+ let lineHeight = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'lineHeight', 1);
123
+ if (item.acroform) {
124
+ item.width = item.width || 25;
125
+ item.height = item.height || 15;
126
+ } else {
127
+ item.width = this.widthOfText(item.text, item);
128
+ item.height = item.font.lineHeight(item.fontSize) * lineHeight;
129
+ if (!item.leadingCut) {
130
+ item.leadingCut = 0;
131
+ }
132
+ let preserveLeadingSpaces = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'preserveLeadingSpaces', false);
133
+ if (!preserveLeadingSpaces) {
134
+ let leadingSpaces = item.text.match(LEADING);
135
+ if (leadingSpaces) {
136
+ item.leadingCut += this.widthOfText(leadingSpaces[0], item);
137
+ }
138
+ }
139
+ item.trailingCut = 0;
140
+ let preserveTrailingSpaces = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'preserveTrailingSpaces', false);
141
+ if (!preserveTrailingSpaces) {
142
+ let trailingSpaces = item.text.match(TRAILING);
143
+ if (trailingSpaces) {
144
+ item.trailingCut = this.widthOfText(trailingSpaces[0], item);
145
+ }
146
+ }
147
+ }
148
+ }, this);
149
+ return array;
150
+ }
151
+
152
+ /**
153
+ * Width of text
154
+ *
155
+ * @param {string} text
156
+ * @param {object} inline
157
+ * @returns {number}
158
+ */
159
+ widthOfText(text, inline) {
160
+ return inline.font.widthOfString(text, inline.fontSize, inline.fontFeatures) + (inline.characterSpacing || 0) * (text.length - 1);
161
+ }
162
+
163
+ /**
164
+ * Returns size of the specified string (without breaking it) using the current style
165
+ *
166
+ * @param {string} text text to be measured
167
+ * @param {object} styleContextStack current style stack
168
+ * @returns {object} size of the specified string
169
+ */
170
+ sizeOfText(text, styleContextStack) {
171
+ //TODO: refactor - extract from measure
172
+ let fontName = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'font', 'Roboto');
173
+ let fontSize = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'fontSize', 12);
174
+ let fontFeatures = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'fontFeatures', null);
175
+ let bold = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'bold', false);
176
+ let italics = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'italics', false);
177
+ let lineHeight = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'lineHeight', 1);
178
+ let characterSpacing = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'characterSpacing', 0);
179
+ let font = this.pdfDocument.provideFont(fontName, bold, italics);
180
+ return {
181
+ width: this.widthOfText(text, {
182
+ font: font,
183
+ fontSize: fontSize,
184
+ characterSpacing: characterSpacing,
185
+ fontFeatures: fontFeatures
186
+ }),
187
+ height: font.lineHeight(fontSize) * lineHeight,
188
+ fontSize: fontSize,
189
+ lineHeight: lineHeight,
190
+ ascender: font.ascender / 1000 * fontSize,
191
+ descender: font.descender / 1000 * fontSize
192
+ };
193
+ }
194
+
195
+ /**
196
+ * Returns size of the specified rotated string (without breaking it) using the current style
197
+ *
198
+ * @param {string} text text to be measured
199
+ * @param {number} angle
200
+ * @param {object} styleContextStack current style stack
201
+ * @returns {object} size of the specified string
202
+ */
203
+ sizeOfRotatedText(text, angle, styleContextStack) {
204
+ let angleRad = angle * Math.PI / -180;
205
+ let size = this.sizeOfText(text, styleContextStack);
206
+ return {
207
+ width: Math.abs(size.height * Math.sin(angleRad)) + Math.abs(size.width * Math.cos(angleRad)),
208
+ height: Math.abs(size.width * Math.sin(angleRad)) + Math.abs(size.height * Math.cos(angleRad))
209
+ };
210
+ }
211
+ }
212
+ var _default = exports.default = TextInlines;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ async function fetchUrl(url, headers = {}) {
6
+ try {
7
+ const response = await fetch(url, {
8
+ headers
9
+ });
10
+ if (!response.ok) {
11
+ throw new Error(`Failed to fetch (status code: ${response.status}, url: "${url}")`);
12
+ }
13
+ return await response.arrayBuffer();
14
+ } catch (error) {
15
+ throw new Error(`Network request failed (url: "${url}", error: ${error.message})`);
16
+ }
17
+ }
18
+ class URLResolver {
19
+ constructor(fs) {
20
+ this.fs = fs;
21
+ this.resolving = {};
22
+ }
23
+ resolve(url, headers = {}) {
24
+ const resolveUrlInternal = async () => {
25
+ if (url.toLowerCase().startsWith('https://') || url.toLowerCase().startsWith('http://')) {
26
+ if (this.fs.existsSync(url)) {
27
+ return; // url was downloaded earlier
28
+ }
29
+ const buffer = await fetchUrl(url, headers);
30
+ this.fs.writeFileSync(url, buffer);
31
+ }
32
+ // else cannot be resolved
33
+ };
34
+ if (!this.resolving[url]) {
35
+ this.resolving[url] = resolveUrlInternal();
36
+ }
37
+ return this.resolving[url];
38
+ }
39
+ resolved() {
40
+ return Promise.all(Object.values(this.resolving));
41
+ }
42
+ }
43
+ var _default = exports.default = URLResolver;
package/js/base.js ADDED
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _Printer = _interopRequireDefault(require("./Printer"));
6
+ var _virtualFs = _interopRequireDefault(require("./virtual-fs"));
7
+ var _tools = require("./helpers/tools");
8
+ var _variableType = require("./helpers/variableType");
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ class pdfmake {
11
+ constructor() {
12
+ this.virtualfs = _virtualFs.default;
13
+ this.urlResolver = null;
14
+ }
15
+
16
+ /**
17
+ * @param {object} docDefinition
18
+ * @param {?object} options
19
+ * @returns {object}
20
+ */
21
+ createPdf(docDefinition, options = {}) {
22
+ if (!(0, _variableType.isObject)(docDefinition)) {
23
+ throw new Error("Parameter 'docDefinition' has an invalid type. Object expected.");
24
+ }
25
+ if (!(0, _variableType.isObject)(options)) {
26
+ throw new Error("Parameter 'options' has an invalid type. Object expected.");
27
+ }
28
+ options.progressCallback = this.progressCallback;
29
+ options.tableLayouts = this.tableLayouts;
30
+ let printer = new _Printer.default(this.fonts, this.virtualfs, this.urlResolver());
31
+ const pdfDocumentPromise = printer.createPdfKitDocument(docDefinition, options);
32
+ return this._transformToDocument(pdfDocumentPromise);
33
+ }
34
+ setProgressCallback(callback) {
35
+ this.progressCallback = callback;
36
+ }
37
+ addTableLayouts(tableLayouts) {
38
+ this.tableLayouts = (0, _tools.pack)(this.tableLayouts, tableLayouts);
39
+ }
40
+ setTableLayouts(tableLayouts) {
41
+ this.tableLayouts = tableLayouts;
42
+ }
43
+ clearTableLayouts() {
44
+ this.tableLayouts = {};
45
+ }
46
+ addFonts(fonts) {
47
+ this.fonts = (0, _tools.pack)(this.fonts, fonts);
48
+ }
49
+ setFonts(fonts) {
50
+ this.fonts = fonts;
51
+ }
52
+ clearFonts() {
53
+ this.fonts = {};
54
+ }
55
+ _transformToDocument(doc) {
56
+ return doc;
57
+ }
58
+ }
59
+ var _default = exports.default = pdfmake;