Please enter valid, positive numbers for weight and duration.
`;
resultsSection.classList.remove('hidden');
return;
}
const selectedExerciseName = exerciseSelect.options[exerciseSelect.selectedIndex].text;
resultsDisplay.innerHTML = `
`;
resultsSection.classList.remove('hidden');
}
// --- PDF DOWNLOAD LOGIC ---
async function downloadPDF() {
if (resultsSection.classList.contains('hidden') || calculateCalories() === null) {
alert("Please calculate your calorie burn first before downloading.");
return;
}
pdfLoader.classList.remove('hidden');
pdfDownloadBtn.disabled = true;
const { jsPDF } = window.jspdf;
const pdfExportContent = document.getElementById('pdf-export-area');
const weight = weightInput.value;
const duration = durationInput.value;
const exerciseName = exerciseSelect.options[exerciseSelect.selectedIndex].text;
const calories = Math.round(calculateCalories());
pdfExportContent.innerHTML = `
Calorie Burn Report
Generated on: ${new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}
Estimated Calories Burned
`;
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(`Calorie-Burn-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 = '';
}
}
// --- INITIALIZE ---
initialize();
});