Target Audience Strategy
Primary Demographics
${data.demographics}
Strategy for Base Voters
${data.baseVoters}
Strategy for Swing Voters
${data.swingVoters}
Budget & Communications
Fundraising Goal: ${data.budget}
Fundraising Strategy: ${data.fundraising}
Communications Strategy: ${data.comms}
`;
elements.planOutput.innerHTML = planHTML;
elements.planPlaceholder.style.display = 'none';
elements.planOutput.style.display = 'block';
elements.pdfArea.style.display = 'block';
showTab(0);
};
const downloadPDF = async () => {
if (typeof window.jspdf === 'undefined' || typeof window.html2canvas === 'undefined') {
alert('PDF libraries not loaded.');
return;
}
elements.pdfBtn.textContent = "Generating...";
elements.pdfBtn.disabled = true;
const { jsPDF } = window.jspdf;
const planContent = document.getElementById('plan-content');
// Temporarily remove contenteditable for cleaner capture
const editables = planContent.querySelectorAll('[contenteditable="true"]');
editables.forEach(el => el.setAttribute('contenteditable', 'false'));
try {
const canvas = await html2canvas(planContent, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'mm', 'a4');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
let heightLeft = imgHeight;
let position = 15; // Top margin
pdf.addImage(imgData, 'PNG', 10, position, pdfWidth - 20, imgHeight);
heightLeft -= (pdfHeight - 30);
while (heightLeft > 0) {
position = heightLeft - imgHeight + 15; // Recalculate position
pdf.addPage();
pdf.addImage(imgData, 'PNG', 10, position, pdfWidth - 20, imgHeight);
heightLeft -= (pdfHeight - 30);
}
pdf.save('campaign-plan.pdf');
} catch (error) {
console.error("PDF generation failed:", error);
alert("Sorry, there was an error creating the PDF.");
} finally {
// Restore contenteditable
editables.forEach(el => el.setAttribute('contenteditable', 'true'));
elements.pdfBtn.textContent = "Download Plan as PDF";
elements.pdfBtn.disabled = false;
}
};
elements.generateBtn.addEventListener("click", generatePlan);
elements.pdfBtn.addEventListener("click", downloadPDF);
showTab(0); // Initialize to first tab
});