-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (25 loc) · 915 Bytes
/
index.js
File metadata and controls
32 lines (25 loc) · 915 Bytes
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
const fs = require('fs');
const Mustache = require('mustache');
const pdf = require('html-pdf');
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/pdf', (req, res) => {
const writePdf = (err, stream) => {
if (err) return res.status(500).send({ error: 'Erro ao gerar pdf.' });
res.contentType("application/pdf");
stream.pipe(res)
stream.on('end', () => res.end());
};
const readHtml = (err, data) => {
const html = Mustache.render(data, { name: 'Renato' })
pdf.create(html, { format: 'A4' }).toStream(writePdf);
};
fs.readFile('./templates/poc.html', 'utf8', readHtml);
});
const server = app.listen(8081, () => {
const host = server.address().address
const port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})