MCPcopy Create free account
hub / github.com/code100x/cms / useGenerateCertificate

Function useGenerateCertificate

src/hooks/useCertGen.ts:17–168  ·  view source on GitHub ↗
({
  certificateDetails,
  userName,
}: GenerateCertificateProps)

Source from the content-addressed store, hash-verified

15};
16
17export function useGenerateCertificate({
18 certificateDetails,
19 userName,
20}: GenerateCertificateProps) {
21 const [generating, setGenerating] = useState(false);
22 const [certificatePdfUrl, setCertificatePdfUrl] = useState<string | null>(
23 null,
24 );
25 const [certificateImageUrl, setCertificateImageUrl] = useState('');
26 const [downloadDate, setDownloadDate] = useState<Date | null>(null);
27
28 useEffect(() => {
29 async function generateCertificateAndImage() {
30 setGenerating(true);
31
32 try {
33 await generateCertificate();
34 await generateCertificateImage();
35 setDownloadDate(new Date());
36 } catch (error) {
37 console.error(error);
38 } finally {
39 setGenerating(false);
40 }
41 }
42
43 generateCertificateAndImage();
44 }, []);
45
46 async function generateCertificate() {
47 try {
48 const existingPdfBytes = await fetch('/certificate.pdf').then((res) =>
49 res.arrayBuffer(),
50 );
51 const pdfDoc = await PDFDocument.load(existingPdfBytes);
52 const timesRomanBoldFont = await pdfDoc.embedFont(
53 StandardFonts.HelveticaBold,
54 );
55
56 const pages = pdfDoc.getPages();
57 const firstPage = pages[0];
58 const { width, height } = firstPage.getSize();
59
60 const fontSize = 30;
61 const textWidth = timesRomanBoldFont.widthOfTextAtSize(
62 userName,
63 fontSize,
64 );
65 const xRecipient = (width - textWidth) / 2;
66 const yRecipient = height * 0.46;
67 firstPage.drawText(capitalizeFirstLetter(userName), {
68 x: xRecipient,
69 y: yRecipient,
70 size: fontSize,
71 font: timesRomanBoldFont,
72 color: rgb(0, 0, 0),
73 });
74

Callers 2

CertificateVerifyFunction · 0.90
CertificateComponentFunction · 0.90

Calls 1

Tested by

no test coverage detected