Persuasive Research Paper Generator
Structure and write a compelling research paper, section by section.
Final Research Paper
Error: ${error.message}
`; } finally { button.disabled = false; buttonText.textContent = 'Strengthen Argument'; spinner.classList.add('hidden'); } }; async function callGeminiAPI(text, systemPrompt) { const apiKey = ""; const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=${apiKey}`; const payload = { contents: [{ parts: [{ text: text }] }], systemInstruction: { parts: [{ text: systemPrompt }] }, }; const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (!response.ok) { const errorBody = await response.json(); throw new Error(errorBody.error?.message || `API request failed with status ${response.status}`); } const result = await response.json(); const candidate = result.candidates?.[0]; if (candidate?.content?.parts?.[0]?.text) { return candidate.content.parts[0].text; } else { throw new Error("Could not get a valid response from the AI model."); } } window.downloadPDF = function() { const { jsPDF } = window.jspdf; const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }); const paperTitle = document.getElementById('input-title')?.value.trim() || 'Untitled Research Paper'; const leftMargin = 20, topMargin = 25, contentWidth = doc.internal.pageSize.getWidth() - leftMargin * 2; const pageHeight = doc.internal.pageSize.getHeight(), bottomMargin = 20; const lineHeight = 6, headingFontSize = 12, bodyFontSize = 10; const font = 'Times'; let cursorY = 0; // --- Title Page --- doc.setFont(font, 'normal'); doc.setFontSize(18); const titleLines = doc.splitTextToSize(paperTitle, contentWidth - 20); const titleHeight = titleLines.length * 10; doc.text(titleLines, doc.internal.pageSize.getWidth() / 2, pageHeight / 2 - titleHeight, { align: 'center' }); doc.setFontSize(12); doc.text(`A Persuasive Research Paper`, doc.internal.pageSize.getWidth() / 2, pageHeight / 2, { align: 'center' }); doc.addPage(); cursorY = topMargin; // --- Main Content --- function addHeader() { doc.setFontSize(9); doc.setFont(font, 'italic'); doc.text(paperTitle.substring(0, 50) + (paperTitle.length > 50 ? '...' : ''), leftMargin, topMargin - 10); } function addFooter(pageNumber) { doc.setFontSize(10); doc.setFont(font, 'normal'); doc.text(String(pageNumber), doc.internal.pageSize.getWidth() / 2, pageHeight - 10, { align: 'center' }); } function checkPageBreak() { if (cursorY + (lineHeight * 2) > pageHeight - bottomMargin) { addFooter(doc.internal.getNumberOfPages()); doc.addPage(); cursorY = topMargin; addHeader(); } } addHeader(); tabsData.forEach(tab => { const textarea = document.getElementById(`textarea-${tab.id}`); if (!textarea || textarea.value.trim() === '') return; checkPageBreak(); cursorY += (lineHeight * 1.5); // Space between sections checkPageBreak(); doc.setFont(font, 'bold'); doc.setFontSize(headingFontSize); const sectionTitle = tab.id === 'title' ? 'Thesis Statement' : tab.title; const titleLines = doc.splitTextToSize(sectionTitle, contentWidth); doc.text(titleLines, leftMargin, cursorY); cursorY += (titleLines.length * (lineHeight + 1)); doc.setFont(font, 'normal'); doc.setFontSize(bodyFontSize); const bodyText = textarea.value.trim(); const splitBody = doc.splitTextToSize(bodyText, contentWidth); splitBody.forEach(line => { checkPageBreak(); doc.text(line, leftMargin, cursorY); cursorY += lineHeight; }); }); addFooter(doc.internal.getNumberOfPages()); doc.save(paperTitle.replace(/[^a-z0-9]/gi, '_').toLowerCase() + '.pdf'); } // Attach Event Listeners if (prevBtn && nextBtn && downloadPdfBtn) { prevBtn.addEventListener('click', () => handleNavigation(-1)); nextBtn.addEventListener('click', () => handleNavigation(1)); downloadPdfBtn.addEventListener('click', window.downloadPDF); } initializeUI(); });