-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (41 loc) · 1.25 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
const userName = document.getElementById("name");
const submitBtn = document.getElementById("submitBtn");
const { PDFDocument, rgb, degrees } = PDFLib;
const capitalize = (str, lower = false) =>
(lower ? str.toLowerCase() : str).replace(/(?:^|\s|["'([{])+\S/g, (match) =>
match.toUpperCase()
);
submitBtn.addEventListener("click", () => {
const val = capitalize(userName.value);
if (val.trim() !== "" && userName.checkValidity()) {
generatePDF(val);
} else {
userName.reportValidity();
}
});
const generatePDF = async (name) => {
const existingPdfBytes = await fetch("./CertificadoUCundinamarca.pdf").then((res) =>
res.arrayBuffer()
);
const pdfDoc = await PDFDocument.load(existingPdfBytes);
pdfDoc.registerFontkit(fontkit);
const fontBytes = await fetch("./Sanchez-Regular.ttf").then((res) =>
res.arrayBuffer()
);
const SanChezFont = await pdfDoc.embedFont(fontBytes);
const pages = pdfDoc.getPages();
const firstPage = pages[0];
firstPage.drawText(name, {
x: 220,
y: 270,
size: 30,
});
const pdfBytes = await pdfDoc.save();
console.log("Certificado Creado");
var file = new File(
[pdfBytes], "Certificado UCundinamarca.pdf", {
type: "application/pdf;charset=utf-8",
}
);
saveAs(file);
};