`;
pdfContentWrapper.appendChild(summarySection);
// Add distribution charts
const chartsSection = document.createElement('div');
chartsSection.classList.add('grid', 'grid-cols-2', 'gap-6', 'mb-6');
chartsSection.innerHTML = `
Cultivated Area by Crop Type
Harvested Quantity by Crop Type
`;
pdfContentWrapper.appendChild(chartsSection);
// Populate PDF charts (re-use logic but target PDF elements)
const areaByCrop = cultivationRecords.reduce((acc, record) => { acc[record.cropType] = (acc[record.cropType] || 0) + record.cultivatedArea; return acc; }, {});
renderBarChart(chartsSection.querySelector('#pdfAreaByCropChart'), areaByCrop, 'area');
const quantityByCrop = cultivationRecords.reduce((acc, record) => { acc[record.cropType] = (acc[record.cropType] || 0) + record.harvestedQuantity; return acc; }, {});
renderBarChart(chartsSection.querySelector('#pdfQuantityByCropChart'), quantityByCrop, 'quantity');
const waterUsageByCrop = cultivationRecords.reduce((acc, record) => { acc[record.cropType] = (acc[record.cropType] || 0) + record.waterUsage; return acc; }, {});
renderBarChart(chartsSection.querySelector('#pdfWaterUsageByCropChart'), waterUsageByCrop, 'water');
// Add detailed cultivation list table
const cultivationListSection = document.createElement('div');
cultivationListSection.innerHTML = `
Detailed Cultivation Records
| ID |
Crop Type |
Area (Acres) |
Planting Date |
Harvest Date |
Quantity (Lbs) |
Water (Gal) |
Fertilizer (Lbs) |
Status |
${dashboardCultivationTableBody.innerHTML}
`;
pdfContentWrapper.appendChild(cultivationListSection);
// Options for html2pdf
const options = {
margin: 10,
filename: 'Cultivation_Dashboard.pdf',
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2, logging: true, dpi: 192, letterRendering: true },
jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
pagebreak: { mode: ['avoid-all', 'css', 'legacy'] }
};
// Generate PDF from the temporary content wrapper
html2pdf().from(pdfContentWrapper).set(options).save();
// Clean up the temporary div (optional, as it's not appended to the DOM)
pdfContentWrapper.remove();
}
/**
* Loads initial sample data for the dashboard.
* Relevant to USA context.
*/
function loadSampleData() {
cultivationRecords = [
{ id: 'CULT001', cropType: 'Corn', cultivatedArea: 100, plantingDate: '2024-04-15', harvestDate: '2024-09-01', harvestedQuantity: 25000, waterUsage: 500000, fertilizerUsage: 1000, status: 'Harvested' },
{ id: 'CULT002', cropType: 'Soybeans', cultivatedArea: 150, plantingDate: '2024-05-01', harvestDate: '', harvestedQuantity: 0, waterUsage: 300000, fertilizerUsage: 750, status: 'Growing' },
{ id: 'CULT003', cropType: 'Wheat', cultivatedArea: 80, plantingDate: '2024-03-10', harvestDate: '2024-07-20', harvestedQuantity: 15000, waterUsage: 200000, fertilizerUsage: 600, status: 'Harvested' },
{ id: 'CULT004', cropType: 'Potatoes', cultivatedArea: 50, plantingDate: '2024-04-20', harvestDate: '', harvestedQuantity: 0, waterUsage: 150000, fertilizerUsage: 400, status: 'Growing' },
{ id: 'CULT005', cropType: 'Cotton', cultivatedArea: 200, plantingDate: '2024-05-10', harvestDate: '', harvestedQuantity: 0, waterUsage: 600000, fertilizerUsage: 1200, status: 'Planted' }
];
}