-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (136 loc) · 4.15 KB
/
index.js
File metadata and controls
155 lines (136 loc) · 4.15 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
153
154
155
// const base = "/Ragam-Certificate-Generator"
// const base =""
const base = "/certificates";
const addText = (text) => {
let h3s = document.querySelectorAll("h3");
for (let i = 0; i < h3s.length; i++) {
h3 = h3s[i];
h3.parentNode.removeChild(h3);
}
let main = document.querySelector(".main");
var h = document.createElement("H3");
var t = document.createTextNode(text);
h.appendChild(t);
main.appendChild(h);
};
const verifyUser = async (regID) => {
let res = await fetch("./participants.json");
res = await res.json();
if (res[regID]) return res[regID];
return null;
};
const titleCase = (str) => {
str = str.split(/[ ._]+/);
for (var i = 0; i < str.length; i++) {
if (str[i].length > 2) {
str[i] =
str[i].toLowerCase().charAt(0).toUpperCase() +
str[i].slice(1).toLowerCase();
}
}
return str.join(" ");
};
const generateRagamPDF = async (name) => {
let main = document.querySelector(".main");
certificate = await fetch("./certificate/details.json").then((res) => {
return res.json();
});
const { PDFDocument, rgb } = PDFLib;
const exBytes = await fetch("./certificate/certificate.pdf").then((res) => {
return res.arrayBuffer();
});
const exFont = await fetch("./fonts/GreatVibes-Regular.ttf").then((res) => {
return res.arrayBuffer();
});
const pdfDoc = await PDFDocument.load(exBytes);
pdfDoc.registerFontkit(fontkit);
const myFont = await pdfDoc.embedFont(exFont);
const pages = pdfDoc.getPages();
const firstPg = pages[0];
if (name != null) {
name = name.trim();
name = titleCase(name);
let opts = {
// size: certificate.name.fontSize,
x: certificate.name.x,
y: certificate.name.y,
color: rgb(
certificate.name.fontColor.r,
certificate.name.fontColor.g,
certificate.name.fontColor.b
),
font: myFont,
}
name.length > 18 ?
opts.size = 45 :
opts.size = 60
firstPg.drawText(name, opts);
}
var qr = new QRious({
value: window.location.href,
foreground: certificate.qrCode.foreground,
background: certificate.qrCode.background,
});
qr = qr.toDataURL();
const qrImage = await pdfDoc.embedPng(qr);
firstPg.drawImage(qrImage, {
x: certificate.qrCode.x,
y: certificate.qrCode.y,
width: 125,
height: 125,
});
const uri = await pdfDoc.saveAsBase64({ dataUri: true });
var elem = document.createElement("img");
elem.setAttribute("id", "download-button");
elem.setAttribute("src", "./static/img/download-icon.svg");
elem.setAttribute("height", "40");
elem.setAttribute("width", "40");
var anchor = document.createElement("a");
anchor.href = uri;
anchor.download = "FOSSMeet'23 Certificate.pdf";
anchor.appendChild(elem);
main.appendChild(anchor);
// var enter = document.createElement("br");
// main.appendChild(enter);
};
var button = document.getElementById("button");
var ID = document.getElementById("ID"); // Get only the element.
const urlParams = new URLSearchParams(window.location.search);
const category = urlParams.get("category");
window.onload = async (event) => {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const regID = urlParams.get("id");
if (regID) {
verifyUser(regID).then((user) => {
if (user) {
document.getElementById("form").style.display = "none";
generateRagamPDF(user);
} else {
document.getElementsByClassName('error')[0].classList.add('show');
setTimeout(()=> document.getElementsByClassName('error')[0].classList.remove('show'),2500);
}
});
}
};
button.addEventListener(
"click",
function (e) {
e.preventDefault();
var regID = ID.value.toUpperCase();
if (regID.length === 0) {
alert("Enter your ID");
return;
}
verifyUser(regID).then((user) => {
if (user) {
window.location.href =
window.location.href.split("?")[0] + "?id=" + regID;
} else {
document.getElementsByClassName('error')[0].classList.add('show');
setTimeout(()=> document.getElementsByClassName('error')[0].classList.remove('show'),2500);
}
});
},
false
);