`;
};
pdfButton.addEventListener('click', async () => {
const pdfExportContainer = document.createElement('div');
Object.assign(pdfExportContainer.style, {
position: 'absolute', left: '-9999px', top: '0',
width: '700px', padding: '2rem', backgroundColor: '#ffffff',
fontFamily: 'Inter, sans-serif'
});
const resultsHTML = document.querySelector('.results-card').innerHTML;
const lastPeriodDate = new Date(lastPeriodInput.value + 'T00:00:00');
const cycleLength = cycleLengthInput.value;
pdfExportContainer.innerHTML = `
${resultsHTML}
Ovulation Calculation Report
Last Period Start: ${formatDate(lastPeriodDate)}
Average Cycle Length: ${cycleLength} days
This report provides an estimation based on the data provided and is not a substitute for professional medical advice.
`; document.body.appendChild(pdfExportContainer); try { const canvas = await html2canvas(pdfExportContainer, { scale: 2 }); const imgData = canvas.toDataURL('image/png'); const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const imgHeight = canvas.height * pdfWidth / canvas.width; pdf.addImage(imgData, 'PNG', 15, 15, pdfWidth - 30, imgHeight - 30); pdf.save('Ovulation-Report.pdf'); } catch (error) { console.error("PDF Generation Failed:", error); alert("An error occurred while generating the PDF."); } finally { document.body.removeChild(pdfExportContainer); } }); });