-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
89 lines (73 loc) · 4.07 KB
/
app.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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer = require('multer');
var http = require('http');
// var sys = require('sys');
var exec = require('child_process').exec;
var util = require('util');
var fs = require('fs');
var api_key = require('./Zapi.js').apikey;
var sendgrid = require('sendgrid')(api_key);
// var nodemailer = require('nodemailer');
// var sleep = require('sleep');
var contents = fs.readFileSync("./uploads/output.json");
var data = JSON.parse(contents);
function sendEmail(i){
// Convert HTML to PDF with wkhtmltopdf
// console.log("Come" + i);
var modifiedFirstName = data[i].Name.replace(/[^a-zA-Z0-9]/g, '');
// var modifiedFirstName = data[i].Name;
var destinationEmail = data[i].Email;
var text_body = "Thank you for Participating in Shaastra. PFA your e-certificate.";
fs.readFile('pdfs/'+ modifiedFirstName +'.pdf',function(err,data){
console.log(destinationEmail);
var params = {
to: destinationEmail,
from: '[email protected]',
fromname: 'Shaastra Outreach',
subject: 'Sample || E-certificate || Shaastra 2016 ',
text: text_body,
files: [{filename: 'e-certificate.pdf', content: data}]
};
var email = new sendgrid.Email(params);
sendgrid.send(email, function (err, json) {
console.log('Error sending mail - ', err);
console.log('Success sending mail - ', json);
});
});
}
function pdfConvert(i){
// console.log(i);
// var dummyContent = '<!DOCTYPE html><html><head><title></title></head><body><img style="width:100%" src="../uploads/participation.jpg"><h3 style="position:absolute;top:42.5%;text-align:center;">' + data[i].Name + '</h3><h3 style="position:absolute;top:47%;text-align:center;">' + data[i].SubDept + '</h3><h3 style="position:absolute;top:51.5%;text-align:center;">' + data[i].Dept + '</h3></body></html>';
var dummyContent ='<!DOCTYPE html><html><head></head>'+
'<body><img style="width:95% ;" src="../uploads/participation.jpg">'+
'<style> @font-face {font-family: Myfont; src: url("./OpenSans-SemiboldItalic.ttf");} h2{ text-align: center;color: #053565;font-size:30px;font-family:Myfont;}</style>' +
'<div style="padding-left: 10%;">'+
'<h2 style="margin-top:-100%;text-align: center;"></h2>'+
'<h2 style="margin-top:51.5%;">' + data[i].Name +'</h2>'+
'<h2 style="margin-top:4%;">' + data[i].Position + '</h2>'+
'<h2 style="margin-top:-1.5%;">' + data[i].SubDept[0].toUpperCase() + data[i].SubDept.slice(1) + '</h2>'+
'<h2 style="margin-top:3.5%;">' + data[i].Dept+ '</h2>'+
'</div>'+
'</body></html>'
// var dummyContent = '<!DOCTYPE html><html><head><title></title></head><body><img style="width:100%" src="../uploads/winnerscertificate.jpg"><h3 style="position:absolute;top:42.5%;left:35%">Howard</h3><h3 style="position:absolute;top:47%;left:32%">IIT Madras</h3></body></html>';
var modifiedFirstName = data[i].Name.replace(/[^a-zA-Z0-9]/g, '');
var htmlFileName = "htmls/"+ modifiedFirstName +".html", pdfFileName = "pdfs/"+ modifiedFirstName +".pdf";
// Save to HTML file
fs.writeFile(htmlFileName, dummyContent, function(err) {
// console.log("Came" + i);
if(err) { throw err; }
util.log("file saved to site.html");
var child = exec("wkhtmltopdf -O landscape " + htmlFileName + " " + pdfFileName, function(err, stdout, stderr) {
if(err) { throw err; }
util.log(stderr);
// console.log("Came 2" + i);
// sendEmail(i);
});
});
console.log('Rendered to ' + htmlFileName + ' and ' + pdfFileName);
}
for(var i=0; i<data.length; i++){
pdfConvert(i);
}