`;
document.getElementById('agreement-output-container').innerHTML = agreementHTML;
document.getElementById('pdf-download-section').classList.remove('hidden');
lucide.createIcons();
showTab(tabs.length - 1);
}
async function downloadPdf() {
const pdfLoader = document.getElementById('pdf-loader');
const downloadBtn = document.getElementById('downloadPdfBtn');
const content = document.getElementById('agreement-output');
pdfLoader.classList.remove('hidden');
downloadBtn.disabled = true;
try {
const canvas = await html2canvas(content, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
// Use letter size (8.5x11 inches) which is 612x792 points
const pdf = new jsPDF('p', 'pt', 'letter');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const margin = 40;
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasWidth / canvasHeight;
const imgWidth = pdfWidth - (margin * 2);
const imgHeight = imgWidth / ratio;
let heightLeft = imgHeight;
let position = margin;
pdf.addImage(imgData, 'PNG', margin, position, imgWidth, imgHeight);
heightLeft -= (pdfHeight - margin * 2);
while (heightLeft > 0) {
position = position - (pdfHeight - margin * 2);
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position, imgWidth, imgHeight);
heightLeft -= (pdfHeight - margin * 2);
}
pdf.save('Mediation-Settlement-Agreement.pdf');
} catch (error) {
console.error("Error generating PDF:", error);
alert("There was an error generating the PDF. Please try again.");
} finally {
pdfLoader.classList.add('hidden');
downloadBtn.disabled = false;
}
}
// Event Listeners
generateBtn.addEventListener('click', generateAgreement);
downloadPdfBtn.addEventListener('click', downloadPdf);
// Initial UI setup
showTab(currentTab);
lucide.createIcons();
});