/
pdfMake.js
140 lines (122 loc) · 3.68 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
/* jslint node: true */
/* jslint browser: true */
/* global BlobBuilder */
'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-Italic.ttf'
}
};
function Document(docDefinition, fonts, vfs) {
this.docDefinition = docDefinition;
this.fonts = fonts || defaultClientFonts;
this.vfs = vfs;
}
Document.prototype._createDoc = function(options, callback) {
var printer = new PdfPrinter(this.fonts);
printer.fs.bindFS(this.vfs);
var doc = printer.createPdfKitDocument(this.docDefinition, options);
var chunks = [];
var result;
doc.on('data', function(chunk) {
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.open = function(message) {
// we have to open the window immediately and store the reference
// otherwise popup blockers will stop us
var win = window.open('', '_blank');
try {
this.getDataUrl(function(result) {
win.location.href = result;
});
} catch(e) {
win.close();
throw e;
}
};
Document.prototype.print = function() {
this.getDataUrl(function(dataUrl) {
var iFrame = document.createElement('iframe');
iFrame.style.position = 'absolute';
iFrame.style.left = '-99999px';
iFrame.src = dataUrl;
iFrame.onload = function() {
function removeIFrame(){
document.body.removeChild(iFrame);
document.removeEventListener('click', removeIFrame);
}
document.addEventListener('click', removeIFrame, false);
};
document.body.appendChild(iFrame);
}, { autoPrint: true });
};
Document.prototype.download = function(defaultFileName, cb) {
if(typeof defaultFileName === "function") {
cb = defaultFileName;
defaultFileName = null;
}
defaultFileName = defaultFileName || 'file.pdf';
this.getBuffer(function (result) {
var blob;
try {
blob = new Blob([result], { 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(result);
blob = new Blob([byteArray.buffer], { type: 'application/pdf' });
}
}
if (blob) {
saveAs(blob, defaultFileName);
}
else {
throw 'Could not generate blob';
}
if (typeof cb === "function") {
cb();
}
});
};
Document.prototype.getBase64 = function(cb, options) {
if (!cb) throw 'getBase64 is an async method and needs a callback argument';
this._createDoc(options, function(buffer) {
cb(buffer.toString('base64'));
});
};
Document.prototype.getDataUrl = function(cb, options) {
if (!cb) throw 'getDataUrl is an async method and needs a callback argument';
this._createDoc(options, function(buffer) {
cb('data:application/pdf;base64,' + buffer.toString('base64'));
});
};
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) {
return new Document(docDefinition, window.pdfMake.fonts, window.pdfMake.vfs);
}
};