-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (41 loc) · 1.38 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
console.log("hello")
const userName = document.getElementById("name");
const submitBtn = document.getElementById("submitBtn");
const { PDFDocument, rgb, degrees } = PDFLib;
submitBtn.addEventListener("click", () => {
const val =userName.value;
if (val.trim() !== "" && userName.checkValidity()) {
// console.log(val);
generatePDF(val);
} else {
userName.reportValidity();
}
});
const generatePDF = async (name) => {
const existingPdfBytes = await fetch("Certificate.pdf").then((res) =>
res.arrayBuffer()
);
// Load a PDFDocument from the existing PDF bytes
const pdfDoc = await PDFDocument.load(existingPdfBytes);
pdfDoc.registerFontkit(fontkit);
//get font
const fontBytes = await fetch("Sanchez-Regular.ttf").then((res) =>
res.arrayBuffer()
);
// Embed our custom font in the document
const SanChezFont = await pdfDoc.embedFont(fontBytes);
// Get the first page of the document
const pages = pdfDoc.getPages();
const firstPage = pages[0];
// Draw a string of text diagonally across the first page
firstPage.drawText(name, {
x: 300,
y: 270,
size: 58,
font: SanChezFont ,
color: rgb(0.2, 0.84, 0.67),
});
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true });
saveAs(pdfDataUri,"newcertificate.pdf")
};