Sentiment Analysis Summary
${currentData.summary}
`;
downloadSection.classList.remove('hidden');
showTab(1);
};
generateBtn.addEventListener('click', generatePrediction);
// --- PDF DOWNLOAD ---
const downloadPDF = () => {
if (!currentData) return;
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const btnText = getElem('pdf-btn-text');
const btnSpinner = getElem('pdf-btn-spinner');
btnText.classList.add('hidden');
btnSpinner.classList.remove('hidden');
downloadPdfBtn.disabled = true;
try {
const pageWidth = pdf.internal.pageSize.getWidth();
const margin = 40;
let y = margin;
// Header
pdf.setFont('helvetica', 'bold');
pdf.setFontSize(20);
pdf.text("Stock Sentiment Analysis Report", pageWidth / 2, y, { align: 'center' });
y += 25;
pdf.setFont('helvetica', 'normal');
pdf.setFontSize(12);
pdf.text(`Stock: ${configSelections.stock}`, margin, y);
pdf.text(`Date: ${new Date().toLocaleDateString('en-US')}`, pageWidth - margin, y, { align: 'right' });
y += 15;
pdf.text(`Data Source: ${configSelections.source}`, margin, y);
y += 20;
// Prediction Summary Box
let fillColor;
if(currentData.outlookClass === 'bullish') fillColor = [236, 253, 245];
else if(currentData.outlookClass === 'bearish') fillColor = [254, 242, 242];
else fillColor = [248, 250, 252];
pdf.setFillColor.apply(null, fillColor);
pdf.setDrawColor(200);
pdf.roundedRect(margin, y, pageWidth - margin*2, 60, 5, 5, 'FD');
pdf.setFontSize(14);
pdf.setFont('helvetica', 'bold');
pdf.text(`Outlook: ${currentData.outlook}`, margin + 20, y + 25);
pdf.setFont('helvetica', 'normal');
pdf.text(`Confidence: ${currentData.confidence}`, pageWidth - margin - 20, y + 25, { align: 'right' });
y += 80;
// KPIs
pdf.autoTable({
startY: y,
head: [['Key Metric', 'Value']],
body: currentData.kpis.map(k => [k.name, k.value]),
theme: 'striped',
headStyles: { fillColor: [30, 64, 175] }, // blue-800
didDrawPage: (data) => {
y = data.cursor.y; // Update y to the position after the table
}
});
y += 20; // Add some margin after the table
// --- Helper Function to add text sections
const addSection = (title, body) => {
const splitText = pdf.splitTextToSize(body, pageWidth - margin*2);
if (y + 20 + (Array.isArray(body) ? body.length * 15 : splitText.length * 14) > pdf.internal.pageSize.getHeight() - margin) {
pdf.addPage();
y = margin;
}
pdf.setFont('helvetica', 'bold');
pdf.setFontSize(14);
pdf.text(title, margin, y);
y += 20;
pdf.setFont('helvetica', 'normal');
pdf.setFontSize(10);
if (Array.isArray(body)) {
body.forEach(item => {
pdf.text(`• ${item}`, margin + 10, y);
y += 15;
});
} else {
pdf.text(splitText, margin, y);
y += splitText.length * 14;
}
y += 20;
};
addSection("Analysis Summary", currentData.summary);
addSection("Key Sentiment Drivers", currentData.drivers);
addSection("Potential Risks", currentData.risks);
pdf.save('Stock-Sentiment-Report.pdf');
} catch(e) { console.error("PDF Generation failed:", e); }
finally {
btnText.classList.remove('hidden');
btnSpinner.classList.add('hidden');
downloadPdfBtn.disabled = false;
}
};
downloadPdfBtn.addEventListener('click', downloadPDF);
});