-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (39 loc) · 1.27 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
let name = document.getElementById('name');
let submit = document.getElementById('submit');
submit.addEventListener('click',()=>{
let credentialUser = Math.ceil(Math.random()*10000);
let str = "TSStudetn2020"+credentialUser.toString();
generetPdf(name.value,str);
name.value = '';
})
const generetPdf = async (name,cr)=>{
const {PDFDocument,rgb} = PDFLib;
const exBytes = await fetch("./Certificate.pdf").then((res)=>{
return res.arrayBuffer()
});
const exFont = await fetch('./Ubuntu-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 firstP = pages[0];
firstP.drawText(name,{
x:330,
y:340,
size:70,
font:myFont,
color: rgb(.2, 0.84, 0.67)
})
firstP.drawText(cr,{
x:600,
y:45,
size:15,
font:myFont,
color: rgb(0, 0.76, 0.8)
})
const uri = await pdfDoc.saveAsBase64({dataUri: true});
saveAs(uri,"Certificate.pdf",{autoBom:true})
// document.querySelector("#myPDF").src = uri;
};