-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
43 lines (34 loc) · 979 Bytes
/
app.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
require('dotenv').config();
const express = require('express');
const pdf = require('html-pdf');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.json({ message: 'Updials Document Service.' });
});
app.post('/document/pdf', (req, res) => {
// TODO: Sanitize incoming html, fileName
// This is potential security issue
const { html, fileName } = req.body;
var options = {
format: 'A4',
phantomArgs: ['--local-url-access=false'],
};
pdf.create(html, options).toStream(function (err, stream) {
if (err) {
res.send(404);
} else {
res.setHeader('Content-Type', 'application/pdf');
res.setHeader(
'Content-Disposition',
`attachment; filename=${fileName}.pdf;`
);
stream.pipe(res);
}
});
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log('Server is listening on port ' + port);
});