Parenting Blog Post Generator
Craft heartfelt and helpful articles for every stage of parenthood.
Define Your Post
Write Your Article
Preview & Export
Your generated article will appear here.
${line}
`; } } previewBodyEl.innerHTML = bodyHtml.replace(/<\/ul>- /g, '') || '
Your generated article will appear here.
'; }; const handleDownloadPdf = () => { if (!window.jspdf) { console.error('jsPDF not loaded.'); return; } const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' }); const pageW = pdf.internal.pageSize.getWidth(); const pageH = pdf.internal.pageSize.getHeight(); const margin = 50; const maxLineWidth = pageW - margin * 2; let cursorY = 0; // --- PDF Template: Clean & Gentle --- // Header pdf.setFillColor(237, 242, 247); // #edf2f7 pdf.rect(0, 0, pageW, 70, 'F'); pdf.setFont('helvetica', 'bold').setFontSize(16).setTextColor(44, 82, 130); // #2c5282 pdf.text('Parenting Insights', margin, 45); cursorY = margin + 50; const addPageIfNeeded = (spaceNeeded) => { if (cursorY + spaceNeeded > pageH - margin) { pdf.addPage(); pdf.setFillColor(237, 242, 247); // #edf2f7 pdf.rect(0, 0, pageW, 70, 'F'); pdf.setFont('helvetica', 'bold').setFontSize(16).setTextColor(44, 82, 130); pdf.text('Parenting Insights', margin, 45); cursorY = margin + 50; } }; const articleText = generatedArticleEl.value || "No article generated."; const lines = articleText.split('\n'); for (let i = 0; i < lines.length; i++) { const line = lines[i]; if (line.startsWith('## ')) { const titleText = line.substring(3); const titleLines = pdf.setFont('helvetica', 'bold').setFontSize(22).setTextColor(45, 55, 72) // Dark Gray .splitTextToSize(titleText, maxLineWidth); addPageIfNeeded(titleLines.length * 22 + 20); pdf.text(titleLines, margin, cursorY); cursorY += titleLines.length * 22 + 20; } else if (line.startsWith('### ')) { cursorY += 15; const headingText = line.substring(4); const headingLines = pdf.setFont('helvetica', 'bold').setFontSize(15).setTextColor(66, 153, 225) // Medium Blue .splitTextToSize(headingText, maxLineWidth); addPageIfNeeded(headingLines.length * 15 + 10); pdf.text(headingLines, margin, cursorY); cursorY += headingLines.length * 15 + 10; } else if (line.startsWith('* ')) { const bulletLines = pdf.setFont('helvetica', 'normal').setFontSize(11).setTextColor(74, 85, 104) // Slate Gray .splitTextToSize(line.substring(2), maxLineWidth - 20); addPageIfNeeded(bulletLines.length * 12 + 4); pdf.setFillColor(99, 179, 237).circle(margin + 5, cursorY - 4, 2.5, 'F'); pdf.text(bulletLines, margin + 15, cursorY); cursorY += bulletLines.length * 12 + 4; } else if (line.trim() !== "") { const paraLines = pdf.setFont('helvetica', 'normal').setFontSize(11).setTextColor(45, 55, 72) // Dark Gray .splitTextToSize(line, maxLineWidth); addPageIfNeeded(paraLines.length * 12 + 10); pdf.text(paraLines, margin, cursorY); cursorY += paraLines.length * 12 + 10; } } pdf.save(`Parenting-Post-${(postTopicEl.value || 'Untitled').replace(/\s+/g, '-')}.pdf`); }; // --- Event Listeners --- if (generateBtn) generateBtn.addEventListener('click', handleGenerateClick); if (downloadPdfBtn) downloadPdfBtn.addEventListener('click', handleDownloadPdf); });