-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhtml-to-pdf.js
122 lines (111 loc) · 3.49 KB
/
html-to-pdf.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
var child_process = require('child_process'),
fs = require('fs'),
UUIDGenerator = require('node-uuid'),
debug = false,
inputEncoding = null,
outputEncoding = null;
exports.setDebug = function (newDebug) {
debug = newDebug;
};
exports.setInputEncoding = function (enc) {
inputEncoding = enc;
};
exports.setOutputEncoding = function (enc) {
outputEncoding = enc;
};
var convertBase64 = function (pdfPath, callback) {
fs.readFile(pdfPath, function (err, data) {
if (err) {
callback(err);
} else {
fs.unlink(pdfPath, function (deleteError) {
if (deleteError) {
callback(deleteError);
} else {
callback(null, data.toString('base64'));
}
});
}
});
}
exports.convertHTMLStringToBase64PDF = function (html, callback) {
var self = this, pdfFileName = UUIDGenerator.v4() + '.pdf';
self.convertHTMLString(html, pdfFileName, function (error, result) {
if (error) {
callback(error);
} else {
convertBase64(pdfFileName, function (err, base64) {
if (err) {
callback(err);
} else {
result.base64 = base64;
callback(null, result);
}
});
}
});
};
exports.convertHTMLFileToBase64PDF = function (html, callback) {
var self = this, pdfFileName = UUIDGenerator.v4() + '.pdf';
self.convertHTMLFile(html, pdfFileName, function (error, result) {
if (error) {
callback(error);
} else {
convertBase64(pdfFileName, function (err, base64) {
if (err) {
callback(err);
} else {
result.base64 = base64;
callback(null, result);
}
});
}
});
};
exports.convertHTMLString = function (html, pdfPath, callback) {
var self = this, uniqueID = UUIDGenerator.v4();
fs.writeFile(uniqueID + '.html', html, function (err) {
if (err) {
callback(err);
} else {
self.convertHTMLFile(uniqueID + '.html', pdfPath, function (error, results) {
if (error) {
callback(error);
} else {
fs.unlink(uniqueID + '.html', function (deleteError) {
if (deleteError) {
callback(deleteError);
} else {
callback(null, results);
}
});
}
});
}
});
};
exports.convertHTMLFile = function (htmlPath, pdfPath, callback) {
var args = ['-jar', __dirname + '/PDFRenderer.jar'];
if (inputEncoding !== null) {
args.push('--input-encoding', inputEncoding);
}
if (outputEncoding !== null) {
args.push('--output-encoding', outputEncoding);
}
args.push(htmlPath, pdfPath);
var renderer = child_process.spawn('java', args);
renderer.on('error', function (error) {
callback(error);
});
if (debug) {
renderer.stdout.on('data', function (data) {
console.log('STDOUT: ' + data);
});
renderer.stderr.on('data', function (data) {
console.log('STDERR: ' + data);
});
}
renderer.on('exit', function (code) {
callback(null, {process_code: code});
});
};