`).join('')}
`;
mealPlanResult.style.display = 'block';
document.getElementById('downloadPdfBtn').style.display = 'block';
});
// PDF Download Functionality
document.getElementById('downloadPdfBtn').addEventListener('click', function() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
// Set color scheme
const primaryColor = '#2c3e50';
const secondaryColor = '#3498db';
const accentColor = '#2ecc71';
// PDF Styling
doc.setFont('helvetica');
// Title
doc.setFontSize(22);
doc.setTextColor(primaryColor);
doc.text('Personalized Meal Plan', 105, 20, { align: 'center' });
// User Details
doc.setFontSize(12);
doc.setTextColor(primaryColor);
const gender = document.getElementById('gender').value;
const age = document.getElementById('age').value;
const weight = document.getElementById('weight').value;
const height = document.getElementById('height').value;
const fitnessGoal = document.getElementById('fitnessGoal').value;
const activityLevel = document.getElementById('activityLevel').value;
doc.text(`Gender: ${gender.charAt(0).toUpperCase() + gender.slice(1)}`, 20, 35);
doc.text(`Age: ${age} years`, 20, 42);
doc.text(`Weight: ${weight} kg`, 20, 49);
doc.text(`Height: ${height} cm`, 20, 56);
doc.text(`Fitness Goal: ${fitnessGoal.replace(/([A-Z])/g, ' $1').replace(/^./, function(str){ return str.toUpperCase(); })}`, 20, 63);
doc.text(`Activity Level: ${activityLevel.replace(/([A-Z])/g, ' $1').replace(/^./, function(str){ return str.toUpperCase(); })}`, 20, 70);
// Dietary Preferences
const dietPreferences = Array.from(document.querySelectorAll('input[name="diet"]:checked'))
.map(checkbox => checkbox.value);
doc.text(`Dietary Preferences: ${dietPreferences.length > 0 ? dietPreferences.map(pref =>
pref.replace(/([A-Z])/g, ' $1').replace(/^./, function(str){ return str.toUpperCase(); })
).join(', ') : 'None'}`, 20, 77);
// Calorie Information
doc.setTextColor(secondaryColor);
doc.setFontSize(14);
doc.text(`Daily Calorie Target: ${calculateCalorieNeeds(
document.getElementById('gender').value,
parseInt(document.getElementById('age').value),
parseFloat(document.getElementById('weight').value),
parseFloat(document.getElementById('height').value),
document.getElementById('activityLevel').value,
document.getElementById('fitnessGoal').value
)} calories`, 20, 90);
// Meal Plan Details
doc.setTextColor(primaryColor);
doc.setFontSize(16);
doc.text('7-Day Meal Plan', 105, 105, { align: 'center' });
// Generate Meal Plan for PDF
const mealPlan = generateMealPlan(
calculateCalorieNeeds(
document.getElementById('gender').value,
parseInt(document.getElementById('age').value),
parseFloat(document.getElementById('weight').value),
parseFloat(document.getElementById('height').value),
document.getElementById('activityLevel').value,
document.getElementById('fitnessGoal').value
),
dietPreferences
);
// Add Meal Plan to PDF
doc.setFontSize(12);
let yPos = 120;
mealPlan.days.forEach(day => {
doc.setTextColor(secondaryColor);
doc.setFontSize(14);
doc.text(day.day, 20, yPos);
doc.setTextColor(primaryColor);
doc.setFontSize(12);
doc.text(`Breakfast: ${day.breakfast}`, 30, yPos + 10);
doc.text(`Lunch: ${day.lunch}`, 30, yPos + 17);
doc.text(`Dinner: ${day.dinner}`, 30, yPos + 24);
doc.text(`Snack: ${day.snack}`, 30, yPos + 31);
yPos += 50;
// Add new page if needed
if (yPos > 280) {
doc.addPage();
yPos = 20;
}
});
// Nutritional Advice Section
doc.setTextColor(accentColor);
doc.setFontSize(14);
doc.text('Nutritional Advice', 105, yPos + 10, { align: 'center' });
doc.setTextColor(primaryColor);
doc.setFontSize(10);
const nutritionalAdvice = [
'1. Stay hydrated by drinking at least 8 glasses of water daily.',
'2. Incorporate a variety of fruits and vegetables in your diet.',
'3. Balance your meals with proteins, complex carbohydrates, and healthy fats.',
'4. Consider consulting a nutritionist for personalized advice.',
'5. Adjust portions based on your specific health goals.'
];
yPos += 20;
nutritionalAdvice.forEach((advice, index) => {
doc.text(advice, 20, yPos + (index * 10));
});
// Footer
doc.setTextColor(primaryColor);
doc.setFontSize(8);
doc.text('Generated by Personalized Meal Plan Generator', 105, 290, { align: 'center' });
// Save the PDF
doc.save('Personalized_Meal_Plan.pdf');
});
