-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (45 loc) · 1.27 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
52
53
54
55
56
57
58
59
60
61
62
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const ejs = require("ejs");
const pdf = require("html-pdf");
const app = express();
const port = process.env.PORT || 4000;
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/api/hello', (req, res) => {
res.send({ express: 'Hello From Express' });
});
app.post('/generatePDF', (req, res) => {
const options = {
"height": "11.25in",
"width": "8.5in",
"border": {
"top": "0.25in", // default is 0, units: mm, cm, in, px
"right": "0.25in",
"bottom": "0.25in",
"left": "0.25in"
}
};
ejs.renderFile("report-template.ejs", {body : req.body}, {}, (err, str) => {
if (err) {
res.send(err);
return res.sendStatus(500);
} else {
pdf.create(str, options).toStream((err, pdfStream) => {
if (err) {
res.send(err);
return res.sendStatus(500);
} else {
res.statusCode = 200
pdfStream.on('end', () => {
return res.end()
})
pdfStream.pipe(res)
}
});
}
})
});
app.listen(port, () => console.log(`Listening on port ${port}`));