News Article Generator
Create a professional news article from a few key points.
Generated Article Preview
Your generated article will appear here.
Go to the 'Data Configuration' tab to enter details.
Article Details
${byline}
${body}
`;
}
function showLoadingState(isLoading) {
if (isLoading) {
previewPlaceholder.classList.add('hidden');
articlePreview.innerHTML = '';
generateBtn.disabled = true;
generateBtn.textContent = 'Generating...';
showFeedback('Generating article, please wait...', false);
} else {
generateBtn.disabled = false;
generateBtn.textContent = 'Generate Article';
}
}
downloadPdfBtn.addEventListener('click', () => {
if (!lastGeneratedArticle) {
showFeedback('Please generate an article before downloading.', true, 3000);
return;
}
showFeedback('Generating PDF...', false, 2000);
try {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ unit: 'pt', format: 'letter' });
const { title, byline, body } = lastGeneratedArticle;
const pageMargin = 72; // 1 inch
const pageWidth = pdf.internal.pageSize.getWidth();
const textWidth = pageWidth - (pageMargin * 2);
let cursorY = pageMargin;
// Title
pdf.setFont('Helvetica', 'bold');
pdf.setFontSize(18);
const titleLines = pdf.splitTextToSize(title, textWidth);
pdf.text(titleLines, pageWidth / 2, cursorY, { align: 'center' });
cursorY += titleLines.length * 18 + 10;
// Byline
pdf.setFont('Helvetica', 'italic');
pdf.setFontSize(10);
const bylineLines = pdf.splitTextToSize(byline, textWidth);
pdf.text(bylineLines, pageWidth / 2, cursorY, { align: 'center' });
cursorY += bylineLines.length * 10 + 24;
// Body
pdf.setFont('Helvetica', 'normal');
pdf.setFontSize(12);
const bodyLines = pdf.splitTextToSize(body, textWidth);
pdf.text(bodyLines, pageMargin, cursorY);
pdf.save(`${title.replace(/[^a-zA-Z0-9]/g, '-').substring(0,20)}.pdf`);
} catch(err) {
console.error("PDF Generation Error: ", err);
showFeedback("Error generating PDF.", true, 3000);
}
});
let feedbackTimeout;
function showFeedback(message, isError = false, duration = 3000) {
clearTimeout(feedbackTimeout);
feedbackMessage.innerHTML = message;
feedbackMessage.style.color = isError ? '#ef4444' : '#4a5568';
if (duration) {
feedbackTimeout = setTimeout(() => { feedbackMessage.innerHTML = ''; }, duration);
}
}
});
