-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
36 lines (26 loc) · 926 Bytes
/
index.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
var http = require('http'),
htmlToPdf = require('wkhtmltopdf'),
conf = require('./lib/conf.js')
http
.createServer(acceptHtmlAndProvidePdf)
.listen(conf.port, conf.bindIp);
function acceptHtmlAndProvidePdf(request, response) {
console.log('Request received: ' + request);
request.content = '';
request.addListener("data", function (chunk) {
if (chunk) {
request.content += chunk;
}
});
request.addListener("end", function () {
var options = {
encoding: 'utf-8',
pageSize: request.headers['x-page-size'] || 'Letter'
};
response.writeHead(200, { 'Content-Type': 'application/pdf' });
htmlToPdf(request.content, options)
.pipe(response);
console.log('Processed HTML to PDF: ' + response);
});
}
console.log('Server running at http://' + conf.bindIp + ':' + conf.port + '/');