-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
152 lines (124 loc) · 4.54 KB
/
script.js
File metadata and controls
152 lines (124 loc) · 4.54 KB
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
const { jsPDF } = window.jspdf;
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.hash = this.calculateHash();
}
calculateHash() {
return btoa(this.index + this.timestamp + JSON.stringify(this.data) + this.previousHash);
}
}
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
}
createGenesisBlock() {
return new Block(0, Date.now(), "Genesis Block", "0");
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
}
verifyCertificate(hash) {
return this.chain.some(block => block.hash === hash);
}
}
const certificateBlockchain = new Blockchain();
function issueCertificate() {
const name = document.getElementById("certificateName").value;
const course = document.getElementById("certificateCourse").value;
if (name && course) {
const newBlock = new Block(
certificateBlockchain.chain.length,
Date.now(),
{ name, course },
certificateBlockchain.getLatestBlock().hash
);
certificateBlockchain.addBlock(newBlock);
updateCertificates();
document.getElementById("certificateName").value = "";
document.getElementById("certificateCourse").value = "";
} else {
alert("Please enter both Name and Course!");
}
}
function updateCertificates() {
const certList = document.getElementById("certificates");
certList.innerHTML = "";
certificateBlockchain.chain.forEach((block, index) => {
if (index !== 0) {
const li = document.createElement("li");
li.className = "certificate";
li.innerHTML = `
<strong>Certificate ID:</strong> ${block.hash}<br>
<strong>Name:</strong> ${block.data.name}<br>
<strong>Course:</strong> ${block.data.course}<br>
<button onclick="downloadCertificate('${block.hash}', '${block.data.name}', '${block.data.course}')">Download PDF</button>
`;
certList.appendChild(li);
}
});
}
function downloadCertificate(id, name, course) {
const doc = new jsPDF("landscape");
// Background Color
doc.setFillColor(230, 230, 250); // Light lavender background
doc.rect(0, 0, 297, 210, "F");
// Add Borders
doc.setDrawColor(0, 0, 128); // Dark blue border
doc.setLineWidth(5);
doc.rect(10, 10, 277, 190);
// Date (Top Right)
doc.setFont("helvetica", "bold");
doc.setFontSize(12);
doc.text(`Date: ${new Date().toLocaleDateString()}`, 270, 20, null, null, "right");
// Certificate Title
doc.setFont("helvetica", "bold");
doc.setFontSize(30);
doc.setTextColor(0, 0, 139); // Dark Blue
doc.text("Certificate of Completion", 148, 50, null, null, "center");
// Subtitle
doc.setFont("helvetica", "italic");
doc.setFontSize(14);
doc.setTextColor(0, 0, 0); // Black
doc.text("This is to certify that", 148, 70, null, null, "center");
// Recipient Name
doc.setFont("times", "bold");
doc.setFontSize(24);
doc.setTextColor(0, 0, 0);
doc.text(name, 148, 90, null, null, "center");
// Course Information
doc.setFont("times", "italic");
doc.setFontSize(16);
doc.text(`has successfully completed the course:`, 148, 110, null, null, "center");
doc.setFont("helvetica", "bold");
doc.setFontSize(18);
doc.setTextColor(255, 69, 0); // Orange Red
doc.text(course, 148, 130, null, null, "center");
// Signature Line
doc.setDrawColor(0);
doc.line(70, 165, 130, 165); // Signature line
doc.setFontSize(12);
doc.text("Authorized Signatory", 80, 175);
// Certificate ID (Bottom Center)
doc.setFont("helvetica", "normal");
doc.setFontSize(12);
doc.setTextColor(0, 0, 0);
doc.text(`Certificate ID: ${id}`, 140, 190, null, null, "center");
// Save PDF
doc.save(`Certificate_${id}.pdf`);
}
function verifyCertificate() {
const certId = document.getElementById("verifyCertId").value;
const resultText = certificateBlockchain.verifyCertificate(certId)
? "Certificate is valid! ✅"
: "Certificate not found ❌";
document.getElementById("verificationResult").innerText = resultText;
}