`;
initialMessage.classList.add('hidden');
resultsContainer.classList.remove('hidden');
pdfButtonContainer.classList.remove('hidden');
};
analyzeBtn.addEventListener('click', () => {
const headlines = headlinesInput.value;
if (headlines.trim()) {
lastAnalysisData = analyzeSentiment(headlines);
displayAnalysis(lastAnalysisData);
}
});
downloadPdfBtn.addEventListener('click', () => {
if (!lastAnalysisData) return;
const { jsPDF } = window.jspdf;
const { score, label, color, positiveWords, negativeWords, headlines } = lastAnalysisData;
const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pageW = doc.internal.pageSize.getWidth();
const margin = 50;
let y = 0;
// PDF Header
doc.setFillColor('#f9fafb'); // gray-50
doc.rect(0, 0, pageW, 70, 'F');
doc.setFont('helvetica', 'bold');
doc.setFontSize(22);
doc.setTextColor('#10b981'); // emerald-500
doc.text('Financial Sentiment Report', margin, 45);
y = 110;
// Sentiment Score Visualization (Horizontal Bar)
const barWidth = pageW - margin * 2;
const barHeight = 20;
const scoreWidth = (score / 100) * barWidth;
const scoreColorHex = { "text-green-600": "#10b981", "text-red-600": "#ef4444", "text-gray-600": "#4b5563" }[color] || '#4b5563';
// Background bar
doc.setFillColor('#e5e7eb'); // gray-200
doc.rect(margin, y, barWidth, barHeight, 'F');
// Score bar
doc.setFillColor(scoreColorHex);
doc.rect(margin, y, scoreWidth, barHeight, 'F');
// Score Text
doc.setFontSize(12);
doc.setFont('helvetica', 'bold');
doc.setTextColor('#ffffff');
doc.text(`${score}/100`, margin + 5, y + barHeight / 2 + 4);
// Label Text
doc.setFontSize(14);
doc.setTextColor(scoreColorHex);
doc.text(label, pageW - margin, y + barHeight / 2 + 5, { align: 'right' });
y += barHeight + 40;
// Helper function for sections
const addSection = (title, textContent, isList = false) => {
if (y > doc.internal.pageSize.getHeight() - 120) { doc.addPage(); y = margin; }
doc.setFont('helvetica', 'bold');
doc.setFontSize(14);
doc.setTextColor('#374151'); // gray-700
doc.text(title, margin, y);
y += 20;
doc.setFont('helvetica', 'normal');
doc.setFontSize(11);
doc.setTextColor('#4b5563'); // gray-600
if (isList) {
textContent.forEach(item => {
doc.text(`• ${item}`, margin + 10, y);
y += 15;
});
} else {
const lines = doc.splitTextToSize(textContent, pageW - margin * 2);
doc.text(lines, margin, y);
y += lines.length * 14;
}
y += 20;
};
addSection('Analyzed Headlines', headlines);
addSection('Positive Keywords Detected', positiveWords.length > 0 ? positiveWords.join(', ') : 'None');
addSection('Negative Keywords Detected', negativeWords.length > 0 ? negativeWords.join(', ') : 'None');
// Footer
const date = `Report Generated: ${new Date().toLocaleDateString('en-US')}`;
doc.setFontSize(9);
doc.setTextColor('#9ca3af'); // gray-400
doc.text(date, pageW / 2, doc.internal.pageSize.getHeight() - 20, { align: 'center' });
doc.save('Sentiment_Report.pdf');
});
});