Research Paper Formatter
Structure and format your academic papers for professional presentation.
Note: This is a simplified preview. The final PDF will include page numbers and professional academic formatting.
Please go back and fill in at least one content section to generate a preview.
`; inputs.pdfButtonContainer.style.display = 'none'; } else { inputs.output.innerHTML = html; inputs.pdfButtonContainer.style.display = 'block'; } }; const downloadPDF = () => { try { const { jsPDF } = window.jspdf; const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }); const data = getPaperData(); const pageWidth = doc.internal.pageSize.getWidth(); const pageHeight = doc.internal.pageSize.getHeight(); const margin = 20; let cursorY = margin; let pageNumber = 1; const addPageNumber = (pageNum) => { doc.setFontSize(10); doc.setFont('helvetica', 'normal'); doc.text(`${pageNum}`, pageWidth / 2, pageHeight - 10, { align: 'center' }); }; const checkPageBreak = (spaceNeeded) => { if (cursorY + spaceNeeded > pageHeight - margin) { addPageNumber(pageNumber); doc.addPage(); pageNumber++; cursorY = margin; return true; } return false; }; // --- Title Page --- doc.setFont('times', 'bold'); doc.setFontSize(18); const titleLines = doc.splitTextToSize(data.title, pageWidth - margin * 2); doc.text(titleLines, pageWidth / 2, pageHeight / 2 - 40, { align: 'center' }); doc.setFont('times', 'normal'); doc.setFontSize(12); if (data.authors) doc.text(data.authors, pageWidth / 2, pageHeight / 2 - 10, { align: 'center' }); if (data.affiliation) doc.text(data.affiliation, pageWidth / 2, pageHeight / 2, { align: 'center' }); if (data.date) doc.text(data.date, pageWidth / 2, pageHeight / 2 + 10, { align: 'center' }); // --- Start Content on New Page --- doc.addPage(); pageNumber = 1; cursorY = margin; // --- Add Sections --- data.sections.forEach(sec => { if (!sec.content) return; checkPageBreak(20); // Space for header doc.setFont('times', 'bold'); doc.setFontSize(14); doc.text(sec.title, margin, cursorY); cursorY += 10; doc.setFont('times', 'normal'); doc.setFontSize(12); const contentLines = doc.splitTextToSize(sec.content, pageWidth - margin * 2); contentLines.forEach(line => { checkPageBreak(7); doc.text(line, margin, cursorY, { align: 'justify' }); cursorY += 7; // Line height for 12pt font }); cursorY += 10; // Space after section }); // Add final page number addPageNumber(pageNumber); doc.save(`${data.title.replace(/\s+/g, '_')}.pdf`); } catch (error) { console.error("Failed to generate PDF:", error); alert("An error occurred while generating the PDF. Please check the console for details."); } }; inputs.downloadPdfButton.addEventListener('click', downloadPDF); updateUI(); });