-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (98 loc) · 3.61 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
120
const userName = document.getElementById("name");
const userN = document.getElementById("id");
const CertificateID = document.getElementById("IdC");
const submitBtn = document.getElementById("submitBtn");
const { PDFDocument, rgb } = PDFLib;
const capitalize = (str, lower = false) =>
(lower ? str.toLowerCase() : str).replace(/(?:^|\s|["'([{])+\S/g, (match) =>
match.toUpperCase()
);
submitBtn.addEventListener("click", () => {
const nameVal = capitalize(userName.value);
const idVal = capitalize(userN.value);
if (nameVal.trim() !== "" && idVal.trim() !== "" && userName.checkValidity() && userN.checkValidity()) {
const currentTime = new Date().toLocaleString();
generatePDF(nameVal, idVal, currentTime);
} else {
userName.reportValidity();
userN.reportValidity();
}
});
const generatePDF = async (name, id, currentTime) => {
const fileName = `${name.replace(/\s/g, " ")}.pdf`;
const existingPdfBytes = await fetch("./Certificado.pdf").then((res) => res.arrayBuffer());
const pdfDoc = await PDFDocument.load(existingPdfBytes);
pdfDoc.registerFontkit(fontkit);
const fontBytes = await fetch("./CenturyGothic.ttf").then((res) => res.arrayBuffer());
const CenturyGothic = await pdfDoc.embedFont(fontBytes);
const pages = pdfDoc.getPages();
const firstPage = pages[0];
const textSize = 30;
const pageWidth = firstPage.getWidth();
const pageHeight = firstPage.getHeight();
const nameTextWidth = CenturyGothic.widthOfTextAtSize(name, textSize);
const nameTextHeight = CenturyGothic.widthOfTextAtSize(name, textSize);
const idTextWidth = CenturyGothic.widthOfTextAtSize(id, textSize);
const idTextHeight = CenturyGothic.widthOfTextAtSize(id, textSize);
const IdC = generateUniqueIdC();
const idCTextWidth = CenturyGothic.widthOfTextAtSize(IdC, textSize);
const totalTextWidth = Math.max(nameTextWidth, idTextWidth);
const totalTextHeight = Math.max(nameTextHeight, idTextHeight);
const centerX = (pageWidth - totalTextWidth) / 2;
const centerY = (pageHeight - totalTextHeight) / 2;
firstPage.drawText(name, {
x: centerX,
y: 280,
size: textSize,
});
firstPage.drawText(id, {
x: 330,
y: 245,
size: 15,
});
firstPage.drawText(IdC, {
x: 48,
y: 75,
size: 10,
color: rgb(68/255, 124/255, 66/255),
});
firstPage.drawText(currentTime, {
x: 48,
y: 90,
size: 10,
color: rgb(68/255, 124/255, 66/255),
});
const qrCodeData = `Certificado de:${name}\nCon número de CC: ${id}\nFecha: ${currentTime}\n${IdC}\n`;
const qrCode = await generateQR(qrCodeData);
const qrCodeImage = await pdfDoc.embedPng(qrCode);
const qrCodeWidth = 80;
const qrCodeHeight = 80;
firstPage.drawImage(qrCodeImage, {
x: 48,
y: 105,
width: qrCodeWidth,
height: qrCodeHeight,
});
const pdfBytes = await pdfDoc.save();
console.log("Certificado Creado");
var file = new File([pdfBytes], fileName, {
type: "application/pdf;charset=utf-8",
});
saveAs(file);
};
const generateQR = async (data) => {
const qrCodeUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(data)}`;
const qrCodeResponse = await fetch(qrCodeUrl);
const qrCodeBlob = await qrCodeResponse.blob();
return new Uint8Array(await qrCodeBlob.arrayBuffer());
};
const generateUniqueIdC = () => {
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*";
const idLength = 20;
let id = "ID único del certificado: ";
for (let i = 0; i < idLength; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
id += characters[randomIndex];
}
return id;
};