-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
79 lines (63 loc) · 1.88 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require('@google-cloud/debug-agent');
const fs = require('fs');
const pdf = require('html-pdf');
const uuidv4 = require('uuid/v4');
const renderTemplate = require('./template');
const dirname = 'file://' + __dirname + '/';
function getAccessToken(header) {
if (header) {
var match = header.match(/^Bearer\s+([^\s]+)$/);
if (match) {
return match[1];
}
}
return null;
}
function createPDF(html) {
return new Promise(function(resolve, reject) {
let file_name = '/tmp/'+uuidv4() + '.pdf';
const options = {
'base': dirname,
'quality': '100',
'format': 'A4'
};
pdf.create(html, options).toFile(file_name, function(err) {
if (err) {
reject('Error: something goes wrong ! '+ err);
} else {
resolve(file_name);
}
});
});
}
function authorized(req, res) {
let data = req.body;
data.dirname = dirname;
// todo: check/validate data
const html = renderTemplate(data);
createPDF(html)
.then(function(file_name){
res.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-Disposition": "attachment; filename=" + file_name
});
fs.createReadStream(file_name).pipe(res);
})
.catch( function(error) {
console.error("Failed!" + error);
res.status(400).send("Error: Pdf generation failed!");
});
}
/**
* Cloud Function.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.generateInvoice = function generateInvoice(req, res) {
console.log('this is the request');
console.log(req);
// todo: check authorization JWT, eventually cors
// var accessToken = getAccessToken(req.get('Authorization'));
authorized(req, res);
};