`;
}
// Add signature and stamp
html += `
`; // Close certificate-border
return html;
}
function generateDocument(type) {
// Get all form values
const formData = getFormData();
// Validate required fields
if (!formData.recipientName || !formData.certificateBody || !formData.signatureName) {
alert('Please fill in all required fields: Recipient Name, Certificate Content, and Authorized Signatory');
return;
}
// Create a temporary div for the document
const docDiv = document.createElement('div');
docDiv.style.padding = '20px';
docDiv.style.width = '210mm';
docDiv.style.minHeight = '297mm';
docDiv.style.margin = '0 auto';
docDiv.style.backgroundColor = 'white';
docDiv.style.boxSizing = 'border-box';
// Build the document HTML
docDiv.innerHTML = generateDocumentHtml(formData);
document.body.appendChild(docDiv);
// Generate PDF
html2canvas(docDiv, {
scale: 2,
logging: false,
useCORS: true,
allowTaint: true,
onclone: function(clonedDoc) {
// Ensure logo is loaded in the cloned document
const logoPreview = clonedDoc.getElementById('logoPreview');
if (logoPreview) {
logoPreview.style.backgroundImage = document.getElementById('logoPreview').style.backgroundImage;
}
}
}).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'mm', 'a4');
const imgWidth = 210;
const pageHeight = 295;
const imgHeight = canvas.height * imgWidth / canvas.width;
let heightLeft = imgHeight;
let position = 0;
pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
}
const fileName = formData.certificateTitle ?
`${formData.certificateTitle}_${formData.recipientName}.pdf` :
`Certificate_${formData.recipientName}.pdf`;
pdf.save(fileName);
document.body.removeChild(docDiv);
}).catch(err => {
console.error('Error generating PDF:', err);
alert('Error generating PDF. Please try again.');
document.body.removeChild(docDiv);
});
}
function formatDate(dateString) {
if (!dateString) return '';
const date = new Date(dateString);
const day = date.getDate().toString().padStart(2, '0');
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}
});
`;
// Add signature if exists
if (formData.signatureName) {
html += `
`;
html += ``;
html += `
`;
}
// Add stamp if exists
if (formData.stampLine1 || formData.stampLine2 || formData.stampLine3) {
html += `${formData.signatureName}
`; if (formData.designation) { html += `${formData.designation}
`; } html += ``;
if (formData.stampLine1) html += `
`;
}
html += `${formData.stampLine1}
`;
if (formData.stampLine2) html += `${formData.stampLine2}
`;
if (formData.stampLine3) html += `${formData.stampLine3}
`;
html += `
