pdfmake 0.3.0-beta.7 → 0.3.0-beta.8

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 (49) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/build/pdfmake.js +2654 -2525
  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/package.json +1 -1
  7. package/js/3rd-party/svg-to-pdfkit/source.js +0 -3626
  8. package/js/3rd-party/svg-to-pdfkit.js +0 -7
  9. package/js/DocMeasure.js +0 -627
  10. package/js/DocPreprocessor.js +0 -238
  11. package/js/DocumentContext.js +0 -265
  12. package/js/ElementWriter.js +0 -331
  13. package/js/LayoutBuilder.js +0 -694
  14. package/js/Line.js +0 -105
  15. package/js/OutputDocument.js +0 -76
  16. package/js/OutputDocumentServer.js +0 -27
  17. package/js/PDFDocument.js +0 -144
  18. package/js/PageElementWriter.js +0 -140
  19. package/js/PageSize.js +0 -74
  20. package/js/Printer.js +0 -291
  21. package/js/Renderer.js +0 -375
  22. package/js/SVGMeasure.js +0 -69
  23. package/js/StyleContextStack.js +0 -180
  24. package/js/TableProcessor.js +0 -511
  25. package/js/TextBreaker.js +0 -139
  26. package/js/TextDecorator.js +0 -143
  27. package/js/TextInlines.js +0 -206
  28. package/js/URLResolver.js +0 -73
  29. package/js/base.js +0 -52
  30. package/js/browser-extensions/OutputDocumentBrowser.js +0 -118
  31. package/js/browser-extensions/URLBrowserResolver.js +0 -76
  32. package/js/browser-extensions/fonts/Roboto.js +0 -38
  33. package/js/browser-extensions/index.js +0 -53
  34. package/js/browser-extensions/pdfMake.js +0 -15
  35. package/js/browser-extensions/standard-fonts/Courier.js +0 -38
  36. package/js/browser-extensions/standard-fonts/Helvetica.js +0 -38
  37. package/js/browser-extensions/standard-fonts/Symbol.js +0 -23
  38. package/js/browser-extensions/standard-fonts/Times.js +0 -38
  39. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +0 -23
  40. package/js/browser-extensions/virtual-fs-cjs.js +0 -3
  41. package/js/columnCalculator.js +0 -129
  42. package/js/helpers/node.js +0 -98
  43. package/js/helpers/tools.js +0 -40
  44. package/js/helpers/variableType.js +0 -47
  45. package/js/index.js +0 -15
  46. package/js/qrEnc.js +0 -721
  47. package/js/standardPageSizes.js +0 -56
  48. package/js/tableLayouts.js +0 -98
  49. package/js/virtual-fs.js +0 -60
@@ -1,143 +0,0 @@
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;
package/js/TextInlines.js DELETED
@@ -1,206 +0,0 @@
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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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} 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;
package/js/URLResolver.js DELETED
@@ -1,73 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.default = void 0;
5
- var _http = _interopRequireDefault(require("http"));
6
- var _https = _interopRequireDefault(require("https"));
7
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
- const fetchUrl = (url, headers = {}) => {
9
- return new Promise((resolve, reject) => {
10
- const parsedUrl = new URL(url);
11
- const h = parsedUrl.protocol === 'https:' ? _https.default : _http.default;
12
- let options = {
13
- headers: headers
14
- };
15
- h.get(url, options, res => {
16
- if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
17
- // redirect url
18
- fetchUrl(res.headers.location).then(buffer => {
19
- resolve(buffer);
20
- }, result => {
21
- reject(result);
22
- });
23
- return;
24
- }
25
- const ok = res.statusCode >= 200 && res.statusCode < 300;
26
- if (!ok) {
27
- reject(new TypeError(`Failed to fetch (status code: ${res.statusCode}, url: "${url}")`));
28
- }
29
- const chunks = [];
30
- res.on('end', () => resolve(Buffer.concat(chunks)));
31
- res.on('data', d => chunks.push(d));
32
- }).on('error', reject);
33
- });
34
- };
35
- class URLResolver {
36
- constructor(fs) {
37
- this.fs = fs;
38
- this.resolving = {};
39
- }
40
- resolve(url, headers = {}) {
41
- if (!this.resolving[url]) {
42
- this.resolving[url] = new Promise((resolve, reject) => {
43
- if (url.toLowerCase().indexOf('https://') === 0 || url.toLowerCase().indexOf('http://') === 0) {
44
- if (this.fs.existsSync(url)) {
45
- // url was downloaded earlier
46
- resolve();
47
- } else {
48
- fetchUrl(url, headers).then(buffer => {
49
- this.fs.writeFileSync(url, buffer);
50
- resolve();
51
- }, result => {
52
- reject(result);
53
- });
54
- }
55
- } else {
56
- // cannot be resolved
57
- resolve();
58
- }
59
- });
60
- }
61
- return this.resolving[url];
62
- }
63
- resolved() {
64
- return new Promise((resolve, reject) => {
65
- Promise.all(Object.values(this.resolving)).then(() => {
66
- resolve();
67
- }, result => {
68
- reject(result);
69
- });
70
- });
71
- }
72
- }
73
- var _default = exports.default = URLResolver;
package/js/base.js DELETED
@@ -1,52 +0,0 @@
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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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;
@@ -1,118 +0,0 @@
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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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
- getBlob() {
25
- return new Promise((resolve, reject) => {
26
- this.getBuffer().then(buffer => {
27
- try {
28
- let blob = new Blob([buffer], {
29
- type: 'application/pdf'
30
- });
31
- resolve(blob);
32
- } catch (e) {
33
- reject(e);
34
- }
35
- }, result => {
36
- reject(result);
37
- });
38
- });
39
- }
40
-
41
- /**
42
- * @param {string} filename
43
- * @returns {Promise}
44
- */
45
- download(filename = 'file.pdf') {
46
- return new Promise((resolve, reject) => {
47
- this.getBlob().then(blob => {
48
- try {
49
- (0, _fileSaver.saveAs)(blob, filename);
50
- resolve();
51
- } catch (e) {
52
- reject(e);
53
- }
54
- }, result => {
55
- reject(result);
56
- });
57
- });
58
- }
59
-
60
- /**
61
- * @param {Window} win
62
- * @returns {Promise}
63
- */
64
- open(win = null) {
65
- return new Promise((resolve, reject) => {
66
- if (!win) {
67
- win = openWindow();
68
- }
69
- this.getBlob().then(blob => {
70
- try {
71
- let urlCreator = window.URL || window.webkitURL;
72
- let pdfUrl = urlCreator.createObjectURL(blob);
73
- win.location.href = pdfUrl;
74
-
75
- //
76
- resolve();
77
- /* temporarily disabled
78
- if (win === window) {
79
- resolve();
80
- } else {
81
- setTimeout(() => {
82
- if (win.window === null) { // is closed by AdBlock
83
- window.location.href = pdfUrl; // open in actual window
84
- }
85
- resolve();
86
- }, 500);
87
- }
88
- */
89
- } catch (e) {
90
- win.close();
91
- reject(e);
92
- }
93
- }, result => {
94
- reject(result);
95
- });
96
- });
97
- }
98
-
99
- /**
100
- * @param {Window} win
101
- * @returns {Promise}
102
- */
103
- print(win = null) {
104
- return new Promise((resolve, reject) => {
105
- this.getStream().then(stream => {
106
- stream.setOpenActionAsPrint();
107
- return this.open(win).then(() => {
108
- resolve();
109
- }, result => {
110
- reject(result);
111
- });
112
- }, result => {
113
- reject(result);
114
- });
115
- });
116
- }
117
- }
118
- var _default = exports.default = OutputDocumentBrowser;
@@ -1,76 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.default = void 0;
5
- const fetchUrl = (url, headers = {}) => {
6
- return new Promise((resolve, reject) => {
7
- const xhr = new XMLHttpRequest();
8
- xhr.open('GET', url, true);
9
- for (let headerName in headers) {
10
- xhr.setRequestHeader(headerName, headers[headerName]);
11
- }
12
- xhr.responseType = 'arraybuffer';
13
- xhr.onreadystatechange = () => {
14
- if (xhr.readyState !== 4) {
15
- return;
16
- }
17
- const ok = xhr.status >= 200 && xhr.status < 300;
18
- if (!ok) {
19
- setTimeout(() => {
20
- reject(new TypeError(`Failed to fetch (url: "${url}")`));
21
- }, 0);
22
- }
23
- };
24
- xhr.onload = () => {
25
- const ok = xhr.status >= 200 && xhr.status < 300;
26
- if (ok) {
27
- resolve(xhr.response);
28
- }
29
- };
30
- xhr.onerror = () => {
31
- setTimeout(() => {
32
- reject(new TypeError(`Network request failed (url: "${url}")`));
33
- }, 0);
34
- };
35
- xhr.ontimeout = () => {
36
- setTimeout(() => {
37
- reject(new TypeError(`Network request failed (url: "${url}")`));
38
- }, 0);
39
- };
40
- xhr.send();
41
- });
42
- };
43
- class URLBrowserResolver {
44
- constructor(fs) {
45
- this.fs = fs;
46
- this.resolving = {};
47
- }
48
- resolve(url, headers = {}) {
49
- if (!this.resolving[url]) {
50
- this.resolving[url] = new Promise((resolve, reject) => {
51
- if (url.toLowerCase().indexOf('https://') === 0 || url.toLowerCase().indexOf('http://') === 0) {
52
- fetchUrl(url, headers).then(buffer => {
53
- this.fs.writeFileSync(url, buffer);
54
- resolve();
55
- }, result => {
56
- reject(result);
57
- });
58
- } else {
59
- // cannot be resolved
60
- resolve();
61
- }
62
- });
63
- }
64
- return this.resolving[url];
65
- }
66
- resolved() {
67
- return new Promise((resolve, reject) => {
68
- Promise.all(Object.values(this.resolving)).then(() => {
69
- resolve();
70
- }, result => {
71
- reject(result);
72
- });
73
- });
74
- }
75
- }
76
- var _default = exports.default = URLBrowserResolver;