-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
119 lines (88 loc) · 2.94 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
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
const puppeteer = require('puppeteer');
const fs = require('fs');
const express = require('express');
const app=express();
const ejs= require('ejs');
const mysql=require("mysql");
const bodyParser = require("body-parser");
app.use(express.json());
app.use(bodyParser.urlencoded({extended:false}));
const db = mysql.createConnection({
host:"database-1.cz4k2aulzdrl.ap-south-1.rds.amazonaws.com",
user:"admin",
password:"rootpass",
database:"toptrove"
})//fill it up
db.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
})
app.get('/',(req,res)=>{
res.send('hello user');
})
app.get("/fileget", (req, res) => {
const file_name =req.body.file_name;
console.log(file_name);
const query = "Select file_data From certificate Where file_name = ?";
db.query(query, [file_name], (err, result) => {
if (err) {
console.log(err);
}
// console.log(Buffer.from(result[0].file_data).toString())
fs.writeFileSync(`${file_name}.pdf`, Buffer.from(result[0].file_data))
// res.send(pdf);
res.send(fs.writeFileSync(`${file_name}.pdf`, Buffer.from(result[0].file_data)));
})
});
app.post('/filestore',async (req,res)=>{
const{cname,ctitle,cscore,cdate}=req.body;//name of candidate,job title,score of candidate and date of issue of certificate is taken from request body.
const browser = await puppeteer.launch();
const page = await browser.newPage();
try {
var compiled = ejs.compile(fs.readFileSync(__dirname + '/views/cert1.ejs', 'utf8'));
var html = compiled({ name: cname, title:ctitle , score:cscore,date:cdate});//DYNAMIC VALUES
await page.setContent(html, { waitUntil: 'domcontentloaded' });
} catch (error) {
console.log(new Error(`${error}`));
await browser.close();
res.send(error);
return;
}
await page.emulateMediaType('screen');
const pdf = await page.pdf({
// path: `${cname}.pdf`,
margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
printBackground: true,
format: 'letter',
});
//once the pdf is created it is not stored in any path, instead its stored in database in next step.
await browser.close();
const values=[cname,pdf]
const query= "INSERT INTO certificate(`file_name`,`file_data`) values (?,?)";
db.query(query,values,(err,result)=>{
if(err)
{
console.log(err);
res.send(err)
return;
}
console.log(result);
});
res.send('ok')
});
const port=process.env.PORT ||8800
app.listen(port,()=>{
console.log(`server running on port number ${port}`);
});
// var type = process.argv.slice(2)[0] || 'url';
// if (type === 'url') {
// const website_url = 'https://vcint.github.io/nodeapi/';
// await page.goto(website_url, { waitUntil: 'networkidle0' });
// } else if (type === 'file') {
// } else {
// console.log(new Error(`HTML source "${type}" is unkown.`));
// await browser.close();
// return;
// }