()
| 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 | |
| 75 | const certificateNumberFontSize = 28; |
| 76 | firstPage.drawText( |
| 77 | `Certificate No:${certificateDetails.certificateSlug}`, |
| 78 | { |
| 79 | x: width * 0.3, |
| 80 | y: height * 0.12, |
| 81 | size: certificateNumberFontSize, |
| 82 | font: timesRomanBoldFont, |
| 83 | color: rgb(0, 0, 0), |
| 84 | }, |
| 85 | ); |
| 86 | |
| 87 | const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true }); |
| 88 | setCertificatePdfUrl(pdfDataUri); |
| 89 | } catch (error) { |
| 90 | console.error(error); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | const generateCertificateImage = async (): Promise<void> => { |
| 95 | try { |
no test coverage detected