pdfmake 0.2.13 → 0.3.0-beta.10
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.
- package/CHANGELOG.md +23 -41
- package/README.md +11 -7
- package/build/pdfmake.js +22291 -23202
- package/build/pdfmake.js.map +1 -1
- package/build/pdfmake.min.js +2 -2
- package/build/pdfmake.min.js.map +1 -1
- package/build/standard-fonts/Courier.js +27 -0
- package/build/standard-fonts/Helvetica.js +27 -0
- package/build/standard-fonts/Symbol.js +21 -0
- package/build/standard-fonts/Times.js +27 -0
- package/build/standard-fonts/ZapfDingbats.js +21 -0
- package/build/vfs_fonts.js +2 -2
- package/build-vfs.js +2 -2
- package/eslint.config.mjs +52 -0
- package/fonts/Roboto/Roboto-Italic.ttf +0 -0
- package/fonts/Roboto/Roboto-Medium.ttf +0 -0
- package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
- package/fonts/Roboto/Roboto-Regular.ttf +0 -0
- package/fonts/Roboto.js +8 -0
- package/js/3rd-party/svg-to-pdfkit/source.js +3626 -0
- package/js/3rd-party/svg-to-pdfkit.js +7 -0
- package/js/DocMeasure.js +626 -0
- package/js/DocPreprocessor.js +238 -0
- package/js/DocumentContext.js +288 -0
- package/js/ElementWriter.js +342 -0
- package/js/LayoutBuilder.js +881 -0
- package/js/Line.js +105 -0
- package/js/OutputDocument.js +76 -0
- package/js/OutputDocumentServer.js +27 -0
- package/js/PDFDocument.js +144 -0
- package/js/PageElementWriter.js +140 -0
- package/js/PageSize.js +74 -0
- package/js/Printer.js +291 -0
- package/js/Renderer.js +375 -0
- package/js/SVGMeasure.js +69 -0
- package/js/StyleContextStack.js +164 -0
- package/js/TableProcessor.js +524 -0
- package/js/TextBreaker.js +139 -0
- package/js/TextDecorator.js +143 -0
- package/js/TextInlines.js +206 -0
- package/js/URLResolver.js +73 -0
- package/js/base.js +52 -0
- package/js/browser-extensions/OutputDocumentBrowser.js +118 -0
- package/js/browser-extensions/URLBrowserResolver.js +76 -0
- package/js/browser-extensions/fonts/Roboto.js +38 -0
- package/js/browser-extensions/index.js +53 -0
- package/js/browser-extensions/pdfMake.js +3 -0
- package/js/browser-extensions/standard-fonts/Courier.js +38 -0
- package/js/browser-extensions/standard-fonts/Helvetica.js +38 -0
- package/js/browser-extensions/standard-fonts/Symbol.js +23 -0
- package/js/browser-extensions/standard-fonts/Times.js +38 -0
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +23 -0
- package/js/browser-extensions/virtual-fs-cjs.js +3 -0
- package/js/columnCalculator.js +148 -0
- package/js/helpers/node.js +98 -0
- package/js/helpers/tools.js +40 -0
- package/js/helpers/variableType.js +59 -0
- package/js/index.js +15 -0
- package/js/qrEnc.js +721 -0
- package/js/standardPageSizes.js +56 -0
- package/js/tableLayouts.js +98 -0
- package/js/virtual-fs.js +60 -0
- package/package.json +34 -28
- package/src/3rd-party/svg-to-pdfkit.js +2 -2
- package/src/DocMeasure.js +707 -0
- package/src/DocPreprocessor.js +264 -0
- package/src/DocumentContext.js +324 -0
- package/src/ElementWriter.js +405 -0
- package/src/LayoutBuilder.js +997 -0
- package/src/Line.js +114 -0
- package/src/OutputDocument.js +78 -0
- package/src/OutputDocumentServer.js +26 -0
- package/src/PDFDocument.js +174 -0
- package/src/PageElementWriter.js +160 -0
- package/src/PageSize.js +53 -0
- package/src/Printer.js +306 -0
- package/src/Renderer.js +405 -0
- package/src/SVGMeasure.js +79 -0
- package/src/StyleContextStack.js +175 -0
- package/src/TableProcessor.js +580 -0
- package/src/TextBreaker.js +149 -0
- package/src/TextDecorator.js +161 -0
- package/src/TextInlines.js +223 -0
- package/src/URLResolver.js +77 -0
- package/src/base.js +61 -0
- package/src/browser-extensions/OutputDocumentBrowser.js +117 -0
- package/src/browser-extensions/URLBrowserResolver.js +45 -57
- package/src/browser-extensions/fonts/Roboto.js +27 -0
- package/src/browser-extensions/index.js +55 -0
- package/src/browser-extensions/pdfMake.js +1 -329
- package/src/browser-extensions/standard-fonts/Courier.js +27 -0
- package/src/browser-extensions/standard-fonts/Helvetica.js +27 -0
- package/src/browser-extensions/standard-fonts/Symbol.js +21 -0
- package/src/browser-extensions/standard-fonts/Times.js +27 -0
- package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -0
- package/src/browser-extensions/virtual-fs-cjs.js +1 -0
- package/src/columnCalculator.js +35 -38
- package/src/helpers/node.js +110 -0
- package/src/helpers/tools.js +39 -0
- package/src/helpers/variableType.js +50 -0
- package/src/index.js +16 -0
- package/src/qrEnc.js +15 -10
- package/src/standardPageSizes.js +1 -3
- package/src/tableLayouts.js +100 -0
- package/src/virtual-fs.js +66 -0
- package/standard-fonts/Courier.js +8 -0
- package/standard-fonts/Helvetica.js +9 -0
- package/standard-fonts/Symbol.js +5 -0
- package/standard-fonts/Times.js +8 -0
- package/standard-fonts/ZapfDingbats.js +5 -0
- package/.idea/codeStyles/Project.xml +0 -7
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/pdfmake.iml +0 -11
- package/.idea/vcs.xml +0 -6
- package/src/browser-extensions/virtual-fs.js +0 -55
- package/src/docMeasure.js +0 -810
- package/src/docPreprocessor.js +0 -255
- package/src/documentContext.js +0 -328
- package/src/elementWriter.js +0 -333
- package/src/fontProvider.js +0 -68
- package/src/helpers.js +0 -138
- package/src/imageMeasure.js +0 -55
- package/src/layoutBuilder.js +0 -989
- package/src/line.js +0 -91
- package/src/pageElementWriter.js +0 -174
- package/src/pdfKitEngine.js +0 -21
- package/src/printer.js +0 -710
- package/src/styleContextStack.js +0 -138
- package/src/svgMeasure.js +0 -70
- package/src/tableProcessor.js +0 -584
- package/src/textDecorator.js +0 -157
- package/src/textTools.js +0 -373
- package/src/traversalTracker.js +0 -47
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import OutputDocument from '../OutputDocument';
|
|
2
|
+
import { saveAs } from 'file-saver';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @returns {Window}
|
|
6
|
+
*/
|
|
7
|
+
const openWindow = () => {
|
|
8
|
+
// we have to open the window immediately and store the reference
|
|
9
|
+
// otherwise popup blockers will stop us
|
|
10
|
+
let win = window.open('', '_blank');
|
|
11
|
+
if (win === null) {
|
|
12
|
+
throw new Error('Open PDF in new window blocked by browser');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return win;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
class OutputDocumentBrowser extends OutputDocument {
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @returns {Promise<Blob>}
|
|
22
|
+
*/
|
|
23
|
+
getBlob() {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
this.getBuffer().then(buffer => {
|
|
26
|
+
try {
|
|
27
|
+
let blob = new Blob([buffer], { type: 'application/pdf' });
|
|
28
|
+
resolve(blob);
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
}, result => {
|
|
33
|
+
reject(result);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {string} filename
|
|
40
|
+
* @returns {Promise}
|
|
41
|
+
*/
|
|
42
|
+
download(filename = 'file.pdf') {
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
this.getBlob().then(blob => {
|
|
45
|
+
try {
|
|
46
|
+
saveAs(blob, filename);
|
|
47
|
+
resolve();
|
|
48
|
+
} catch (e) {
|
|
49
|
+
reject(e);
|
|
50
|
+
}
|
|
51
|
+
}, result => {
|
|
52
|
+
reject(result);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {Window} win
|
|
59
|
+
* @returns {Promise}
|
|
60
|
+
*/
|
|
61
|
+
open(win = null) {
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
if (!win) {
|
|
64
|
+
win = openWindow();
|
|
65
|
+
}
|
|
66
|
+
this.getBlob().then(blob => {
|
|
67
|
+
try {
|
|
68
|
+
let urlCreator = window.URL || window.webkitURL;
|
|
69
|
+
let pdfUrl = urlCreator.createObjectURL(blob);
|
|
70
|
+
win.location.href = pdfUrl;
|
|
71
|
+
|
|
72
|
+
//
|
|
73
|
+
resolve();
|
|
74
|
+
/* temporarily disabled
|
|
75
|
+
if (win === window) {
|
|
76
|
+
resolve();
|
|
77
|
+
} else {
|
|
78
|
+
setTimeout(() => {
|
|
79
|
+
if (win.window === null) { // is closed by AdBlock
|
|
80
|
+
window.location.href = pdfUrl; // open in actual window
|
|
81
|
+
}
|
|
82
|
+
resolve();
|
|
83
|
+
}, 500);
|
|
84
|
+
}
|
|
85
|
+
*/
|
|
86
|
+
} catch (e) {
|
|
87
|
+
win.close();
|
|
88
|
+
reject(e);
|
|
89
|
+
}
|
|
90
|
+
}, result => {
|
|
91
|
+
reject(result);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {Window} win
|
|
98
|
+
* @returns {Promise}
|
|
99
|
+
*/
|
|
100
|
+
print(win = null) {
|
|
101
|
+
return new Promise((resolve, reject) => {
|
|
102
|
+
this.getStream().then(stream => {
|
|
103
|
+
stream.setOpenActionAsPrint();
|
|
104
|
+
return this.open(win).then(() => {
|
|
105
|
+
resolve();
|
|
106
|
+
}, result => {
|
|
107
|
+
reject(result);
|
|
108
|
+
});
|
|
109
|
+
}, result => {
|
|
110
|
+
reject(result);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export default OutputDocumentBrowser;
|
|
@@ -1,49 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
if (typeof window !== 'undefined' && !window.Promise) {
|
|
5
|
-
require('core-js/features/promise');
|
|
6
|
-
}
|
|
7
|
-
require('core-js/es/object/values');
|
|
8
|
-
|
|
9
|
-
var fetchUrl = function (url, headers) {
|
|
10
|
-
return new Promise(function (resolve, reject) {
|
|
11
|
-
var xhr = new XMLHttpRequest();
|
|
1
|
+
const fetchUrl = (url, headers = {}) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
const xhr = new XMLHttpRequest();
|
|
12
4
|
xhr.open('GET', url, true);
|
|
13
|
-
for (
|
|
5
|
+
for (let headerName in headers) {
|
|
14
6
|
xhr.setRequestHeader(headerName, headers[headerName]);
|
|
15
7
|
}
|
|
16
8
|
xhr.responseType = 'arraybuffer';
|
|
17
9
|
|
|
18
|
-
xhr.onreadystatechange =
|
|
10
|
+
xhr.onreadystatechange = () => {
|
|
19
11
|
if (xhr.readyState !== 4) {
|
|
20
12
|
return;
|
|
21
13
|
}
|
|
22
14
|
|
|
23
|
-
|
|
15
|
+
const ok = xhr.status >= 200 && xhr.status < 300;
|
|
24
16
|
if (!ok) {
|
|
25
|
-
setTimeout(
|
|
26
|
-
reject(new TypeError(
|
|
17
|
+
setTimeout(() => {
|
|
18
|
+
reject(new TypeError(`Failed to fetch (url: "${url}")`));
|
|
27
19
|
}, 0);
|
|
28
20
|
}
|
|
29
21
|
};
|
|
30
22
|
|
|
31
|
-
xhr.onload =
|
|
32
|
-
|
|
23
|
+
xhr.onload = () => {
|
|
24
|
+
const ok = xhr.status >= 200 && xhr.status < 300;
|
|
33
25
|
if (ok) {
|
|
34
26
|
resolve(xhr.response);
|
|
35
27
|
}
|
|
36
28
|
};
|
|
37
29
|
|
|
38
|
-
xhr.onerror =
|
|
39
|
-
setTimeout(
|
|
40
|
-
reject(new TypeError(
|
|
30
|
+
xhr.onerror = () => {
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
reject(new TypeError(`Network request failed (url: "${url}")`));
|
|
41
33
|
}, 0);
|
|
42
34
|
};
|
|
43
35
|
|
|
44
|
-
xhr.ontimeout =
|
|
45
|
-
setTimeout(
|
|
46
|
-
reject(new TypeError(
|
|
36
|
+
xhr.ontimeout = () => {
|
|
37
|
+
setTimeout(() => {
|
|
38
|
+
reject(new TypeError(`Network request failed (url: "${url}")`));
|
|
47
39
|
}, 0);
|
|
48
40
|
};
|
|
49
41
|
|
|
@@ -51,46 +43,42 @@ var fetchUrl = function (url, headers) {
|
|
|
51
43
|
});
|
|
52
44
|
};
|
|
53
45
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
46
|
+
class URLBrowserResolver {
|
|
47
|
+
constructor(fs) {
|
|
48
|
+
this.fs = fs;
|
|
49
|
+
this.resolving = {};
|
|
50
|
+
}
|
|
58
51
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
// url was downloaded earlier
|
|
66
|
-
resolve();
|
|
67
|
-
} else {
|
|
68
|
-
fetchUrl(url, headers).then(function (buffer) {
|
|
69
|
-
_this.fs.writeFileSync(url, buffer);
|
|
52
|
+
resolve(url, headers = {}) {
|
|
53
|
+
if (!this.resolving[url]) {
|
|
54
|
+
this.resolving[url] = new Promise((resolve, reject) => {
|
|
55
|
+
if (url.toLowerCase().indexOf('https://') === 0 || url.toLowerCase().indexOf('http://') === 0) {
|
|
56
|
+
fetchUrl(url, headers).then(buffer => {
|
|
57
|
+
this.fs.writeFileSync(url, buffer);
|
|
70
58
|
resolve();
|
|
71
|
-
},
|
|
59
|
+
}, result => {
|
|
72
60
|
reject(result);
|
|
73
61
|
});
|
|
62
|
+
} else {
|
|
63
|
+
// cannot be resolved
|
|
64
|
+
resolve();
|
|
74
65
|
}
|
|
75
|
-
}
|
|
76
|
-
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return this.resolving[url];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
resolved() {
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
Promise.all(Object.values(this.resolving)).then(() => {
|
|
77
75
|
resolve();
|
|
78
|
-
}
|
|
76
|
+
}, result => {
|
|
77
|
+
reject(result);
|
|
78
|
+
});
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
URLBrowserResolver.prototype.resolved = function () {
|
|
86
|
-
var _this = this;
|
|
87
|
-
return new Promise(function (resolve, reject) {
|
|
88
|
-
Promise.all(Object.values(_this.resolving)).then(function () {
|
|
89
|
-
resolve();
|
|
90
|
-
}, function (result) {
|
|
91
|
-
reject(result);
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
};
|
|
82
|
+
}
|
|
95
83
|
|
|
96
|
-
|
|
84
|
+
export default URLBrowserResolver;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var fs = require('fs');
|
|
2
|
+
|
|
3
|
+
var fontContainer = {
|
|
4
|
+
vfs: {
|
|
5
|
+
'Roboto-Regular.ttf': { data: fs.readFileSync(__dirname + '/../../../fonts/Roboto/Roboto-Regular.ttf', 'base64'), encoding: 'base64' },
|
|
6
|
+
'Roboto-Medium.ttf': { data: fs.readFileSync(__dirname + '/../../../fonts/Roboto/Roboto-Medium.ttf', 'base64'), encoding: 'base64' },
|
|
7
|
+
'Roboto-Italic.ttf': { data: fs.readFileSync(__dirname + '/../../../fonts/Roboto/Roboto-Italic.ttf', 'base64'), encoding: 'base64' },
|
|
8
|
+
'Roboto-MediumItalic.ttf': { data: fs.readFileSync(__dirname + '/../../../fonts/Roboto/Roboto-MediumItalic.ttf', 'base64'), encoding: 'base64' }
|
|
9
|
+
},
|
|
10
|
+
fonts: {
|
|
11
|
+
Roboto: {
|
|
12
|
+
normal: 'Roboto-Regular.ttf',
|
|
13
|
+
bold: 'Roboto-Medium.ttf',
|
|
14
|
+
italics: 'Roboto-Italic.ttf',
|
|
15
|
+
bolditalics: 'Roboto-MediumItalic.ttf'
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var _global = typeof window === 'object' ? window : typeof global === 'object' ? global : typeof self === 'object' ? self : this;
|
|
21
|
+
if (typeof _global.pdfMake !== 'undefined' && typeof _global.pdfMake.addFontContainer !== 'undefined') {
|
|
22
|
+
_global.pdfMake.addFontContainer(fontContainer);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typeof module !== 'undefined') {
|
|
26
|
+
module.exports = fontContainer;
|
|
27
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import pdfmakeBase from '../base';
|
|
2
|
+
import OutputDocumentBrowser from './OutputDocumentBrowser';
|
|
3
|
+
import URLBrowserResolver from './URLBrowserResolver';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import configurator from 'core-js/configurator';
|
|
6
|
+
|
|
7
|
+
// core-js: Polyfills will be used only if natives completely unavailable.
|
|
8
|
+
configurator({
|
|
9
|
+
useNative: ['Promise']
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
let defaultClientFonts = {
|
|
13
|
+
Roboto: {
|
|
14
|
+
normal: 'Roboto-Regular.ttf',
|
|
15
|
+
bold: 'Roboto-Medium.ttf',
|
|
16
|
+
italics: 'Roboto-Italic.ttf',
|
|
17
|
+
bolditalics: 'Roboto-MediumItalic.ttf'
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
class pdfmake extends pdfmakeBase {
|
|
22
|
+
constructor() {
|
|
23
|
+
super();
|
|
24
|
+
this.urlResolver = new URLBrowserResolver(this.virtualfs);
|
|
25
|
+
this.fonts = defaultClientFonts;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
addFontContainer(fontContainer) {
|
|
29
|
+
this.addVirtualFileSystem(fontContainer.vfs);
|
|
30
|
+
this.addFonts(fontContainer.fonts);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
addVirtualFileSystem(vfs) {
|
|
34
|
+
for (let key in vfs) {
|
|
35
|
+
if (vfs.hasOwnProperty(key)) {
|
|
36
|
+
let data;
|
|
37
|
+
let encoding;
|
|
38
|
+
if (typeof vfs[key] === 'object') {
|
|
39
|
+
data = vfs[key].data;
|
|
40
|
+
encoding = vfs[key].encoding || 'base64';
|
|
41
|
+
} else {
|
|
42
|
+
data = vfs[key];
|
|
43
|
+
encoding = 'base64';
|
|
44
|
+
}
|
|
45
|
+
fs.writeFileSync(key, data, encoding);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_transformToDocument(doc) {
|
|
51
|
+
return new OutputDocumentBrowser(doc);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export default new pdfmake();
|
|
@@ -1,329 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var isFunction = require('../helpers').isFunction;
|
|
4
|
-
var isUndefined = require('../helpers').isUndefined;
|
|
5
|
-
var isNull = require('../helpers').isNull;
|
|
6
|
-
var FileSaver = require('file-saver');
|
|
7
|
-
var saveAs = FileSaver.saveAs;
|
|
8
|
-
|
|
9
|
-
var defaultClientFonts = {
|
|
10
|
-
Roboto: {
|
|
11
|
-
normal: 'Roboto-Regular.ttf',
|
|
12
|
-
bold: 'Roboto-Medium.ttf',
|
|
13
|
-
italics: 'Roboto-Italic.ttf',
|
|
14
|
-
bolditalics: 'Roboto-MediumItalic.ttf'
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
function Document(docDefinition, tableLayouts, fonts, vfs) {
|
|
19
|
-
this.docDefinition = docDefinition;
|
|
20
|
-
this.tableLayouts = tableLayouts || null;
|
|
21
|
-
this.fonts = fonts || defaultClientFonts;
|
|
22
|
-
this.vfs = vfs;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function canCreatePdf() {
|
|
26
|
-
// Ensure the browser provides the level of support needed
|
|
27
|
-
try {
|
|
28
|
-
var arr = new Uint8Array(1);
|
|
29
|
-
var proto = { foo: function () { return 42; } };
|
|
30
|
-
Object.setPrototypeOf(proto, Uint8Array.prototype);
|
|
31
|
-
Object.setPrototypeOf(arr, proto);
|
|
32
|
-
return arr.foo() === 42;
|
|
33
|
-
} catch (e) {
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
Document.prototype._createDoc = function (options, cb) {
|
|
39
|
-
var getExtendedUrl = function (url) {
|
|
40
|
-
if (typeof url === 'object') {
|
|
41
|
-
return { url: url.url, headers: url.headers };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return { url: url, headers: {} };
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
options = options || {};
|
|
48
|
-
if (this.tableLayouts) {
|
|
49
|
-
options.tableLayouts = this.tableLayouts;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
var PdfPrinter = require('../printer');
|
|
53
|
-
|
|
54
|
-
var printer = new PdfPrinter(this.fonts);
|
|
55
|
-
require('fs').bindFS(this.vfs); // bind virtual file system to file system
|
|
56
|
-
|
|
57
|
-
if (!isFunction(cb)) {
|
|
58
|
-
var doc = printer.createPdfKitDocument(this.docDefinition, options);
|
|
59
|
-
|
|
60
|
-
return doc;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
var URLBrowserResolver = require('./URLBrowserResolver');
|
|
64
|
-
var urlResolver = new URLBrowserResolver(require('fs'));
|
|
65
|
-
|
|
66
|
-
for (var font in this.fonts) {
|
|
67
|
-
if (this.fonts.hasOwnProperty(font)) {
|
|
68
|
-
if (this.fonts[font].normal) {
|
|
69
|
-
if (Array.isArray(this.fonts[font].normal)) { // TrueType Collection
|
|
70
|
-
var url = getExtendedUrl(this.fonts[font].normal[0]);
|
|
71
|
-
urlResolver.resolve(url.url, url.headers);
|
|
72
|
-
this.fonts[font].normal[0] = url.url;
|
|
73
|
-
} else {
|
|
74
|
-
var url = getExtendedUrl(this.fonts[font].normal);
|
|
75
|
-
urlResolver.resolve(url.url, url.headers);
|
|
76
|
-
this.fonts[font].normal = url.url;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (this.fonts[font].bold) {
|
|
80
|
-
if (Array.isArray(this.fonts[font].bold)) { // TrueType Collection
|
|
81
|
-
var url = getExtendedUrl(this.fonts[font].bold[0]);
|
|
82
|
-
urlResolver.resolve(url.url, url.headers);
|
|
83
|
-
this.fonts[font].bold[0] = url.url;
|
|
84
|
-
} else {
|
|
85
|
-
var url = getExtendedUrl(this.fonts[font].bold);
|
|
86
|
-
urlResolver.resolve(url.url, url.headers);
|
|
87
|
-
this.fonts[font].bold = url.url;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
if (this.fonts[font].italics) {
|
|
91
|
-
if (Array.isArray(this.fonts[font].italics)) { // TrueType Collection
|
|
92
|
-
var url = getExtendedUrl(this.fonts[font].italics[0]);
|
|
93
|
-
urlResolver.resolve(url.url, url.headers);
|
|
94
|
-
this.fonts[font].italics[0] = url.url;
|
|
95
|
-
} else {
|
|
96
|
-
var url = getExtendedUrl(this.fonts[font].italics);
|
|
97
|
-
urlResolver.resolve(url.url, url.headers);
|
|
98
|
-
this.fonts[font].italics = url.url;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (this.fonts[font].bolditalics) {
|
|
102
|
-
if (Array.isArray(this.fonts[font].bolditalics)) { // TrueType Collection
|
|
103
|
-
var url = getExtendedUrl(this.fonts[font].bolditalics[0]);
|
|
104
|
-
urlResolver.resolve(url.url, url.headers);
|
|
105
|
-
this.fonts[font].bolditalics[0] = url.url;
|
|
106
|
-
} else {
|
|
107
|
-
var url = getExtendedUrl(this.fonts[font].bolditalics);
|
|
108
|
-
urlResolver.resolve(url.url, url.headers);
|
|
109
|
-
this.fonts[font].bolditalics = url.url;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (this.docDefinition.images) {
|
|
116
|
-
for (var image in this.docDefinition.images) {
|
|
117
|
-
if (this.docDefinition.images.hasOwnProperty(image)) {
|
|
118
|
-
var url = getExtendedUrl(this.docDefinition.images[image]);
|
|
119
|
-
urlResolver.resolve(url.url, url.headers);
|
|
120
|
-
this.docDefinition.images[image] = url.url;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
var _this = this;
|
|
126
|
-
|
|
127
|
-
urlResolver.resolved().then(function () {
|
|
128
|
-
var doc = printer.createPdfKitDocument(_this.docDefinition, options);
|
|
129
|
-
|
|
130
|
-
cb(doc);
|
|
131
|
-
}, function (result) {
|
|
132
|
-
throw result;
|
|
133
|
-
});
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
Document.prototype._flushDoc = function (doc, callback) {
|
|
137
|
-
var chunks = [];
|
|
138
|
-
var result;
|
|
139
|
-
|
|
140
|
-
doc.on('readable', function () {
|
|
141
|
-
var chunk;
|
|
142
|
-
while ((chunk = doc.read(9007199254740991)) !== null) {
|
|
143
|
-
chunks.push(chunk);
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
doc.on('end', function () {
|
|
147
|
-
result = Buffer.concat(chunks);
|
|
148
|
-
callback(result, doc._pdfMakePages);
|
|
149
|
-
});
|
|
150
|
-
doc.end();
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
Document.prototype._getPages = function (options, cb) {
|
|
154
|
-
if (!cb) {
|
|
155
|
-
throw '_getPages is an async method and needs a callback argument';
|
|
156
|
-
}
|
|
157
|
-
var _this = this;
|
|
158
|
-
|
|
159
|
-
this._createDoc(options, function (doc) {
|
|
160
|
-
_this._flushDoc(doc, function (ignoreBuffer, pages) {
|
|
161
|
-
cb(pages);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
Document.prototype._bufferToBlob = function (buffer) {
|
|
167
|
-
var blob;
|
|
168
|
-
try {
|
|
169
|
-
blob = new Blob([buffer], { type: 'application/pdf' });
|
|
170
|
-
} catch (e) {
|
|
171
|
-
// Old browser which can't handle it without making it an byte array (ie10)
|
|
172
|
-
if (e.name === 'InvalidStateError') {
|
|
173
|
-
var byteArray = new Uint8Array(buffer);
|
|
174
|
-
blob = new Blob([byteArray.buffer], { type: 'application/pdf' });
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (!blob) {
|
|
179
|
-
throw 'Could not generate blob';
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return blob;
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
Document.prototype._openWindow = function () {
|
|
186
|
-
// we have to open the window immediately and store the reference
|
|
187
|
-
// otherwise popup blockers will stop us
|
|
188
|
-
var win = window.open('', '_blank');
|
|
189
|
-
if (win === null) {
|
|
190
|
-
throw 'Open PDF in new window blocked by browser';
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return win;
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
Document.prototype._openPdf = function (options, win) {
|
|
197
|
-
if (!win) {
|
|
198
|
-
win = this._openWindow();
|
|
199
|
-
}
|
|
200
|
-
try {
|
|
201
|
-
this.getBlob(function (result) {
|
|
202
|
-
var urlCreator = window.URL || window.webkitURL;
|
|
203
|
-
var pdfUrl = urlCreator.createObjectURL(result);
|
|
204
|
-
win.location.href = pdfUrl;
|
|
205
|
-
|
|
206
|
-
/* temporarily disabled
|
|
207
|
-
if (win !== window) {
|
|
208
|
-
setTimeout(function () {
|
|
209
|
-
if (isNull(win.window)) { // is closed by AdBlock
|
|
210
|
-
window.location.href = pdfUrl; // open in actual window
|
|
211
|
-
}
|
|
212
|
-
}, 500);
|
|
213
|
-
}
|
|
214
|
-
*/
|
|
215
|
-
}, options);
|
|
216
|
-
} catch (e) {
|
|
217
|
-
win.close();
|
|
218
|
-
throw e;
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
Document.prototype.open = function (options, win) {
|
|
223
|
-
options = options || {};
|
|
224
|
-
options.autoPrint = false;
|
|
225
|
-
win = win || null;
|
|
226
|
-
|
|
227
|
-
this._openPdf(options, win);
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
Document.prototype.print = function (options, win) {
|
|
232
|
-
options = options || {};
|
|
233
|
-
options.autoPrint = true;
|
|
234
|
-
win = win || null;
|
|
235
|
-
|
|
236
|
-
this._openPdf(options, win);
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* download(defaultFileName = 'file.pdf', cb = null, options = {})
|
|
241
|
-
* or
|
|
242
|
-
* download(cb, options = {})
|
|
243
|
-
*/
|
|
244
|
-
Document.prototype.download = function (defaultFileName, cb, options) {
|
|
245
|
-
if (isFunction(defaultFileName)) {
|
|
246
|
-
if (!isUndefined(cb)) {
|
|
247
|
-
options = cb;
|
|
248
|
-
}
|
|
249
|
-
cb = defaultFileName;
|
|
250
|
-
defaultFileName = null;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
defaultFileName = defaultFileName || 'file.pdf';
|
|
254
|
-
this.getBlob(function (result) {
|
|
255
|
-
saveAs(result, defaultFileName);
|
|
256
|
-
|
|
257
|
-
if (isFunction(cb)) {
|
|
258
|
-
cb();
|
|
259
|
-
}
|
|
260
|
-
}, options);
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
Document.prototype.getBase64 = function (cb, options) {
|
|
264
|
-
if (!cb) {
|
|
265
|
-
throw 'getBase64 is an async method and needs a callback argument';
|
|
266
|
-
}
|
|
267
|
-
this.getBuffer(function (buffer) {
|
|
268
|
-
cb(buffer.toString('base64'));
|
|
269
|
-
}, options);
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
Document.prototype.getDataUrl = function (cb, options) {
|
|
273
|
-
if (!cb) {
|
|
274
|
-
throw 'getDataUrl is an async method and needs a callback argument';
|
|
275
|
-
}
|
|
276
|
-
this.getBuffer(function (buffer) {
|
|
277
|
-
cb('data:application/pdf;base64,' + buffer.toString('base64'));
|
|
278
|
-
}, options);
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
Document.prototype.getBlob = function (cb, options) {
|
|
282
|
-
if (!cb) {
|
|
283
|
-
throw 'getBlob is an async method and needs a callback argument';
|
|
284
|
-
}
|
|
285
|
-
var that = this;
|
|
286
|
-
this.getBuffer(function (result) {
|
|
287
|
-
var blob = that._bufferToBlob(result);
|
|
288
|
-
cb(blob);
|
|
289
|
-
}, options);
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
Document.prototype.getBuffer = function (cb, options) {
|
|
293
|
-
if (!cb) {
|
|
294
|
-
throw 'getBuffer is an async method and needs a callback argument';
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
var _this = this;
|
|
298
|
-
|
|
299
|
-
this._createDoc(options, function (doc) {
|
|
300
|
-
_this._flushDoc(doc, function (buffer) {
|
|
301
|
-
cb(buffer);
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
Document.prototype.getStream = function (options, cb) {
|
|
307
|
-
if (!isFunction(cb)) {
|
|
308
|
-
var doc = this._createDoc(options);
|
|
309
|
-
return doc;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
this._createDoc(options, function (doc) {
|
|
313
|
-
cb(doc);
|
|
314
|
-
});
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
module.exports = {
|
|
318
|
-
createPdf: function (docDefinition, tableLayouts, fonts, vfs) {
|
|
319
|
-
if (!canCreatePdf()) {
|
|
320
|
-
throw 'Your browser does not provide the level of support needed';
|
|
321
|
-
}
|
|
322
|
-
return new Document(
|
|
323
|
-
docDefinition,
|
|
324
|
-
tableLayouts || global.pdfMake.tableLayouts,
|
|
325
|
-
fonts || global.pdfMake.fonts,
|
|
326
|
-
vfs || global.pdfMake.vfs
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
};
|
|
1
|
+
module.exports = require('./index').default;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var fs = require('fs');
|
|
2
|
+
|
|
3
|
+
var fontContainer = {
|
|
4
|
+
vfs: {
|
|
5
|
+
'data/Courier.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Courier.afm', 'utf8'), encoding: 'utf8' },
|
|
6
|
+
'data/Courier-Bold.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Courier-Bold.afm', 'utf8'), encoding: 'utf8' },
|
|
7
|
+
'data/Courier-Oblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Courier-Oblique.afm', 'utf8'), encoding: 'utf8' },
|
|
8
|
+
'data/Courier-BoldOblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Courier-BoldOblique.afm', 'utf8'), encoding: 'utf8' }
|
|
9
|
+
},
|
|
10
|
+
fonts: {
|
|
11
|
+
Courier: {
|
|
12
|
+
normal: 'Courier',
|
|
13
|
+
bold: 'Courier-Bold',
|
|
14
|
+
italics: 'Courier-Oblique',
|
|
15
|
+
bolditalics: 'Courier-BoldOblique'
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var _global = typeof window === 'object' ? window : typeof global === 'object' ? global : typeof self === 'object' ? self : this;
|
|
21
|
+
if (typeof _global.pdfMake !== 'undefined' && typeof _global.pdfMake.addFontContainer !== 'undefined') {
|
|
22
|
+
_global.pdfMake.addFontContainer(fontContainer);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typeof module !== 'undefined') {
|
|
26
|
+
module.exports = fontContainer;
|
|
27
|
+
}
|