-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf.js
91 lines (81 loc) · 3.33 KB
/
pdf.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
80
81
82
83
84
85
86
87
88
89
90
91
var system = require('system');
var args = system.args;
// Send some info node's childProcess' stdout
var fs = require('fs');
var async = require('async');
var AWS = require('aws-sdk');
var util = require('util');
var request = require('request');
var mktemp = require("mktemp");
var pdfFiller = require('pdffiller');
var s3 = new AWS.S3();
var ts=(new Date().getTime());
var event=args[1];
var context=args[2];
//lambdaResponse.file.pre_signed_key = context.awsRequestId+'_'+ts+'_preSigned.pdf';
lambdaResponse.file.pre_signed_key=event.gid;
lambdaResponse.file.filename = context.awsRequestId+'_'+ts+'_.pdf';
//lambdaResponse.file.pre_signed_url = s3.getSignedUrl('putObject', {Bucket: config.awsBucket, Key: lambdaResponse.file.pre_signed_key, Expires:3000});
//console.log("The URL is",lambdaResponse);
async.waterfall([
function buildPDF(next) {
console.log('generating pdf');
var sourcePDF, destinationPDF;
sourcePDF="./"+event.form_name;
destinationPDF = mktemp.createFileSync("/tmp/"+lambdaResponse.file.filename);
//fs.chmod(destinationPDF,'777');
var shouldFlatten = false;
var data = event.fields;
pdfFiller.fillForm(sourcePDF, destinationPDF, data, shouldFlatten, next);
setTimeout(function() {
console.log("Set Timeout Finished");
//var fileStream = fs.createReadStream(destinationPDF);
var data=fs.readFileSync(destinationPDF);
var base64 = new Buffer(data).toString('base64');
//request({method: 'PUT', url: lambdaResponse.file.pre_signed_url, body: data}, function(err, res, base64){
// if(err) callback(addError(config.errors[3]));
// console.log("uploaded to S3: ");
next(null, base64);
//});
}, 20000);
},
function uploadToS3(pdfData, next) {
var params = {
Bucket: config.awsBucket,
Key: event.gid,
ACL:"bucket-owner-full-control", //http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUTacl.html
Body: pdfData,
ContentType: "application/pdf"
}
s3.putObject(params, function(err, response) {
if (err) {
system.stdout.write(addError(config.errors[3]));
} else {
console.log("S3 response");
console.log(response);
next();
}
});
}
], function (err) {
if (err) {
//console.log(err);
system.stdout.write(addError(config.errors[0]));
} else {
system.stdout.write(JSON.stringify(lambdaResponse));
}
}
);
function validateEvent(event){
if (event) {
system.stdout.write(addError(config.errors[7]));
}
if (event) {
system.stdout.write(addError(config.errors[8]));
}
}
function addError(errorObj){
errorObj.request_id=context.awsRequestId;
console.log(errorObj);
system.stdout.write(JSON.stringify(errorObj));
}