AI-Powered Blog Post Writer

AI-Powered Blog Post Writer

Generate high-quality blog content in seconds. Just provide a topic, keywords, and your desired style.

tags. - If lists are relevant, use

    or
      with
    1. tags. - Incorporate the provided keywords naturally throughout the text. - Do not include any introductory text like "Here is your blog post:" or any concluding remarks about the generation itself. Only provide the blog post content. - Ensure the tone and language are appropriate for the specified target audience.`; // User prompt provides the specific task for this request const userQuery = `Write a ${length} blog post for an audience of ${audience}. Topic: "${topic}" Keywords to include: "${keywords}" Tone of Voice: ${tone}`; const apiKey = ""; // This will be handled by the execution environment const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=${apiKey}`; try { const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ contents: [{ parts: [{ text: userQuery }] }], systemInstruction: { parts: [{ text: systemPrompt }] }, }) }); if (!response.ok) { throw new Error(`API request failed with status ${response.status}`); } const result = await response.json(); const candidate = result.candidates?.[0]; if (candidate && candidate.content?.parts?.[0]?.text) { blogPostOutput.innerHTML = candidate.content.parts[0].text; pdfDownloadSection.classList.remove('hidden'); } else { throw new Error("No content received from API."); } } catch (error) { console.error("Error calling Gemini API:", error); errorMessage.classList.remove('hidden'); } finally { loadingIndicator.classList.add('hidden'); loadingIndicator.classList.remove('flex'); } }; // --- PDF Generation --- const handlePdfDownload = () => { // Use the 'jsPDF' object from the global window scope const { jsPDF } = window.jspdf; if (!window.html2canvas || !jsPDF) { console.error("jsPDF or html2canvas library not loaded."); alert("Could not generate PDF. Required libraries are missing."); return; } const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' }); const contentToPrint = document.getElementById('blogPostOutput'); const topic = blogTopicInput.value.trim().replace(/[^a-z0-9]/gi, '_').toLowerCase() || 'blog_post'; // Temporarily remove padding for better canvas capture const originalPadding = contentToPrint.style.padding; contentToPrint.style.padding = '20px'; // Add some margin for the PDF content html2canvas(contentToPrint, { scale: 2, // Increase scale for better resolution useCORS: true }).then(canvas => { // Restore original padding contentToPrint.style.padding = originalPadding; const imgData = canvas.toDataURL('image/png'); const imgProps = doc.getImageProperties(imgData); const pdfWidth = doc.internal.pageSize.getWidth(); const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width; let heightLeft = pdfHeight; let position = 0; doc.addImage(imgData, 'PNG', 0, position, pdfWidth, pdfHeight); heightLeft -= doc.internal.pageSize.getHeight(); while (heightLeft >= 0) { position = heightLeft - pdfHeight; doc.addPage(); doc.addImage(imgData, 'PNG', 0, position, pdfWidth, pdfHeight); heightLeft -= doc.internal.pageSize.getHeight(); } doc.save(`${topic}.pdf`); }).catch(err => { console.error("Error generating PDF:", err); alert("An error occurred while generating the PDF."); // Restore padding on error contentToPrint.style.padding = originalPadding; }); }; // --- Event Listeners --- tabBtnConfigure.addEventListener('click', () => showTab('configure')); tabBtnGenerated.addEventListener('click', () => showTab('generated')); nextBtn.addEventListener('click', () => { if (currentTab === 'configure') { generateBlogPost(); } else { showTab('configure'); } }); prevBtn.addEventListener('click', () => { if (currentTab === 'generated') { showTab('configure'); } }); downloadPdfBtn.addEventListener('click', handlePdfDownload); });
Scroll to Top