|
| 1 | +async function GeneratePdf( statement ) { |
| 2 | + var dateValue = $('#dateValue').val(); |
| 3 | + const [year, month, date1] = dateValue.split("-"); |
| 4 | + var student = $('#m_username').val().trim(); |
| 5 | + var roll = $('#m_rollno').val(); |
| 6 | + var menname = $('#m_mentorname').val(); |
| 7 | + var mencode = $('#m_mentorcode').val(); |
| 8 | + var gen = $("input[name='radio1']:checked").val(); |
| 9 | + var stay = $("input[name='radio2']:checked").val(); |
| 10 | + details = splitString($('#details').val()); |
| 11 | + |
| 12 | + if (!dateValue || !student || !roll || !menname || !mencode || !gen || !stay ) { |
| 13 | + alert('Please fill in all the fields.'); |
| 14 | + return 0; |
| 15 | + } |
| 16 | + |
| 17 | + const existingPdfUrl = '../forms/AttendanceForm.pdf'; |
| 18 | + const existingPdfBytes = await fetch(existingPdfUrl).then(res => res.arrayBuffer()); |
| 19 | + const pdfDoc = await PDFLib.PDFDocument.load(existingPdfBytes); |
| 20 | + |
| 21 | + const pngUrl = '../imgs/checkmark.png' |
| 22 | + const pngImageBytes = await fetch(pngUrl).then((res) => res.arrayBuffer()) |
| 23 | + const pngImage = await pdfDoc.embedPng(pngImageBytes) |
| 24 | + const pngDims = pngImage.scale(0.5) |
| 25 | + |
| 26 | + // Set the page you want to modify |
| 27 | + const page = pdfDoc.getPages()[0]; |
| 28 | + // Add text to the page |
| 29 | + const timesnewroman = await pdfDoc.embedFont(PDFLib.StandardFonts.TimesRoman); |
| 30 | + page.drawText(date1, { x: 403, y: 707, font: timesnewroman, size: 12 });//date |
| 31 | + page.drawText(month, { x: 434, y: 707, font: timesnewroman, size: 12 });//month |
| 32 | + page.drawText(year, { x: 460, y: 707, font: timesnewroman, size: 12 });//year |
| 33 | + page.drawText(student, { x: 160, y: 621, font: timesnewroman, size: 12 });//student_name |
| 34 | + page.drawText(menname, { x: 158, y: 665, font: timesnewroman, size: 12 });//mentor_name |
| 35 | + page.drawText(mencode, { x: 374, y: 665, font: timesnewroman, size: 12 });//mentor_code |
| 36 | + page.drawText('__________________________________________',{x:50,y:544,rotate:PDFLib.degrees(-14),font: timesnewroman, size: 25}); |
| 37 | + page.drawText(roll, { x: 358, y: 621, font: timesnewroman, size: 12 });//roll_number |
| 38 | + |
| 39 | + page.drawText(details[0], { x: 180, y: 343, font: timesnewroman, size: 12 });//details1(max-70) |
| 40 | + if (typeof details[1] !== 'undefined') { |
| 41 | + page.drawText(details[1], { x: 180, y: 326, font: timesnewroman, size: 12 });//details2(max-70) |
| 42 | + } |
| 43 | + else{ |
| 44 | + console.error('Text is undefined.'); |
| 45 | + } |
| 46 | + |
| 47 | + page.drawImage(pngImage, { x: 46,y: 362, width: 15,height: 15,}); // attendance_tick |
| 48 | + gen == 0 ? page.drawImage(pngImage, { x: 166,y: 607, width: 15,height: 15,}) : page.drawImage(pngImage, { x: 208,y: 607, width: 15,height: 15,}); |
| 49 | + stay == 0 ? page.drawImage(pngImage, { x: 380,y: 607, width: 15,height: 15,}) : page.drawImage(pngImage, { x: 440,y: 607, width: 15,height: 15,}); |
| 50 | + |
| 51 | + |
| 52 | + // Save modified PDF document |
| 53 | + const modifiedPdfBytes = await pdfDoc.save(); |
| 54 | + |
| 55 | + // Convert the PDF document to a blob |
| 56 | + const modifiedPdfBlob = new Blob([modifiedPdfBytes], { type: 'application/pdf' }); |
| 57 | + |
| 58 | +// Create a download link for the modified PDF and click it to download |
| 59 | + const downloadLink = document.createElement('a'); |
| 60 | + downloadLink.href = URL.createObjectURL(modifiedPdfBlob); |
| 61 | +if (statement == 'download') { |
| 62 | + downloadLink.download = `Attendance Unblock Form - ${roll}.pdf`; |
| 63 | + downloadLink.click(); |
| 64 | +} |
| 65 | + |
| 66 | + if (statement == 'print'){ |
| 67 | + const printWindow = window.open('', '_blank'); |
| 68 | + const printDocument = printWindow.document; |
| 69 | + |
| 70 | + // Embed the PDF in an <embed> element for printing |
| 71 | + const embedElement = printDocument.createElement('embed'); |
| 72 | + embedElement.src = downloadLink.href; |
| 73 | + embedElement.type = 'application/pdf'; |
| 74 | + embedElement.style.width = '100%'; |
| 75 | + embedElement.style.height = '100vh'; |
| 76 | + |
| 77 | + printDocument.body.appendChild(embedElement); |
| 78 | + return 1; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function splitString(input) { |
| 83 | + const words = input.split(' '); |
| 84 | + const result = []; |
| 85 | + let currentLine = ''; |
| 86 | + for (let i = 0; i < words.length; i++) { |
| 87 | + const word = words[i]; |
| 88 | + if (currentLine.length + word.length <= 90) { |
| 89 | + currentLine += (currentLine.length === 0 ? '' : ' ') + word; |
| 90 | + } else { |
| 91 | + result.push(currentLine); |
| 92 | + currentLine = word; |
| 93 | + } |
| 94 | + } |
| 95 | + if (currentLine.length > 0) { |
| 96 | + result.push(currentLine); |
| 97 | + } |
| 98 | + return result; |
| 99 | +} |
| 100 | + |
0 commit comments