Analysis & Advice
${advice}
Key Metrics
Time in Bed: ${Math.floor(results.totalTimeInBedMins/60)}h ${results.totalTimeInBedMins%60}m
Time Asleep: ${Math.floor(results.totalTimeAsleepMins/60)}h ${results.totalTimeAsleepMins%60}m
Sleep Efficiency: ${results.sleepEfficiency.toFixed(1)}%
Time to Fall Asleep: ${results.timeToSleep} min
Time Awake at Night: ${results.timeAwake} min
Score Breakdown
Duration Score: ${results.durationScore}/40
Efficiency Score: ${results.efficiencyScore}/30
Latency Score: ${results.latencyScore}/15
Wakefulness Score: ${results.wakefulnessScore}/15
`;
await new Promise(resolve => setTimeout(resolve, 100));
try {
const canvas = await html2canvas(pdfExportContent, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: "portrait", unit: "pt", format: "a4" });
const pageWidth = pdf.internal.pageSize.getWidth();
const margin = 40;
const imgProps = pdf.getImageProperties(imgData);
const pdfImageWidth = pageWidth - (margin * 2);
const pdfImageHeight = (imgProps.height * pdfImageWidth) / imgProps.width;
pdf.addImage(imgData, 'PNG', margin, margin, pdfImageWidth, pdfImageHeight);
pdf.save(`Sleep-Quality-Report-${new Date().toISOString().slice(0,10)}.pdf`);
} catch (error) {
console.error("Error generating PDF:", error);
alert("Sorry, there was an error creating the PDF.");
} finally {
pdfLoader.classList.add('hidden');
pdfDownloadBtn.disabled = false;
pdfExportContent.innerHTML = '';
}
}
// --- START THE APP ---
initialize();
});