@flowaccount/pdfmake 1.0.5 → 1.0.6-staging.3
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/LICENSE +21 -21
- package/README.md +297 -297
- package/build/pdfmake.js +28541 -28790
- package/build/pdfmake.min.js +2 -2
- package/build/pdfmake.min.js.map +1 -1
- package/build/vfs_fonts.js +6 -6
- package/package.json +110 -110
- package/src/3rd-party/svg-to-pdfkit.js +3 -3
- package/src/browser-extensions/URLBrowserResolver.js +96 -96
- package/src/browser-extensions/pdfMake.js +361 -361
- package/src/browser-extensions/tokenizer-shim.js +15 -15
- package/src/browser-extensions/virtual-fs.js +55 -55
- package/src/columnCalculator.js +157 -157
- package/src/docMeasure.js +831 -831
- package/src/docPreprocessor.js +277 -277
- package/src/documentContext.js +383 -383
- package/src/elementWriter.js +442 -434
- package/src/fontProvider.js +68 -68
- package/src/helpers.js +138 -138
- package/src/imageMeasure.js +70 -70
- package/src/layoutBuilder.js +1993 -1770
- package/src/line.js +91 -91
- package/src/pageElementWriter.js +362 -362
- package/src/pdfKitEngine.js +21 -21
- package/src/printer.js +1191 -1191
- package/src/qrEnc.js +790 -790
- package/src/standardPageSizes.js +54 -54
- package/src/styleContextStack.js +138 -138
- package/src/svgMeasure.js +70 -70
- package/src/tableProcessor.js +791 -789
- package/src/textDecorator.js +157 -157
- package/src/textTools.js +442 -442
- package/src/traversalTracker.js +47 -47
|
@@ -1,361 +1,361 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var isFunction = require('../helpers').isFunction;
|
|
4
|
-
var isUndefined = require('../helpers').isUndefined;
|
|
5
|
-
//var isNull = require('../helpers').isNull;
|
|
6
|
-
var pack = require('../helpers').pack;
|
|
7
|
-
var FileSaver = require('file-saver');
|
|
8
|
-
var saveAs = FileSaver.saveAs;
|
|
9
|
-
|
|
10
|
-
var defaultClientFonts = {
|
|
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
|
-
var globalVfs;
|
|
20
|
-
var globalFonts;
|
|
21
|
-
var globalTableLayouts;
|
|
22
|
-
|
|
23
|
-
function Document(docDefinition, tableLayouts, fonts, vfs) {
|
|
24
|
-
this.docDefinition = docDefinition;
|
|
25
|
-
this.tableLayouts = tableLayouts || null;
|
|
26
|
-
this.fonts = fonts || defaultClientFonts;
|
|
27
|
-
this.vfs = vfs;
|
|
28
|
-
this._remoteImagesPrefetched = false;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function canCreatePdf() {
|
|
32
|
-
// Ensure the browser provides the level of support needed
|
|
33
|
-
try {
|
|
34
|
-
var arr = new Uint8Array(1);
|
|
35
|
-
var proto = { foo: function () { return 42; } };
|
|
36
|
-
Object.setPrototypeOf(proto, Uint8Array.prototype);
|
|
37
|
-
Object.setPrototypeOf(arr, proto);
|
|
38
|
-
return arr.foo() === 42;
|
|
39
|
-
} catch (e) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
Document.prototype._createDoc = function (options, cb) {
|
|
45
|
-
var getExtendedUrl = function (url) {
|
|
46
|
-
if (typeof url === 'object') {
|
|
47
|
-
return { url: url.url, headers: url.headers };
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return { url: url, headers: {} };
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
options = options || {};
|
|
54
|
-
if (this.tableLayouts) {
|
|
55
|
-
options.tableLayouts = this.tableLayouts;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
var PdfPrinter = require('../printer');
|
|
59
|
-
|
|
60
|
-
var printer = new PdfPrinter(this.fonts);
|
|
61
|
-
require('fs').bindFS(this.vfs); // bind virtual file system to file system
|
|
62
|
-
|
|
63
|
-
if (!isFunction(cb)) {
|
|
64
|
-
var doc = printer.createPdfKitDocument(this.docDefinition, options);
|
|
65
|
-
|
|
66
|
-
return doc;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
var URLBrowserResolver = require('./URLBrowserResolver');
|
|
70
|
-
var urlResolver = new URLBrowserResolver(require('fs'));
|
|
71
|
-
|
|
72
|
-
for (var font in this.fonts) {
|
|
73
|
-
if (this.fonts.hasOwnProperty(font)) {
|
|
74
|
-
if (this.fonts[font].normal) {
|
|
75
|
-
if (Array.isArray(this.fonts[font].normal)) { // TrueType Collection
|
|
76
|
-
var url = getExtendedUrl(this.fonts[font].normal[0]);
|
|
77
|
-
urlResolver.resolve(url.url, url.headers);
|
|
78
|
-
this.fonts[font].normal[0] = url.url;
|
|
79
|
-
} else {
|
|
80
|
-
var url = getExtendedUrl(this.fonts[font].normal);
|
|
81
|
-
urlResolver.resolve(url.url, url.headers);
|
|
82
|
-
this.fonts[font].normal = url.url;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
if (this.fonts[font].bold) {
|
|
86
|
-
if (Array.isArray(this.fonts[font].bold)) { // TrueType Collection
|
|
87
|
-
var url = getExtendedUrl(this.fonts[font].bold[0]);
|
|
88
|
-
urlResolver.resolve(url.url, url.headers);
|
|
89
|
-
this.fonts[font].bold[0] = url.url;
|
|
90
|
-
} else {
|
|
91
|
-
var url = getExtendedUrl(this.fonts[font].bold);
|
|
92
|
-
urlResolver.resolve(url.url, url.headers);
|
|
93
|
-
this.fonts[font].bold = url.url;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
if (this.fonts[font].italics) {
|
|
97
|
-
if (Array.isArray(this.fonts[font].italics)) { // TrueType Collection
|
|
98
|
-
var url = getExtendedUrl(this.fonts[font].italics[0]);
|
|
99
|
-
urlResolver.resolve(url.url, url.headers);
|
|
100
|
-
this.fonts[font].italics[0] = url.url;
|
|
101
|
-
} else {
|
|
102
|
-
var url = getExtendedUrl(this.fonts[font].italics);
|
|
103
|
-
urlResolver.resolve(url.url, url.headers);
|
|
104
|
-
this.fonts[font].italics = url.url;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (this.fonts[font].bolditalics) {
|
|
108
|
-
if (Array.isArray(this.fonts[font].bolditalics)) { // TrueType Collection
|
|
109
|
-
var url = getExtendedUrl(this.fonts[font].bolditalics[0]);
|
|
110
|
-
urlResolver.resolve(url.url, url.headers);
|
|
111
|
-
this.fonts[font].bolditalics[0] = url.url;
|
|
112
|
-
} else {
|
|
113
|
-
var url = getExtendedUrl(this.fonts[font].bolditalics);
|
|
114
|
-
urlResolver.resolve(url.url, url.headers);
|
|
115
|
-
this.fonts[font].bolditalics = url.url;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (this.docDefinition.images) {
|
|
122
|
-
for (var image in this.docDefinition.images) {
|
|
123
|
-
if (this.docDefinition.images.hasOwnProperty(image)) {
|
|
124
|
-
var originalVal = this.docDefinition.images[image];
|
|
125
|
-
var urlImg = getExtendedUrl(originalVal);
|
|
126
|
-
if (typeof urlImg.url === 'string' && (urlImg.url.toLowerCase().startsWith('http://') || urlImg.url.toLowerCase().startsWith('https://'))) {
|
|
127
|
-
urlResolver.resolve(urlImg.url, urlImg.headers);
|
|
128
|
-
this.docDefinition.images[image] = urlImg.url;
|
|
129
|
-
} else {
|
|
130
|
-
this.docDefinition.images[image] = urlImg.url;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
var _this = this;
|
|
137
|
-
|
|
138
|
-
urlResolver.resolved().then(function () {
|
|
139
|
-
var doc = printer.createPdfKitDocument(_this.docDefinition, options);
|
|
140
|
-
|
|
141
|
-
cb(doc);
|
|
142
|
-
}, function (result) {
|
|
143
|
-
throw result;
|
|
144
|
-
});
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
Document.prototype._flushDoc = function (doc, callback) {
|
|
148
|
-
var chunks = [];
|
|
149
|
-
var result;
|
|
150
|
-
|
|
151
|
-
doc.on('readable', function () {
|
|
152
|
-
var chunk;
|
|
153
|
-
while ((chunk = doc.read(9007199254740991)) !== null) {
|
|
154
|
-
chunks.push(chunk);
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
doc.on('end', function () {
|
|
158
|
-
result = Buffer.concat(chunks);
|
|
159
|
-
callback(result, doc._pdfMakePages);
|
|
160
|
-
});
|
|
161
|
-
doc.end();
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
Document.prototype._getPages = function (options, cb) {
|
|
165
|
-
if (!cb) {
|
|
166
|
-
throw '_getPages is an async method and needs a callback argument';
|
|
167
|
-
}
|
|
168
|
-
var _this = this;
|
|
169
|
-
|
|
170
|
-
this._createDoc(options, function (doc) {
|
|
171
|
-
_this._flushDoc(doc, function (ignoreBuffer, pages) {
|
|
172
|
-
cb(pages);
|
|
173
|
-
});
|
|
174
|
-
});
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
Document.prototype._bufferToBlob = function (buffer) {
|
|
178
|
-
var blob;
|
|
179
|
-
try {
|
|
180
|
-
blob = new Blob([buffer], { type: 'application/pdf' });
|
|
181
|
-
} catch (e) {
|
|
182
|
-
// Old browser which can't handle it without making it an byte array (ie10)
|
|
183
|
-
if (e.name === 'InvalidStateError') {
|
|
184
|
-
var byteArray = new Uint8Array(buffer);
|
|
185
|
-
blob = new Blob([byteArray.buffer], { type: 'application/pdf' });
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (!blob) {
|
|
190
|
-
throw 'Could not generate blob';
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return blob;
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
Document.prototype._openWindow = function () {
|
|
197
|
-
// we have to open the window immediately and store the reference
|
|
198
|
-
// otherwise popup blockers will stop us
|
|
199
|
-
var win = window.open('', '_blank');
|
|
200
|
-
if (win === null) {
|
|
201
|
-
throw 'Open PDF in new window blocked by browser';
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return win;
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
Document.prototype._openPdf = function (options, win) {
|
|
208
|
-
if (!win) {
|
|
209
|
-
win = this._openWindow();
|
|
210
|
-
}
|
|
211
|
-
try {
|
|
212
|
-
this.getBlob(function (result) {
|
|
213
|
-
var urlCreator = window.URL || window.webkitURL;
|
|
214
|
-
var pdfUrl = urlCreator.createObjectURL(result);
|
|
215
|
-
win.location.href = pdfUrl;
|
|
216
|
-
|
|
217
|
-
/* temporarily disabled
|
|
218
|
-
if (win !== window) {
|
|
219
|
-
setTimeout(function () {
|
|
220
|
-
if (isNull(win.window)) { // is closed by AdBlock
|
|
221
|
-
window.location.href = pdfUrl; // open in actual window
|
|
222
|
-
}
|
|
223
|
-
}, 500);
|
|
224
|
-
}
|
|
225
|
-
*/
|
|
226
|
-
}, options);
|
|
227
|
-
} catch (e) {
|
|
228
|
-
win.close();
|
|
229
|
-
throw e;
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
Document.prototype.open = function (options, win) {
|
|
234
|
-
options = options || {};
|
|
235
|
-
options.autoPrint = false;
|
|
236
|
-
win = win || null;
|
|
237
|
-
|
|
238
|
-
this._openPdf(options, win);
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
Document.prototype.print = function (options, win) {
|
|
243
|
-
options = options || {};
|
|
244
|
-
options.autoPrint = true;
|
|
245
|
-
win = win || null;
|
|
246
|
-
|
|
247
|
-
this._openPdf(options, win);
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* download(defaultFileName = 'file.pdf', cb = null, options = {})
|
|
252
|
-
* or
|
|
253
|
-
* download(cb, options = {})
|
|
254
|
-
*/
|
|
255
|
-
Document.prototype.download = function (defaultFileName, cb, options) {
|
|
256
|
-
if (isFunction(defaultFileName)) {
|
|
257
|
-
if (!isUndefined(cb)) {
|
|
258
|
-
options = cb;
|
|
259
|
-
}
|
|
260
|
-
cb = defaultFileName;
|
|
261
|
-
defaultFileName = null;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
defaultFileName = defaultFileName || 'file.pdf';
|
|
265
|
-
this.getBlob(function (result) {
|
|
266
|
-
saveAs(result, defaultFileName);
|
|
267
|
-
|
|
268
|
-
if (isFunction(cb)) {
|
|
269
|
-
cb();
|
|
270
|
-
}
|
|
271
|
-
}, options);
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
Document.prototype.getBase64 = function (cb, options) {
|
|
275
|
-
if (!cb) {
|
|
276
|
-
throw 'getBase64 is an async method and needs a callback argument';
|
|
277
|
-
}
|
|
278
|
-
this.getBuffer(function (buffer) {
|
|
279
|
-
cb(buffer.toString('base64'));
|
|
280
|
-
}, options);
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
Document.prototype.getDataUrl = function (cb, options) {
|
|
284
|
-
if (!cb) {
|
|
285
|
-
throw 'getDataUrl is an async method and needs a callback argument';
|
|
286
|
-
}
|
|
287
|
-
this.getBuffer(function (buffer) {
|
|
288
|
-
cb('data:application/pdf;base64,' + buffer.toString('base64'));
|
|
289
|
-
}, options);
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
Document.prototype.getBlob = function (cb, options) {
|
|
293
|
-
if (!cb) {
|
|
294
|
-
throw 'getBlob is an async method and needs a callback argument';
|
|
295
|
-
}
|
|
296
|
-
var that = this;
|
|
297
|
-
this.getBuffer(function (result) {
|
|
298
|
-
var blob = that._bufferToBlob(result);
|
|
299
|
-
cb(blob);
|
|
300
|
-
}, options);
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
Document.prototype.getBuffer = function (cb, options) {
|
|
304
|
-
if (!cb) {
|
|
305
|
-
throw 'getBuffer is an async method and needs a callback argument';
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
var _this = this;
|
|
309
|
-
|
|
310
|
-
this._createDoc(options, function (doc) {
|
|
311
|
-
_this._flushDoc(doc, function (buffer) {
|
|
312
|
-
cb(buffer);
|
|
313
|
-
});
|
|
314
|
-
});
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
Document.prototype.getStream = function (options, cb) {
|
|
318
|
-
if (!isFunction(cb)) {
|
|
319
|
-
var doc = this._createDoc(options);
|
|
320
|
-
return doc;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
this._createDoc(options, function (doc) {
|
|
324
|
-
cb(doc);
|
|
325
|
-
});
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
module.exports = {
|
|
329
|
-
createPdf: function (docDefinition, tableLayouts, fonts, vfs) {
|
|
330
|
-
if (!canCreatePdf()) {
|
|
331
|
-
throw 'Your browser does not provide the level of support needed';
|
|
332
|
-
}
|
|
333
|
-
return new Document(
|
|
334
|
-
docDefinition,
|
|
335
|
-
tableLayouts || globalTableLayouts || global.pdfMake.tableLayouts,
|
|
336
|
-
fonts || globalFonts || global.pdfMake.fonts,
|
|
337
|
-
vfs || globalVfs || global.pdfMake.vfs
|
|
338
|
-
);
|
|
339
|
-
},
|
|
340
|
-
addVirtualFileSystem: function (vfs) {
|
|
341
|
-
globalVfs = vfs;
|
|
342
|
-
},
|
|
343
|
-
addFonts: function (fonts) {
|
|
344
|
-
globalFonts = pack(globalFonts, fonts);
|
|
345
|
-
},
|
|
346
|
-
setFonts: function (fonts) {
|
|
347
|
-
globalFonts = fonts;
|
|
348
|
-
},
|
|
349
|
-
clearFonts: function () {
|
|
350
|
-
globalFonts = undefined;
|
|
351
|
-
},
|
|
352
|
-
addTableLayouts: function (tableLayouts) {
|
|
353
|
-
globalTableLayouts = pack(globalTableLayouts, tableLayouts);
|
|
354
|
-
},
|
|
355
|
-
setTableLayouts: function (tableLayouts) {
|
|
356
|
-
globalTableLayouts = tableLayouts;
|
|
357
|
-
},
|
|
358
|
-
clearTableLayouts: function () {
|
|
359
|
-
globalTableLayouts = undefined;
|
|
360
|
-
}
|
|
361
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var isFunction = require('../helpers').isFunction;
|
|
4
|
+
var isUndefined = require('../helpers').isUndefined;
|
|
5
|
+
//var isNull = require('../helpers').isNull;
|
|
6
|
+
var pack = require('../helpers').pack;
|
|
7
|
+
var FileSaver = require('file-saver');
|
|
8
|
+
var saveAs = FileSaver.saveAs;
|
|
9
|
+
|
|
10
|
+
var defaultClientFonts = {
|
|
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
|
+
var globalVfs;
|
|
20
|
+
var globalFonts;
|
|
21
|
+
var globalTableLayouts;
|
|
22
|
+
|
|
23
|
+
function Document(docDefinition, tableLayouts, fonts, vfs) {
|
|
24
|
+
this.docDefinition = docDefinition;
|
|
25
|
+
this.tableLayouts = tableLayouts || null;
|
|
26
|
+
this.fonts = fonts || defaultClientFonts;
|
|
27
|
+
this.vfs = vfs;
|
|
28
|
+
this._remoteImagesPrefetched = false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function canCreatePdf() {
|
|
32
|
+
// Ensure the browser provides the level of support needed
|
|
33
|
+
try {
|
|
34
|
+
var arr = new Uint8Array(1);
|
|
35
|
+
var proto = { foo: function () { return 42; } };
|
|
36
|
+
Object.setPrototypeOf(proto, Uint8Array.prototype);
|
|
37
|
+
Object.setPrototypeOf(arr, proto);
|
|
38
|
+
return arr.foo() === 42;
|
|
39
|
+
} catch (e) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
Document.prototype._createDoc = function (options, cb) {
|
|
45
|
+
var getExtendedUrl = function (url) {
|
|
46
|
+
if (typeof url === 'object') {
|
|
47
|
+
return { url: url.url, headers: url.headers };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { url: url, headers: {} };
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
options = options || {};
|
|
54
|
+
if (this.tableLayouts) {
|
|
55
|
+
options.tableLayouts = this.tableLayouts;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
var PdfPrinter = require('../printer');
|
|
59
|
+
|
|
60
|
+
var printer = new PdfPrinter(this.fonts);
|
|
61
|
+
require('fs').bindFS(this.vfs); // bind virtual file system to file system
|
|
62
|
+
|
|
63
|
+
if (!isFunction(cb)) {
|
|
64
|
+
var doc = printer.createPdfKitDocument(this.docDefinition, options);
|
|
65
|
+
|
|
66
|
+
return doc;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
var URLBrowserResolver = require('./URLBrowserResolver');
|
|
70
|
+
var urlResolver = new URLBrowserResolver(require('fs'));
|
|
71
|
+
|
|
72
|
+
for (var font in this.fonts) {
|
|
73
|
+
if (this.fonts.hasOwnProperty(font)) {
|
|
74
|
+
if (this.fonts[font].normal) {
|
|
75
|
+
if (Array.isArray(this.fonts[font].normal)) { // TrueType Collection
|
|
76
|
+
var url = getExtendedUrl(this.fonts[font].normal[0]);
|
|
77
|
+
urlResolver.resolve(url.url, url.headers);
|
|
78
|
+
this.fonts[font].normal[0] = url.url;
|
|
79
|
+
} else {
|
|
80
|
+
var url = getExtendedUrl(this.fonts[font].normal);
|
|
81
|
+
urlResolver.resolve(url.url, url.headers);
|
|
82
|
+
this.fonts[font].normal = url.url;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (this.fonts[font].bold) {
|
|
86
|
+
if (Array.isArray(this.fonts[font].bold)) { // TrueType Collection
|
|
87
|
+
var url = getExtendedUrl(this.fonts[font].bold[0]);
|
|
88
|
+
urlResolver.resolve(url.url, url.headers);
|
|
89
|
+
this.fonts[font].bold[0] = url.url;
|
|
90
|
+
} else {
|
|
91
|
+
var url = getExtendedUrl(this.fonts[font].bold);
|
|
92
|
+
urlResolver.resolve(url.url, url.headers);
|
|
93
|
+
this.fonts[font].bold = url.url;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (this.fonts[font].italics) {
|
|
97
|
+
if (Array.isArray(this.fonts[font].italics)) { // TrueType Collection
|
|
98
|
+
var url = getExtendedUrl(this.fonts[font].italics[0]);
|
|
99
|
+
urlResolver.resolve(url.url, url.headers);
|
|
100
|
+
this.fonts[font].italics[0] = url.url;
|
|
101
|
+
} else {
|
|
102
|
+
var url = getExtendedUrl(this.fonts[font].italics);
|
|
103
|
+
urlResolver.resolve(url.url, url.headers);
|
|
104
|
+
this.fonts[font].italics = url.url;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (this.fonts[font].bolditalics) {
|
|
108
|
+
if (Array.isArray(this.fonts[font].bolditalics)) { // TrueType Collection
|
|
109
|
+
var url = getExtendedUrl(this.fonts[font].bolditalics[0]);
|
|
110
|
+
urlResolver.resolve(url.url, url.headers);
|
|
111
|
+
this.fonts[font].bolditalics[0] = url.url;
|
|
112
|
+
} else {
|
|
113
|
+
var url = getExtendedUrl(this.fonts[font].bolditalics);
|
|
114
|
+
urlResolver.resolve(url.url, url.headers);
|
|
115
|
+
this.fonts[font].bolditalics = url.url;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (this.docDefinition.images) {
|
|
122
|
+
for (var image in this.docDefinition.images) {
|
|
123
|
+
if (this.docDefinition.images.hasOwnProperty(image)) {
|
|
124
|
+
var originalVal = this.docDefinition.images[image];
|
|
125
|
+
var urlImg = getExtendedUrl(originalVal);
|
|
126
|
+
if (typeof urlImg.url === 'string' && (urlImg.url.toLowerCase().startsWith('http://') || urlImg.url.toLowerCase().startsWith('https://'))) {
|
|
127
|
+
urlResolver.resolve(urlImg.url, urlImg.headers);
|
|
128
|
+
this.docDefinition.images[image] = urlImg.url;
|
|
129
|
+
} else {
|
|
130
|
+
this.docDefinition.images[image] = urlImg.url;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
var _this = this;
|
|
137
|
+
|
|
138
|
+
urlResolver.resolved().then(function () {
|
|
139
|
+
var doc = printer.createPdfKitDocument(_this.docDefinition, options);
|
|
140
|
+
|
|
141
|
+
cb(doc);
|
|
142
|
+
}, function (result) {
|
|
143
|
+
throw result;
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
Document.prototype._flushDoc = function (doc, callback) {
|
|
148
|
+
var chunks = [];
|
|
149
|
+
var result;
|
|
150
|
+
|
|
151
|
+
doc.on('readable', function () {
|
|
152
|
+
var chunk;
|
|
153
|
+
while ((chunk = doc.read(9007199254740991)) !== null) {
|
|
154
|
+
chunks.push(chunk);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
doc.on('end', function () {
|
|
158
|
+
result = Buffer.concat(chunks);
|
|
159
|
+
callback(result, doc._pdfMakePages);
|
|
160
|
+
});
|
|
161
|
+
doc.end();
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
Document.prototype._getPages = function (options, cb) {
|
|
165
|
+
if (!cb) {
|
|
166
|
+
throw '_getPages is an async method and needs a callback argument';
|
|
167
|
+
}
|
|
168
|
+
var _this = this;
|
|
169
|
+
|
|
170
|
+
this._createDoc(options, function (doc) {
|
|
171
|
+
_this._flushDoc(doc, function (ignoreBuffer, pages) {
|
|
172
|
+
cb(pages);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
Document.prototype._bufferToBlob = function (buffer) {
|
|
178
|
+
var blob;
|
|
179
|
+
try {
|
|
180
|
+
blob = new Blob([buffer], { type: 'application/pdf' });
|
|
181
|
+
} catch (e) {
|
|
182
|
+
// Old browser which can't handle it without making it an byte array (ie10)
|
|
183
|
+
if (e.name === 'InvalidStateError') {
|
|
184
|
+
var byteArray = new Uint8Array(buffer);
|
|
185
|
+
blob = new Blob([byteArray.buffer], { type: 'application/pdf' });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (!blob) {
|
|
190
|
+
throw 'Could not generate blob';
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return blob;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
Document.prototype._openWindow = function () {
|
|
197
|
+
// we have to open the window immediately and store the reference
|
|
198
|
+
// otherwise popup blockers will stop us
|
|
199
|
+
var win = window.open('', '_blank');
|
|
200
|
+
if (win === null) {
|
|
201
|
+
throw 'Open PDF in new window blocked by browser';
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return win;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
Document.prototype._openPdf = function (options, win) {
|
|
208
|
+
if (!win) {
|
|
209
|
+
win = this._openWindow();
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
this.getBlob(function (result) {
|
|
213
|
+
var urlCreator = window.URL || window.webkitURL;
|
|
214
|
+
var pdfUrl = urlCreator.createObjectURL(result);
|
|
215
|
+
win.location.href = pdfUrl;
|
|
216
|
+
|
|
217
|
+
/* temporarily disabled
|
|
218
|
+
if (win !== window) {
|
|
219
|
+
setTimeout(function () {
|
|
220
|
+
if (isNull(win.window)) { // is closed by AdBlock
|
|
221
|
+
window.location.href = pdfUrl; // open in actual window
|
|
222
|
+
}
|
|
223
|
+
}, 500);
|
|
224
|
+
}
|
|
225
|
+
*/
|
|
226
|
+
}, options);
|
|
227
|
+
} catch (e) {
|
|
228
|
+
win.close();
|
|
229
|
+
throw e;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
Document.prototype.open = function (options, win) {
|
|
234
|
+
options = options || {};
|
|
235
|
+
options.autoPrint = false;
|
|
236
|
+
win = win || null;
|
|
237
|
+
|
|
238
|
+
this._openPdf(options, win);
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
Document.prototype.print = function (options, win) {
|
|
243
|
+
options = options || {};
|
|
244
|
+
options.autoPrint = true;
|
|
245
|
+
win = win || null;
|
|
246
|
+
|
|
247
|
+
this._openPdf(options, win);
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* download(defaultFileName = 'file.pdf', cb = null, options = {})
|
|
252
|
+
* or
|
|
253
|
+
* download(cb, options = {})
|
|
254
|
+
*/
|
|
255
|
+
Document.prototype.download = function (defaultFileName, cb, options) {
|
|
256
|
+
if (isFunction(defaultFileName)) {
|
|
257
|
+
if (!isUndefined(cb)) {
|
|
258
|
+
options = cb;
|
|
259
|
+
}
|
|
260
|
+
cb = defaultFileName;
|
|
261
|
+
defaultFileName = null;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
defaultFileName = defaultFileName || 'file.pdf';
|
|
265
|
+
this.getBlob(function (result) {
|
|
266
|
+
saveAs(result, defaultFileName);
|
|
267
|
+
|
|
268
|
+
if (isFunction(cb)) {
|
|
269
|
+
cb();
|
|
270
|
+
}
|
|
271
|
+
}, options);
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
Document.prototype.getBase64 = function (cb, options) {
|
|
275
|
+
if (!cb) {
|
|
276
|
+
throw 'getBase64 is an async method and needs a callback argument';
|
|
277
|
+
}
|
|
278
|
+
this.getBuffer(function (buffer) {
|
|
279
|
+
cb(buffer.toString('base64'));
|
|
280
|
+
}, options);
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
Document.prototype.getDataUrl = function (cb, options) {
|
|
284
|
+
if (!cb) {
|
|
285
|
+
throw 'getDataUrl is an async method and needs a callback argument';
|
|
286
|
+
}
|
|
287
|
+
this.getBuffer(function (buffer) {
|
|
288
|
+
cb('data:application/pdf;base64,' + buffer.toString('base64'));
|
|
289
|
+
}, options);
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
Document.prototype.getBlob = function (cb, options) {
|
|
293
|
+
if (!cb) {
|
|
294
|
+
throw 'getBlob is an async method and needs a callback argument';
|
|
295
|
+
}
|
|
296
|
+
var that = this;
|
|
297
|
+
this.getBuffer(function (result) {
|
|
298
|
+
var blob = that._bufferToBlob(result);
|
|
299
|
+
cb(blob);
|
|
300
|
+
}, options);
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
Document.prototype.getBuffer = function (cb, options) {
|
|
304
|
+
if (!cb) {
|
|
305
|
+
throw 'getBuffer is an async method and needs a callback argument';
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
var _this = this;
|
|
309
|
+
|
|
310
|
+
this._createDoc(options, function (doc) {
|
|
311
|
+
_this._flushDoc(doc, function (buffer) {
|
|
312
|
+
cb(buffer);
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
Document.prototype.getStream = function (options, cb) {
|
|
318
|
+
if (!isFunction(cb)) {
|
|
319
|
+
var doc = this._createDoc(options);
|
|
320
|
+
return doc;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
this._createDoc(options, function (doc) {
|
|
324
|
+
cb(doc);
|
|
325
|
+
});
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
module.exports = {
|
|
329
|
+
createPdf: function (docDefinition, tableLayouts, fonts, vfs) {
|
|
330
|
+
if (!canCreatePdf()) {
|
|
331
|
+
throw 'Your browser does not provide the level of support needed';
|
|
332
|
+
}
|
|
333
|
+
return new Document(
|
|
334
|
+
docDefinition,
|
|
335
|
+
tableLayouts || globalTableLayouts || global.pdfMake.tableLayouts,
|
|
336
|
+
fonts || globalFonts || global.pdfMake.fonts,
|
|
337
|
+
vfs || globalVfs || global.pdfMake.vfs
|
|
338
|
+
);
|
|
339
|
+
},
|
|
340
|
+
addVirtualFileSystem: function (vfs) {
|
|
341
|
+
globalVfs = vfs;
|
|
342
|
+
},
|
|
343
|
+
addFonts: function (fonts) {
|
|
344
|
+
globalFonts = pack(globalFonts, fonts);
|
|
345
|
+
},
|
|
346
|
+
setFonts: function (fonts) {
|
|
347
|
+
globalFonts = fonts;
|
|
348
|
+
},
|
|
349
|
+
clearFonts: function () {
|
|
350
|
+
globalFonts = undefined;
|
|
351
|
+
},
|
|
352
|
+
addTableLayouts: function (tableLayouts) {
|
|
353
|
+
globalTableLayouts = pack(globalTableLayouts, tableLayouts);
|
|
354
|
+
},
|
|
355
|
+
setTableLayouts: function (tableLayouts) {
|
|
356
|
+
globalTableLayouts = tableLayouts;
|
|
357
|
+
},
|
|
358
|
+
clearTableLayouts: function () {
|
|
359
|
+
globalTableLayouts = undefined;
|
|
360
|
+
}
|
|
361
|
+
};
|