pdfmake 0.3.0-beta.8 → 0.3.0

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 (87) hide show
  1. package/CHANGELOG.md +7 -40
  2. package/LICENSE +1 -1
  3. package/README.md +78 -85
  4. package/build/pdfmake.js +60308 -68400
  5. package/build/pdfmake.js.map +1 -1
  6. package/build/pdfmake.min.js +2 -2
  7. package/build/pdfmake.min.js.map +1 -1
  8. package/build/vfs_fonts.js +4 -4
  9. package/fonts/Roboto/Roboto-Italic.ttf +0 -0
  10. package/fonts/Roboto/Roboto-Medium.ttf +0 -0
  11. package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  12. package/fonts/Roboto/Roboto-Regular.ttf +0 -0
  13. package/js/3rd-party/svg-to-pdfkit/source.js +3626 -0
  14. package/js/3rd-party/svg-to-pdfkit.js +7 -0
  15. package/js/DocMeasure.js +645 -0
  16. package/js/DocPreprocessor.js +253 -0
  17. package/js/DocumentContext.js +305 -0
  18. package/js/ElementWriter.js +354 -0
  19. package/js/LayoutBuilder.js +1105 -0
  20. package/js/Line.js +105 -0
  21. package/js/OutputDocument.js +64 -0
  22. package/js/OutputDocumentServer.js +22 -0
  23. package/js/PDFDocument.js +144 -0
  24. package/js/PageElementWriter.js +155 -0
  25. package/js/PageSize.js +74 -0
  26. package/js/Printer.js +291 -0
  27. package/js/Renderer.js +383 -0
  28. package/js/SVGMeasure.js +69 -0
  29. package/js/StyleContextStack.js +168 -0
  30. package/js/TableProcessor.js +548 -0
  31. package/js/TextBreaker.js +166 -0
  32. package/js/TextDecorator.js +143 -0
  33. package/js/TextInlines.js +206 -0
  34. package/js/URLResolver.js +43 -0
  35. package/js/base.js +52 -0
  36. package/js/browser-extensions/OutputDocumentBrowser.js +81 -0
  37. package/js/browser-extensions/fonts/Roboto.js +38 -0
  38. package/js/browser-extensions/index.js +53 -0
  39. package/js/browser-extensions/pdfMake.js +3 -0
  40. package/js/browser-extensions/standard-fonts/Courier.js +38 -0
  41. package/js/browser-extensions/standard-fonts/Helvetica.js +38 -0
  42. package/js/browser-extensions/standard-fonts/Symbol.js +23 -0
  43. package/js/browser-extensions/standard-fonts/Times.js +38 -0
  44. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +23 -0
  45. package/js/browser-extensions/virtual-fs-cjs.js +3 -0
  46. package/js/columnCalculator.js +148 -0
  47. package/js/helpers/node.js +98 -0
  48. package/js/helpers/tools.js +46 -0
  49. package/js/helpers/variableType.js +59 -0
  50. package/js/index.js +15 -0
  51. package/js/qrEnc.js +721 -0
  52. package/js/standardPageSizes.js +56 -0
  53. package/js/tableLayouts.js +98 -0
  54. package/js/virtual-fs.js +60 -0
  55. package/package.json +25 -24
  56. package/src/DocMeasure.js +28 -7
  57. package/src/DocPreprocessor.js +25 -6
  58. package/src/DocumentContext.js +62 -33
  59. package/src/ElementWriter.js +30 -7
  60. package/src/LayoutBuilder.js +557 -120
  61. package/src/OutputDocument.js +23 -37
  62. package/src/OutputDocumentServer.js +6 -11
  63. package/src/PDFDocument.js +1 -1
  64. package/src/PageElementWriter.js +21 -2
  65. package/src/Printer.js +134 -131
  66. package/src/Renderer.js +13 -15
  67. package/src/SVGMeasure.js +2 -2
  68. package/src/StyleContextStack.js +7 -44
  69. package/src/TableProcessor.js +62 -22
  70. package/src/TextBreaker.js +24 -5
  71. package/src/TextInlines.js +1 -1
  72. package/src/URLResolver.js +24 -58
  73. package/src/base.js +1 -1
  74. package/src/browser-extensions/OutputDocumentBrowser.js +33 -71
  75. package/src/browser-extensions/index.js +3 -3
  76. package/src/browser-extensions/pdfMake.js +0 -14
  77. package/src/browser-extensions/standard-fonts/Courier.js +4 -4
  78. package/src/browser-extensions/standard-fonts/Helvetica.js +4 -4
  79. package/src/browser-extensions/standard-fonts/Symbol.js +1 -1
  80. package/src/browser-extensions/standard-fonts/Times.js +4 -4
  81. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +1 -1
  82. package/src/columnCalculator.js +24 -3
  83. package/src/helpers/tools.js +5 -0
  84. package/src/helpers/variableType.js +11 -0
  85. package/src/index.js +1 -1
  86. package/standard-fonts/Helvetica.js +0 -1
  87. package/src/browser-extensions/URLBrowserResolver.js +0 -84
@@ -0,0 +1,166 @@
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._textRef && item._textRef._textNodeRef.text) {
134
+ item.text = item._textRef._textNodeRef.text;
135
+ }
136
+ words = splitWords(item.text, noWrap, breakAll);
137
+ style = _StyleContextStack.default.copyStyle(item);
138
+ } else {
139
+ words = splitWords(item, noWrap, breakAll);
140
+ }
141
+ if (lastWord && words.length) {
142
+ let firstWord = getFirstWord(words, noWrap);
143
+ let wrapWords = splitWords(lastWord + firstWord, false);
144
+ if (wrapWords.length === 1) {
145
+ results[results.length - 1].noNewLine = true;
146
+ }
147
+ }
148
+ for (let i2 = 0, l2 = words.length; i2 < l2; i2++) {
149
+ let result = {
150
+ text: words[i2].text
151
+ };
152
+ if (words[i2].lineEnd) {
153
+ result.lineEnd = true;
154
+ }
155
+ _StyleContextStack.default.copyStyle(style, result);
156
+ results.push(result);
157
+ }
158
+ lastWord = null;
159
+ if (i + 1 < l) {
160
+ lastWord = getLastWord(words, noWrap);
161
+ }
162
+ }
163
+ return results;
164
+ }
165
+ }
166
+ var _default = exports.default = TextBreaker;
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ const groupDecorations = line => {
6
+ let groups = [];
7
+ let currentGroup = null;
8
+ for (let i = 0, l = line.inlines.length; i < l; i++) {
9
+ let inline = line.inlines[i];
10
+ let decoration = inline.decoration;
11
+ if (!decoration) {
12
+ currentGroup = null;
13
+ continue;
14
+ }
15
+ if (!Array.isArray(decoration)) {
16
+ decoration = [decoration];
17
+ }
18
+ let color = inline.decorationColor || inline.color || 'black';
19
+ let style = inline.decorationStyle || 'solid';
20
+ for (let ii = 0, ll = decoration.length; ii < ll; ii++) {
21
+ let decorationItem = decoration[ii];
22
+ if (!currentGroup || decorationItem !== currentGroup.decoration || style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor) {
23
+ currentGroup = {
24
+ line: line,
25
+ decoration: decorationItem,
26
+ decorationColor: color,
27
+ decorationStyle: style,
28
+ inlines: [inline]
29
+ };
30
+ groups.push(currentGroup);
31
+ } else {
32
+ currentGroup.inlines.push(inline);
33
+ }
34
+ }
35
+ }
36
+ return groups;
37
+ };
38
+ class TextDecorator {
39
+ constructor(pdfDocument) {
40
+ this.pdfDocument = pdfDocument;
41
+ }
42
+ drawBackground(line, x, y) {
43
+ let height = line.getHeight();
44
+ for (let i = 0, l = line.inlines.length; i < l; i++) {
45
+ let inline = line.inlines[i];
46
+ if (!inline.background) {
47
+ continue;
48
+ }
49
+ let color = inline.background;
50
+ let patternColor = this.pdfDocument.providePattern(inline.background);
51
+ if (patternColor !== null) {
52
+ color = patternColor;
53
+ }
54
+ let justifyShift = inline.justifyShift || 0;
55
+ this.pdfDocument.fillColor(color).rect(x + inline.x - justifyShift, y, inline.width + justifyShift, height).fill();
56
+ }
57
+ }
58
+ drawDecorations(line, x, y) {
59
+ let groups = groupDecorations(line);
60
+ for (let i = 0, l = groups.length; i < l; i++) {
61
+ this._drawDecoration(groups[i], x, y);
62
+ }
63
+ }
64
+ _drawDecoration(group, x, y) {
65
+ const maxInline = () => {
66
+ let max = 0;
67
+ for (let i = 0, l = group.inlines.length; i < l; i++) {
68
+ let inline = group.inlines[i];
69
+ max = inline.fontSize > max ? i : max;
70
+ }
71
+ return group.inlines[max];
72
+ };
73
+ const width = () => {
74
+ let sum = 0;
75
+ for (let i = 0, l = group.inlines.length; i < l; i++) {
76
+ let justifyShift = group.inlines[i].justifyShift || 0;
77
+ sum += group.inlines[i].width + justifyShift;
78
+ }
79
+ return sum;
80
+ };
81
+ let firstInline = group.inlines[0];
82
+ let biggerInline = maxInline();
83
+ let totalWidth = width();
84
+ let lineAscent = group.line.getAscenderHeight();
85
+ let ascent = biggerInline.font.ascender / 1000 * biggerInline.fontSize;
86
+ let height = biggerInline.height;
87
+ let descent = height - ascent;
88
+ let lw = 0.5 + Math.floor(Math.max(biggerInline.fontSize - 8, 0) / 2) * 0.12;
89
+ switch (group.decoration) {
90
+ case 'underline':
91
+ y += lineAscent + descent * 0.45;
92
+ break;
93
+ case 'overline':
94
+ y += lineAscent - ascent * 0.85;
95
+ break;
96
+ case 'lineThrough':
97
+ y += lineAscent - ascent * 0.25;
98
+ break;
99
+ default:
100
+ throw new Error(`Unknown decoration : ${group.decoration}`);
101
+ }
102
+ this.pdfDocument.save();
103
+ if (group.decorationStyle === 'double') {
104
+ let gap = Math.max(0.5, lw * 2);
105
+ 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();
106
+ } else if (group.decorationStyle === 'dashed') {
107
+ let nbDashes = Math.ceil(totalWidth / (3.96 + 2.84));
108
+ let rdx = x + firstInline.x;
109
+ this.pdfDocument.rect(rdx, y, totalWidth, lw).clip();
110
+ this.pdfDocument.fillColor(group.decorationColor);
111
+ for (let i = 0; i < nbDashes; i++) {
112
+ this.pdfDocument.rect(rdx, y - lw / 2, 3.96, lw).fill();
113
+ rdx += 3.96 + 2.84;
114
+ }
115
+ } else if (group.decorationStyle === 'dotted') {
116
+ let nbDots = Math.ceil(totalWidth / (lw * 3));
117
+ let rx = x + firstInline.x;
118
+ this.pdfDocument.rect(rx, y, totalWidth, lw).clip();
119
+ this.pdfDocument.fillColor(group.decorationColor);
120
+ for (let i = 0; i < nbDots; i++) {
121
+ this.pdfDocument.rect(rx, y - lw / 2, lw, lw).fill();
122
+ rx += lw * 3;
123
+ }
124
+ } else if (group.decorationStyle === 'wavy') {
125
+ let sh = 0.7,
126
+ sv = 1;
127
+ let nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
128
+ let rwx = x + firstInline.x - 1;
129
+ this.pdfDocument.rect(x + firstInline.x, y - sv, totalWidth, y + sv).clip();
130
+ this.pdfDocument.lineWidth(0.24);
131
+ this.pdfDocument.moveTo(rwx, y);
132
+ for (let i = 0; i < nbWaves; i++) {
133
+ 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);
134
+ rwx += sh * 6;
135
+ }
136
+ this.pdfDocument.stroke(group.decorationColor);
137
+ } else {
138
+ this.pdfDocument.fillColor(group.decorationColor).rect(x + firstInline.x, y - lw / 2, totalWidth, lw).fill();
139
+ }
140
+ this.pdfDocument.restore();
141
+ }
142
+ }
143
+ var _default = exports.default = TextDecorator;
@@ -0,0 +1,206 @@
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 breakedText = textBreaker.getBreaks(flattenedTextArray, styleContextStack);
62
+ let measuredText = this.measure(breakedText, 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.background = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'background', null);
110
+ item.link = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'link', null);
111
+ item.linkToPage = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'linkToPage', null);
112
+ item.linkToDestination = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'linkToDestination', null);
113
+ item.noWrap = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'noWrap', null);
114
+ item.opacity = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'opacity', 1);
115
+ item.sup = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'sup', false);
116
+ item.sub = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'sub', false);
117
+ if (item.sup || item.sub) {
118
+ // font size reduction taken from here: https://en.wikipedia.org/wiki/Subscript_and_superscript#Desktop_publishing
119
+ item.fontSize *= 0.58;
120
+ }
121
+ let lineHeight = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'lineHeight', 1);
122
+ item.width = this.widthOfText(item.text, item);
123
+ item.height = item.font.lineHeight(item.fontSize) * lineHeight;
124
+ if (!item.leadingCut) {
125
+ item.leadingCut = 0;
126
+ }
127
+ let preserveLeadingSpaces = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'preserveLeadingSpaces', false);
128
+ if (!preserveLeadingSpaces) {
129
+ let leadingSpaces = item.text.match(LEADING);
130
+ if (leadingSpaces) {
131
+ item.leadingCut += this.widthOfText(leadingSpaces[0], item);
132
+ }
133
+ }
134
+ item.trailingCut = 0;
135
+ let preserveTrailingSpaces = _StyleContextStack.default.getStyleProperty(item, styleContextStack, 'preserveTrailingSpaces', false);
136
+ if (!preserveTrailingSpaces) {
137
+ let trailingSpaces = item.text.match(TRAILING);
138
+ if (trailingSpaces) {
139
+ item.trailingCut = this.widthOfText(trailingSpaces[0], item);
140
+ }
141
+ }
142
+ }, this);
143
+ return array;
144
+ }
145
+
146
+ /**
147
+ * Width of text
148
+ *
149
+ * @param {string} text
150
+ * @param {object} inline
151
+ * @returns {number}
152
+ */
153
+ widthOfText(text, inline) {
154
+ return inline.font.widthOfString(text, inline.fontSize, inline.fontFeatures) + (inline.characterSpacing || 0) * (text.length - 1);
155
+ }
156
+
157
+ /**
158
+ * Returns size of the specified string (without breaking it) using the current style
159
+ *
160
+ * @param {string} text text to be measured
161
+ * @param {object} styleContextStack current style stack
162
+ * @returns {object} size of the specified string
163
+ */
164
+ sizeOfText(text, styleContextStack) {
165
+ //TODO: refactor - extract from measure
166
+ let fontName = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'font', 'Roboto');
167
+ let fontSize = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'fontSize', 12);
168
+ let fontFeatures = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'fontFeatures', null);
169
+ let bold = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'bold', false);
170
+ let italics = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'italics', false);
171
+ let lineHeight = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'lineHeight', 1);
172
+ let characterSpacing = _StyleContextStack.default.getStyleProperty({}, styleContextStack, 'characterSpacing', 0);
173
+ let font = this.pdfDocument.provideFont(fontName, bold, italics);
174
+ return {
175
+ width: this.widthOfText(text, {
176
+ font: font,
177
+ fontSize: fontSize,
178
+ characterSpacing: characterSpacing,
179
+ fontFeatures: fontFeatures
180
+ }),
181
+ height: font.lineHeight(fontSize) * lineHeight,
182
+ fontSize: fontSize,
183
+ lineHeight: lineHeight,
184
+ ascender: font.ascender / 1000 * fontSize,
185
+ descender: font.descender / 1000 * fontSize
186
+ };
187
+ }
188
+
189
+ /**
190
+ * Returns size of the specified rotated string (without breaking it) using the current style
191
+ *
192
+ * @param {string} text text to be measured
193
+ * @param {number} angle
194
+ * @param {object} styleContextStack current style stack
195
+ * @returns {object} size of the specified string
196
+ */
197
+ sizeOfRotatedText(text, angle, styleContextStack) {
198
+ let angleRad = angle * Math.PI / -180;
199
+ let size = this.sizeOfText(text, styleContextStack);
200
+ return {
201
+ width: Math.abs(size.height * Math.sin(angleRad)) + Math.abs(size.width * Math.cos(angleRad)),
202
+ height: Math.abs(size.width * Math.sin(angleRad)) + Math.abs(size.height * Math.cos(angleRad))
203
+ };
204
+ }
205
+ }
206
+ 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,52 @@
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
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
+ class pdfmake {
10
+ constructor() {
11
+ this.virtualfs = _virtualFs.default;
12
+ this.urlResolver = null;
13
+ }
14
+
15
+ /**
16
+ * @param {object} docDefinition
17
+ * @param {?object} options
18
+ * @returns {object}
19
+ */
20
+ createPdf(docDefinition, options = {}) {
21
+ options.progressCallback = this.progressCallback;
22
+ options.tableLayouts = this.tableLayouts;
23
+ let printer = new _Printer.default(this.fonts, this.virtualfs, this.urlResolver());
24
+ const pdfDocumentPromise = printer.createPdfKitDocument(docDefinition, options);
25
+ return this._transformToDocument(pdfDocumentPromise);
26
+ }
27
+ setProgressCallback(callback) {
28
+ this.progressCallback = callback;
29
+ }
30
+ addTableLayouts(tableLayouts) {
31
+ this.tableLayouts = (0, _tools.pack)(this.tableLayouts, tableLayouts);
32
+ }
33
+ setTableLayouts(tableLayouts) {
34
+ this.tableLayouts = tableLayouts;
35
+ }
36
+ clearTableLayouts() {
37
+ this.tableLayouts = {};
38
+ }
39
+ addFonts(fonts) {
40
+ this.fonts = (0, _tools.pack)(this.fonts, fonts);
41
+ }
42
+ setFonts(fonts) {
43
+ this.fonts = fonts;
44
+ }
45
+ clearFonts() {
46
+ this.fonts = {};
47
+ }
48
+ _transformToDocument(doc) {
49
+ return doc;
50
+ }
51
+ }
52
+ var _default = exports.default = pdfmake;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _OutputDocument = _interopRequireDefault(require("../OutputDocument"));
6
+ var _fileSaver = require("file-saver");
7
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
+ /**
9
+ * @returns {Window}
10
+ */
11
+ const openWindow = () => {
12
+ // we have to open the window immediately and store the reference
13
+ // otherwise popup blockers will stop us
14
+ let win = window.open('', '_blank');
15
+ if (win === null) {
16
+ throw new Error('Open PDF in new window blocked by browser');
17
+ }
18
+ return win;
19
+ };
20
+ class OutputDocumentBrowser extends _OutputDocument.default {
21
+ /**
22
+ * @returns {Promise<Blob>}
23
+ */
24
+ async getBlob() {
25
+ const buffer = await this.getBuffer();
26
+ return new Blob([buffer], {
27
+ type: 'application/pdf'
28
+ });
29
+ }
30
+
31
+ /**
32
+ * @param {string} filename
33
+ * @returns {Promise}
34
+ */
35
+ async download(filename = 'file.pdf') {
36
+ const blob = await this.getBlob();
37
+ (0, _fileSaver.saveAs)(blob, filename);
38
+ }
39
+
40
+ /**
41
+ * @param {Window} win
42
+ * @returns {Promise}
43
+ */
44
+ async open(win = null) {
45
+ if (!win) {
46
+ win = openWindow();
47
+ }
48
+ const blob = await this.getBlob();
49
+ try {
50
+ let urlCreator = window.URL || window.webkitURL;
51
+ let pdfUrl = urlCreator.createObjectURL(blob);
52
+ win.location.href = pdfUrl;
53
+
54
+ /* temporarily disabled
55
+ if (win === window) {
56
+ return;
57
+ } else {
58
+ setTimeout(() => {
59
+ if (win.window === null) { // is closed by AdBlock
60
+ window.location.href = pdfUrl; // open in actual window
61
+ }
62
+ return;
63
+ }, 500);
64
+ }
65
+ */
66
+ } finally {
67
+ win.close();
68
+ }
69
+ }
70
+
71
+ /**
72
+ * @param {Window} win
73
+ * @returns {Promise}
74
+ */
75
+ async print(win = null) {
76
+ const stream = await this.getStream();
77
+ stream.setOpenActionAsPrint();
78
+ await this.open(win);
79
+ }
80
+ }
81
+ var _default = exports.default = OutputDocumentBrowser;