forked from gmuller/duckhunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (44 loc) · 1.26 KB
/
server.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
var express = require('express');
var url = require('url');
var http = require('http');
var webshot = require('webshot');
var fs = require("fs");
var temp = require("temp");
var app = express();
app.use(express.static(__dirname));
app.use(express.bodyParser());
app.get('/', function(req, res) {
res.statusCode = 200;
res.send("OK\n");
});
app.post('/renderPDF', function(req, res) {
console.log(req.body);
var url = req.body.url;
var timeout = 20000;
if ('timeout' in req.body) { timeout = req.body.timeout; }
console.log(url);
console.log(timeout);
var options = {
paperSize: { format: 'A4', orientation: 'portrait', border: '1cm' },
takeShotOnCallback: true,
timeout: timeout
};
var tempName = temp.path({suffix: '.pdf'});
webshot(url, tempName, options, function(err) {
console.log(err);
res.writeHead( 200, {"Content-Type": 'application/pdf'});
var readStream = fs.createReadStream(tempName);
readStream.pipe(res);
readStream.on('error', function(err){
console.log("Error with file generation");
console.log(err);
res.end();
});
readStream.on('close', function() {
console.log("Finished file write");
fs.unlink(tempName);
})
});
});
app.listen(3002);
console.log('Listening on port 3002');