This report is an estimation based on provided data and average metabolic rates. It is not a substitute for professional medical advice.
Report generated by the Caffeine Consumption & Sleep Impact Analyzer.
`;
document.body.appendChild(reportContainer);
// 4. Use html2canvas to render the hidden container
try {
const canvas = await html2canvas(reportContainer, { scale: 2, useCORS: true, logging: false });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' });
// Define page dimensions and margins
const pageHeight = pdf.internal.pageSize.getHeight();
const pageWidth = pdf.internal.pageSize.getWidth();
const margin = 10; // 10mm margin
// Calculate the width and height of the image on the PDF, maintaining aspect ratio
const contentWidth = pageWidth - (margin * 2);
const imgProps = pdf.getImageProperties(imgData);
const contentHeight = (imgProps.height * contentWidth) / imgProps.width;
let heightLeft = contentHeight;
let position = 0;
// Add the first page
pdf.addImage(imgData, 'PNG', margin, margin, contentWidth, contentHeight);
heightLeft -= (pageHeight - (margin * 2));
// Add new pages if the content is taller than one page
while (heightLeft > 0) {
position -= (pageHeight - (margin * 2));
pdf.addPage();
// Add the same image, but shift its Y position up to show the next part
pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight);
heightLeft -= (pageHeight - (margin * 2));
}
pdf.save('Caffeine-Sleep-Analysis-Report.pdf');
} catch (error) {
console.error("Error generating PDF:", error);
alert("Sorry, there was an error creating the PDF report.");
} finally {
// 5. Clean up by removing the hidden container from the DOM
document.body.removeChild(reportContainer);
}
};
// --- SCRIPT EXECUTION START ---
// Set initial state
switchTab(1);
populateConfig();
addCaffeineEntry(); // Add one entry by default
});