Snoring Intensity Tracker Snoring Intensity Tracker Log and monitor snoring patterns over time. New Snoring Entry Date Intensity Light Moderate Loud Severe Notes (optional) Log Entry Snoring Log Download Report No entries logged yet. ${new Date(entry.date + 'T00:00:00').toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })} ${entry.notes || 'No notes'} ${entry.intensity} `; logList.appendChild(div); }); } } function addLogEntry() { const date = entryDateInput.value; const intensity = intensityLevelInput.value; const notes = entryNotesInput.value.trim(); if (!date) { errorMessageEl.textContent = 'Please select a date for the entry.'; return; } errorMessageEl.textContent = ''; logEntries.push({ date, intensity, notes }); // Clear inputs for next entry entryNotesInput.value = ''; intensityLevelInput.selectedIndex = 0; renderLog(); } // --- PDF DOWNLOAD FUNCTIONALITY --- async function downloadPDF() { if (logEntries.length === 0) { errorMessageEl.textContent = 'Cannot download an empty log.'; return; } errorMessageEl.textContent = ''; // Temporarily hide the download button so it's not in the PDF downloadPdfBtn.style.visibility = 'hidden'; try { const { jsPDF } = window.jspdf; const canvas = await html2canvas(pdfOutputArea, { scale: 2, useCORS: true, logging: false, }); 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 canvasAspectRatio = canvas.height / canvas.width; let imgWidth = pdfWidth * 0.9; let imgHeight = imgWidth * canvasAspectRatio; if (imgHeight > pdfHeight * 0.95) { imgHeight = pdfHeight * 0.95; imgWidth = imgHeight / canvasAspectRatio; } const xPos = (pdfWidth - imgWidth) / 2; const yPos = (pdfHeight - imgHeight) / 2; pdf.addImage(imgData, 'PNG', xPos, yPos, imgWidth, imgHeight); pdf.save('Snoring-Intensity-Report.pdf'); } catch (error) { console.error("Failed to generate PDF:", error); errorMessageEl.textContent = 'An error occurred while creating the PDF.'; } finally { // Always make the button visible again downloadPdfBtn.style.visibility = 'visible'; } } // --- EVENT LISTENERS --- logEntryBtn.addEventListener('click', addLogEntry); downloadPdfBtn.addEventListener('click', downloadPDF); // --- INITIALIZATION --- renderLog(); });