-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcertificate.js
More file actions
41 lines (41 loc) · 1.4 KB
/
certificate.js
File metadata and controls
41 lines (41 loc) · 1.4 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
const getPdf = async (name, wpm, acc) => {
const { PDFDocument, rgb, StandardFonts } = PDFLib
const exBytes = await fetch("./Certificate.pdf").then((res) => {
return res.arrayBuffer()
})
const PDFDoc = await PDFDocument.load(exBytes)
const customFont = await PDFDoc.embedFont(StandardFonts.Courier)
const pages = PDFDoc.getPages()
const firstPg = pages[0]
firstPg.drawText(name, {
x: 150,
y: 320,
size: 60,
font: customFont
})
firstPg.drawText(wpm, {
x: 245,
y: 221,
size: 24,
})
firstPg.drawText(acc, {
x: 580,
y: 221,
size: 24,
})
const uri = await PDFDoc.saveAsBase64({ dataUri: true })
document.getElementById("cert").src = uri
document.getElementById("c").addEventListener("click",()=>{
const link = document.createElement("a");
link.href = uri;
link.download = localStorage.getItem("name")+"-TyPost";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
}
document.getElementById("typing").addEventListener("input",()=>{
if (document.getElementById("typing").value.length == localStorage.getItem("length")) {
getPdf(localStorage.getItem("name"),`${localStorage.getItem("type")} WPM`,`${Math.round(localStorage.getItem("accuracy"))}%`)
}
})