forked from kaarthik-raja/E-Certificate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FinalCert.js
187 lines (173 loc) · 7.43 KB
/
FinalCert.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
In line 46, if pdfconvert() is called, pdfs will be generated.
TO send mail, call the next line, and comment this line out.
*/
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer = require('multer');
var pdf = require('html-pdf');
var exec = require('child_process').exec;
var util = require('util');
var fs = require('fs');
var sendgrid = require('sendgrid')("");
var mailsent=0;
const request = require("request");
node_xj = require("xlsx-to-json");
var sheet = 'Sheet28';
node_xj({
input: "./uploads/input.xlsx", // input xls
output: "./uploads/input.json", // output json
sheet: sheet // specific sheetname
}, function(err, result) {
if(err) {
console.error(err);
} else {
data = result;
console.log(data.length);
// var contents = fs.readFileSync("./uploads/input.json");
//var data = JSON.parse(contents);
var ctr = 0;
for (var i=0;i<data.length;i++) {
for(var j=i+1;j<data.length;j++) {
if(data[i].festID === data[j].festID) {
ctr++;
break;
}
}
}
console.log(data);
console.log("CTR is ", ctr);
for(var i=0; i<data.length; i++)
//for(var i=0; i<1; i++)
{
if (data[i].festID !== '' && data[i].email !== '') {
pdfConvert(i,sheet.replace(/ /g,'_'));
//sendEmail(i);
}
}
}
});
// var data = require('./list.json');
/*fs.readdir('pdfs', function(err, filenames) {
if (err) {
onError(err);
return;
}
filenames.forEach(function(filename) {
if (filename!=='.pdf') {
var festID = filename.slice(0,-4);
var url='http://shaastra.org:8000/api/users/getmember/' +festID ;
request.get(url, (error, response) => {
var json = JSON.parse(response.body).user;
fs.readFile('pdfs/'+ filename,function(err,data){
var params = {
to: json.email,
// to: '[email protected]',
from: '[email protected]',
fromname: 'Shaastra Webops',
subject: 'E-certificate || Shaastra 2018 ',
html : 'Open only in the latest version of Adobe Reader on pc.',
files: [{filename: 'e-certificate.pdf', content: data}]
};
var email = new sendgrid.Email(params);
sendgrid.send(email, function (err, json2) {
if(err){console.log(err);}
mailsent+=1;
console.log("mailsent:",json.email);
console.log(mailsent);
});
});
});
}
});
});*/
function sendEmail(i){
var modifiedFirstName = data[i].festID;
// var modifiedFirstName = data[i].Name;
var destinationEmail = data[i].email;
var text_body = "<html><body>Hello " + data[i].Name+ "!<br><br>PFA the Certificates. <br>"+
"Open only in the latest Adobe Reader Version in PC.</body></html>";
var fileName = 'certis_signed/'+ modifiedFirstName + '.pdf';
fs.readFile(fileName,function(err,data){
if (err) {console.log(err);}
else {
console.log("mail Sent to ",destinationEmail);
var params = {
to: destinationEmail,
// to: '[email protected]',
from: '[email protected]',
fromname: 'Shaastra Student Relations',
subject: 'E-certificate || Shaastra ',
html : text_body,
files: [{filename: 'e-certificate.pdf', content: data}]
};
var email = new sendgrid.Email(params);
sendgrid.send(email, function (err, json) {
if(err)console.log("error mailing @ ",destinationEmail);
mailsent+=1;
console.log(i);
console.log("mailsent:",mailsent);
});
}
});
}
function pdfConvert(i, eventName){
//console.log(data[i]);
//var url='http://shaastra.org:8000/api/users/getmember/' +data[i].festID.replace(/[^a-zA-Z0-9]/g, '') ;
//var url='http://shaastra.org:8000/api/users/getmember/SHA1803941' ;
// request.get(url, (error, response) => {
//let json = JSON.parse(response.body).user;
//var json = data[i];
//if(error){
//console.log("\n\n\n\n");
//throw error;
//}
// 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>'
// the above one was for coordinator certificates
var dummyContent ='<!DOCTYPE html><html><head></head>'+
'<style> @font-face {font-family: Myfont; src: url("../OpenSans-SemiboldItalic.ttf");} h2{ position: absolute; text-align: center; top: 0%; width: 0%; margin-left: 0%; color: #053565; font-size: 24px; font-family: Myfont;}</style>'+
'<body><img style="width:95% ;" src="../s2k18.jpg">'+
'<h2 style="top: 35%; margin-left: 20%;width: 50%;font-size: 15px;">'+data[i].Name+'</h2>'+
'<h2 style="top: 43.5%; margin-left: 20%;width: 60%;font-size: 10px;">'+data[i].clg_name+'</h2>'+
//'<h2 style="top: 35%; margin-left: 20%;width: 50%;">'+ json.name +'</h2>'+
//'<h2 style="top: 43.5%; margin-left: 24%;width: 60%;font-size: 20px;">'+ json.college+'</h2>'+
'<h2 style="top: 54%; margin-left: 20%;width: 60%;font-size: 15px;">'+data[i].event+'</h2>'+
//'<h2 style="top: 54%; margin-left: 20%;width: 50%;">'+data[i].event+'</h2>'+
'</div></body></html>';
var festID = data[i].festID;
var modifiedFirstName = festID.replace(/[^a-zA-Z0-9]/g, '');
var modifiedEventName = data[i].event.replace(/ /g,"_");
var htmlFileName = "./htmls/" + modifiedEventName+ modifiedFirstName +".html", pdfFileName = "./pdfs/" + modifiedEventName + "/" +modifiedFirstName +".pdf";
var htmlcreateName = __dirname + "/htmls/" +modifiedEventName+ modifiedFirstName +".html"
// Save to HTML file
fs.writeFile(htmlcreateName, dummyContent, function(err) {
console.log("Came");
if(err) { throw err; }
util.log("file saved to site.html");
// var child = exec("firefox -print " + htmlFileName + " -printmode pdf " + pdfFileName, function(err, stdout, stderr) {
var child = exec("phantomjs rasterize.js " + htmlFileName + " " + pdfFileName, function(err, stdout, stderr) {
if(err) { throw err; }
util.log(stderr);
mailsent++;
console.log(mailsent);
// console.log("came to send mail");
// sendEmail(i);
});
});
// pdf.create(dummyContent).toFile(pdfFileName, function(err, res){
// console.log("created ",res.filename);
//});
// console.log('Rendered to ' + htmlFileName + ' and ' + pdfFileName);
//});
}