`;
pdfDownloadContainer.classList.remove('hidden');
};
/**
* Generates and triggers the download of a PDF report.
*/
const generatePdf = () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const content = document.getElementById('summary-content');
if (!content) return;
const title = content.querySelector('h2').textContent;
const sections = content.querySelectorAll('h3');
doc.setFontSize(18);
doc.text(title, 105, 20, { align: 'center' });
let yPos = 35;
sections.forEach(section => {
if (yPos > 260) { // Add new page if content overflows
doc.addPage();
yPos = 20;
}
doc.setFontSize(14);
doc.setFont(undefined, 'bold');
doc.text(section.textContent, 14, yPos);
yPos += 8;
doc.setFontSize(11);
doc.setFont(undefined, 'normal');
const nextElement = section.nextElementSibling;
if (nextElement.tagName === 'P') {
const text = doc.splitTextToSize(nextElement.textContent, 180);
doc.text(text, 14, yPos);
yPos += (text.length * 5) + 5;
} else if (nextElement.tagName === 'UL') {
const listItems = Array.from(nextElement.querySelectorAll('li'));
// Special handling for Action Items to use autoTable
if (section.textContent === 'Action Items') {
const tableBody = listItems.map(li => {
const text = li.textContent;
const match = text.match(/(.*) \(Owner: (.*), Due: (.*)\)/);
if (match) {
return [match[1], match[2], match[3]];
}
return [text, '', ''];
});
doc.autoTable({
startY: yPos - 4,
head: [['Task', 'Owner', 'Due Date']],
body: tableBody,
theme: 'grid',
headStyles: { fillColor: [243, 244, 246], textColor: [55, 65, 81], fontStyle: 'bold' },
styles: { cellPadding: 2.5, fontSize: 9 },
});
yPos = doc.lastAutoTable.finalY + 10;
} else {
listItems.forEach(li => {
doc.text(`• ${li.textContent}`, 18, yPos);
yPos += 7;
});
yPos += 3;
}
}
});
doc.save(`${title.replace(/\s+/g, '_')}_Summary.pdf`);
};
// --- Event Listeners ---
downloadPdfBtn.addEventListener('click', generatePdf);
// --- Initial Setup ---
updateNavButtons();
});
