forked from jblotus/aws-lambda-wkhtmltopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (35 loc) · 1011 Bytes
/
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
var wkhtmltopdf = require('wkhtmltopdf');
var fs = require('fs');
var AWS = require('aws-sdk');
var config = require('./config.js');
var s3 = new AWS.S3();
exports.handler = function(event, context) {
return_data = {};
if (event.html) {
var output_filename = Math.random().toString(36).slice(2) + '.pdf';
var output = '/tmp/' + output_filename;
writeStream = fs.createWriteStream(output);
wkhtmltopdf(event.html, function(code, signal) {
s3.putObject({
Bucket : dstBucket,
Key : output_filename,
Body : fs.createReadStream(output),
ContentType : "application/pdf"
}, function(error, data) {
if (error != null) {
console.log("error: " + error);
} else {
console.log('upload done...');
}
return_data = {
filename : output_filename
};
// context.succeed("File has been uploaded");
context.done(null, return_data);
});
}).pipe(writeStream);
} else {
console.log('error');
context.done('unable to get the html', {});
}
};