-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathserver.js
48 lines (36 loc) · 1.32 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
var http = require('http');
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var pdfmake = require('../js/index');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ extended: false }));
function createPdfBinary(docDefinition) {
var fonts = {
Roboto: {
normal: path.join(__dirname, '..', 'examples', '/fonts/Roboto-Regular.ttf'),
bold: path.join(__dirname, '..', 'examples', '/fonts/Roboto-Medium.ttf'),
italics: path.join(__dirname, '..', 'examples', '/fonts/Roboto-Italic.ttf'),
bolditalics: path.join(__dirname, '..', 'examples', '/fonts/Roboto-MediumItalic.ttf')
}
};
pdfmake.setFonts(fonts);
var pdf = pdfmake.createPdf(docDefinition);
return pdf.getDataUrl();
}
app.post('/pdf', function (req, res) {
const dd = new Function(req.body.content + '; return dd;')();
createPdfBinary(dd).then(function (binary) {
res.contentType('application/pdf');
res.send(binary);
}, function (error) {
res.send('ERROR:' + error);
});
});
var server = http.createServer(app);
var port = process.env.PORT || 1234;
server.listen(port);
console.log('http server listening on port %d', port);
console.log('dev-playground is available at http://localhost:%d', port);