Taking control of your finances is one of the most empowering steps you can take. For ${audience}, understanding the basics of ${topic.toLowerCase()} is the first step toward building a secure financial future. This guide will walk you through everything you need to know, in simple, actionable steps.
`,
Why: (topic) => `
Why ${topic} Matters
Why should you care about ${topic.toLowerCase()}? Simply put, it's the bedrock of financial stability. It allows you to manage your money effectively, reduce financial stress, and work towards your long-term goals, whether that's buying a home, traveling the world, or retiring comfortably. Without a solid grasp of this, you might find yourself living paycheck to paycheck, unable to get ahead.
`,
HowTo: (topic) => `
How to Get Started with ${topic}
Getting started is easier than you think. Here's a step-by-step breakdown:
- Step 1: Assess Your Current Situation. Gather all your financial documents, including bank statements and bills, to understand your income and expenses. This clarity is crucial. For example, you might realize you spend $200 per month on subscriptions you don't use.
- Step 2: Set Clear, Achievable Goals. What do you want to achieve? Define specific, measurable goals like 'save $5,000 for an emergency fund in one year.'
- Step 3: Choose the Right Tools. There are many apps and spreadsheet templates available to help you track your progress. Find one that works for you and stick with it.
`,
Tips: (topic) => `
Pro-Tips for Success
To master ${topic.toLowerCase()}, keep these tips in mind:
- Be Realistic: Don't set overly ambitious goals that lead to burnout. Small, consistent steps are more effective.
- Automate Your Finances: Set up automatic transfers for savings and bill payments to make the process effortless.
- Review and Adjust Regularly: Your financial situation will change. Review your plan monthly or quarterly to ensure it still aligns with your goals.
`,
Risks: (topic) => `
Common Pitfalls to Avoid
As you begin with ${topic.toLowerCase()}, watch out for these common mistakes:
- Ignoring Small Expenses: The daily coffee or lunch out can add up significantly. Track every dollar.
- Not Having an Emergency Fund: Unexpected expenses can derail your progress. Aim to have 3-6 months of living expenses saved.
- Getting Discouraged: You might have setbacks. The key is to stay persistent and not give up on your financial goals.
`,
Conclusion: (topic) => `
Conclusion: Take Charge of Your Future
Mastering ${topic.toLowerCase()} is not about restriction; it's about freedom. It gives you the power to make conscious decisions with your money and build the life you want. Start today, and you'll thank yourself in the future.
`
};
const generatePost = () => {
const topic = topicInput.value.trim();
const audience = audienceInput.value.trim();
const selectedSections = Array.from(document.querySelectorAll('#sections-container input:checked')).map(cb => cb.value);
if (!topic) {
postContainer.innerHTML = '
Please enter a topic for the blog post.
';
return;
}
let postHTML = `
A Beginner's Guide to ${topic} for ${audience}
`;
selectedSections.forEach(key => {
if (sectionTemplates[key]) {
postHTML += sectionTemplates[key](topic, audience);
}
});
postContainer.innerHTML = postHTML;
outputTitle.textContent = `Generated Post: ${topic}`;
downloadPdfBtn.classList.remove('hidden');
};
const downloadPdf = () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const content = getEl('generated-post-container');
const title = `A Beginner's Guide to ${topicInput.value.trim()}`;
const pageWidth = doc.internal.pageSize.getWidth();
const margin = 15;
const generationDate = new Date().toLocaleDateString('en-US');
// --- PDF Template: Financial Article ---
// Header
doc.setFillColor(6, 78, 59); // Emerald-900
doc.rect(0, 0, pageWidth, 30, 'F');
doc.setFont('helvetica', 'bold');
doc.setFontSize(16);
doc.setTextColor(255, 255, 255);
const splitTitle = doc.splitTextToSize(title, pageWidth - (margin * 2));
doc.text(splitTitle, margin, 15);
doc.setFont('helvetica', 'normal');
doc.setFontSize(9);
doc.setTextColor(209, 213, 219); // Gray-300
doc.text(`Published on: ${generationDate}`, margin, 25);
html2canvas(content, { scale: 2, useCORS: true, windowWidth: content.scrollWidth, windowHeight: content.scrollHeight }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const imgProps = doc.getImageProperties(imgData);
const pdfWidth = pageWidth - (margin * 2);
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
let heightLeft = pdfHeight;
let position = 40; // Start content below header
doc.addImage(imgData, 'PNG', margin, position, pdfWidth, pdfHeight, undefined, 'FAST');
heightLeft -= (doc.internal.pageSize.getHeight() - position - margin);
while (heightLeft >= 0) {
position = heightLeft - pdfHeight;
doc.addPage();
doc.addImage(imgData, 'PNG', margin, position, pdfWidth, pdfHeight, undefined, 'FAST');
heightLeft -= doc.internal.pageSize.getHeight();
}
// Footer on all pages
const pageCount = doc.internal.getNumberOfPages();
for(let i = 1; i <= pageCount; i++) {
doc.setPage(i);
doc.setDrawColor(16, 185, 129); // Emerald-500
doc.setLineWidth(0.2);
doc.line(margin, 282, pageWidth - margin, 282);
doc.setFontSize(8);
doc.setTextColor(107, 114, 128);
doc.text(
`Finance Blog Post Generator`,
margin,
287
);
doc.text(
`Page ${i} of ${pageCount}`,
pageWidth - margin,
287,
{ align: 'right' }
);
}
doc.save(`Finance_Post_${topicInput.value.trim().replace(/\s/g, '_')}.pdf`);
}).catch(err => {
console.error("Error generating PDF:", err);
alert("Could not generate PDF. See console for details.");
});
};
const showTab = (tabNum) => {
currentTab = tabNum;
tabButtons.forEach(btn => btn.classList.toggle('active', parseInt(btn.dataset.tab) === tabNum));
tabPanes.forEach(pane => pane.classList.toggle('hidden', parseInt(pane.id.split('-')[2]) !== tabNum));
prevBtn.classList.toggle('invisible', currentTab === 1);
nextBtn.textContent = currentTab === 1 ? 'Generate Post' : 'Generate Again';
};
// --- Event Listeners ---
tabButtons.forEach(button => {
button.addEventListener('click', () => {
const tabNum = parseInt(button.dataset.tab);
if (tabNum === 2 && currentTab === 1) generatePost();
showTab(tabNum);
});
});
prevBtn.addEventListener('click', () => { if (currentTab > 1) showTab(currentTab - 1) });
nextBtn.addEventListener('click', () => {
if (currentTab === 1) {
generatePost();
showTab(2);
} else {
generatePost();
}
});
downloadPdfBtn.addEventListener('click', downloadPdf);
// --- Initial Setup ---
showTab(1);
});