DIY & Craft Blog Post Generator
Bring your creative projects to life with a beautifully written blog post.
Tell Us About Your Craft
Create Your Blog Post
Preview & Export Your Post
Your generated article will appear here.
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 = 60; const maxLineWidth = pageW - margin * 2; let cursorY = 0; // --- PDF Template: Crafty & Warm --- // Background color pdf.setFillColor(253, 250, 246); // #fdfaf6 pdf.rect(0, 0, pageW, pageH, 'F'); // Header accent pdf.setFillColor(229, 115, 115); // #e57373 pdf.rect(0, 0, pageW, 40, 'F'); cursorY = margin + 20; const addPageIfNeeded = (spaceNeeded) => { if (cursorY + spaceNeeded > pageH - margin) { pdf.addPage(); pdf.setFillColor(253, 250, 246); pdf.rect(0, 0, pageW, pageH, 'F'); pdf.setFillColor(229, 115, 115); pdf.rect(0, 0, pageW, 40, 'F'); cursorY = margin + 20; } }; 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(24).setTextColor(79, 70, 60) // Dark Brown .splitTextToSize(titleText, maxLineWidth); addPageIfNeeded(titleLines.length * 24 + 15); pdf.text(titleLines, pageW / 2, cursorY, { align: 'center' }); cursorY += titleLines.length * 24 + 20; } else if (line.startsWith('### ')) { cursorY += 15; const headingText = line.substring(4); const headingLines = pdf.setFont('helvetica', 'bold').setFontSize(16).setTextColor(196, 92, 92) // Dark Coral .splitTextToSize(headingText, maxLineWidth); addPageIfNeeded(headingLines.length * 16 + 10); pdf.text(headingLines, margin, cursorY); cursorY += headingLines.length * 16 + 8; pdf.setDrawColor(224, 216, 208).line(margin, cursorY - 4, margin + 100, cursorY - 4); } else if (line.startsWith('* ')) { const bulletLines = pdf.setFont('helvetica', 'normal').setFontSize(11).setTextColor(112, 100, 88) .splitTextToSize(line.substring(2), maxLineWidth - 20); addPageIfNeeded(bulletLines.length * 12 + 4); pdf.text('•', margin, cursorY); pdf.text(bulletLines, margin + 15, cursorY); cursorY += bulletLines.length * 12 + 4; } else if (line.trim() !== "") { const paraLines = pdf.setFont('helvetica', 'normal').setFontSize(11).setTextColor(99, 87, 75) .splitTextToSize(line, maxLineWidth); addPageIfNeeded(paraLines.length * 12 + 10); pdf.text(paraLines, margin, cursorY); cursorY += paraLines.length * 12 + 10; } } pdf.save(`DIY-Craft-Post-${(projectTitleEl.value || 'Untitled').replace(/\s+/g, '-')}.pdf`); }; // --- Event Listeners --- if (generateBtn) generateBtn.addEventListener('click', handleGenerateClick); if (downloadPdfBtn) downloadPdfBtn.addEventListener('click', handleDownloadPdf); });