`;
if (previewContainer) {
previewContainer.innerHTML = previewHTML;
}
}
/**
* Handles the PDF download functionality using jsPDF and html2canvas.
*/
function downloadPDF() {
const { jsPDF } = window.jspdf;
const contentToCapture = document.getElementById('preview-container');
if (!contentToCapture) {
console.error("PDF content container not found.");
return;
}
// Temporarily change style for PDF generation to ensure best capture
contentToCapture.style.boxShadow = 'none';
contentToCapture.style.border = 'none';
html2canvas(contentToCapture, {
scale: 2, // Increase scale for better resolution
useCORS: true
}).then(canvas => {
// Restore original styles
contentToCapture.style.boxShadow = 'inset 0 2px 4px 0 rgba(0,0,0,0.05)';
contentToCapture.style.border = '1px solid #e5e7eb';
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'portrait',
unit: 'pt',
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasWidth / canvasHeight;
const width = pdfWidth - 80; // A4 width in points with margin
const height = width / ratio;
// Check if content exceeds one page
if (height > pdfHeight - 80) {
console.warn("Content might be too long for a single PDF page. Consider layout adjustments.");
}
pdf.addImage(imgData, 'PNG', 40, 40, width, height);
pdf.save('Contract-Termination-Notice.pdf');
}).catch(error => {
console.error("Error generating PDF:", error);
// Restore styles even if there's an error
contentToCapture.style.boxShadow = 'inset 0 2px 4px 0 rgba(0,0,0,0.05)';
contentToCapture.style.border = '1px solid #e5e7eb';
});
}
// --- Event Listeners ---
tabs.forEach((tab, index) => {
tab.addEventListener('click', () => switchTab(index));
});
nextBtn.addEventListener('click', () => {
if (currentTab < tabs.length - 1) {
switchTab(currentTab + 1);
}
});
prevBtn.addEventListener('click', () => {
if (currentTab > 0) {
switchTab(currentTab - 1);
}
});
downloadPdfBtn.addEventListener('click', downloadPDF);
// --- Initial Setup ---
updateNavButtons(); // Set initial state of nav buttons
});
