forked from bpampuch/pdfmake
/
pdfMake.js
189 lines (161 loc) · 4.39 KB
/
pdfMake.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* jslint node: true */
/* jslint browser: true */
'use strict';
var PdfPrinter = require('../printer');
var FileSaver = require('../../libs/FileSaver.js/FileSaver');
var saveAs = FileSaver.saveAs;
var defaultClientFonts = {
Roboto: {
normal: 'Roboto-Regular.ttf',
bold: 'Roboto-Medium.ttf',
italics: 'Roboto-Italic.ttf',
bolditalics: 'Roboto-MediumItalic.ttf'
}
};
function Document(docDefinition, tableLayouts, fonts, vfs) {
this.docDefinition = docDefinition;
this.tableLayouts = tableLayouts || null;
this.fonts = fonts || defaultClientFonts;
this.vfs = vfs;
}
function canCreatePdf() {
// Ensure the browser provides the level of support needed
if (!Object.keys) {
return false;
}
return true;
}
Document.prototype._createDoc = function (options, callback) {
options = options || {};
if (this.tableLayouts) {
options.tableLayouts = this.tableLayouts;
}
var printer = new PdfPrinter(this.fonts);
printer.fs.bindFS(this.vfs);
var doc = printer.createPdfKitDocument(this.docDefinition, options);
var chunks = [];
var result;
doc.on('readable', function () {
var chunk;
while ((chunk = doc.read(9007199254740991)) !== null) {
chunks.push(chunk);
}
});
doc.on('end', function () {
result = Buffer.concat(chunks);
callback(result, doc._pdfMakePages);
});
doc.end();
};
Document.prototype._getPages = function (options, cb) {
if (!cb) {
throw 'getBuffer is an async method and needs a callback argument';
}
this._createDoc(options, function (ignoreBuffer, pages) {
cb(pages);
});
};
Document.prototype._bufferToBlob = function (buffer) {
var blob;
try {
blob = new Blob([buffer], {type: 'application/pdf'});
} catch (e) {
// Old browser which can't handle it without making it an byte array (ie10)
if (e.name === 'InvalidStateError') {
var byteArray = new Uint8Array(buffer);
blob = new Blob([byteArray.buffer], {type: 'application/pdf'});
}
}
if (!blob) {
throw 'Could not generate blob';
}
return blob;
};
Document.prototype._openWindow = function () {
// we have to open the window immediately and store the reference
// otherwise popup blockers will stop us
var win = window.open('', '_blank');
if (win === null) {
throw 'Open PDF in new window blocked by browser';
}
return win;
};
Document.prototype.open = function (options) {
var win = this._openWindow();
options.autoPrint = false;
try {
var that = this;
this.getBuffer(function (result) {
var blob = that._bufferToBlob(result);
var urlCreator = window.URL || window.webkitURL;
var pdfUrl = urlCreator.createObjectURL(blob);
win.location.href = pdfUrl;
}, options);
} catch (e) {
win.close();
throw e;
}
};
Document.prototype.print = function (options) {
var win = this._openWindow();
options.autoPrint = true;
try {
var that = this;
this.getBuffer(function (result) {
var blob = that._bufferToBlob(result);
var urlCreator = window.URL || window.webkitURL;
var pdfUrl = urlCreator.createObjectURL(blob);
win.location.href = pdfUrl;
}, options);
} catch (e) {
win.close();
throw e;
}
};
Document.prototype.download = function (defaultFileName, cb, options) {
if (typeof defaultFileName === 'function') {
cb = defaultFileName;
defaultFileName = null;
}
defaultFileName = defaultFileName || 'file.pdf';
var that = this;
this.getBuffer(function (result) {
var blob = that._bufferToBlob(result);
saveAs(blob, defaultFileName);
if (typeof cb === 'function') {
cb();
}
}, options);
};
Document.prototype.getBase64 = function (cb, options) {
if (!cb) {
throw 'getBase64 is an async method and needs a callback argument';
}
this.getBuffer(function (buffer) {
cb(buffer.toString('base64'));
}, options);
};
Document.prototype.getDataUrl = function (cb, options) {
if (!cb) {
throw 'getDataUrl is an async method and needs a callback argument';
}
this.getBuffer(function (buffer) {
cb('data:application/pdf;base64,' + buffer.toString('base64'));
}, options);
};
Document.prototype.getBuffer = function (cb, options) {
if (!cb) {
throw 'getBuffer is an async method and needs a callback argument';
}
this._createDoc(options, function (buffer) {
cb(buffer);
});
};
module.exports = {
createPdf: function (docDefinition) {
if (!canCreatePdf()) {
throw 'Your browser does not provide the level of support needed';
}
return new Document(docDefinition, window.pdfMake.tableLayouts, window.pdfMake.fonts, window.pdfMake.vfs);
}
};