Competitive Landscape
${competitorRows}
Opportunity Assessment
${currentData.opportunity}
Risk Assessment
${currentData.risk}
`;
downloadSection.classList.remove('hidden');
showTab(1);
};
generateReportBtn.addEventListener('click', generateReport);
// --- 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 = 0;
// Title Page
pdf.setFontSize(26);
pdf.setFont('helvetica', 'bold');
pdf.text("Market Intelligence Report", pageWidth / 2, 200, { align: 'center' });
pdf.setFontSize(16);
pdf.setFont('helvetica', 'normal');
pdf.text("Alternative Data Analysis", pageWidth / 2, 230, { align: 'center' });
pdf.setFontSize(12);
pdf.text(`Subject: ${currentData.title}`, pageWidth / 2, 280, { align: 'center' });
pdf.text(`Date: ${new Date().toLocaleDateString('en-US')}`, pageWidth / 2, 300, { align: 'center' });
pdf.addPage();
y = margin;
// --- Helper Function to add sections
const addSection = (title, bodyText) => {
const splitText = pdf.splitTextToSize(bodyText, pageWidth - margin*2);
if (y + 20 + 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);
pdf.text(splitText, margin, y);
y += splitText.length * 14 + 20;
}
// --- Report Content ---
addSection("1. Data Overview", currentData.overview);
addSection("2. Trend Analysis", currentData.analysis);
// KPIs Table
pdf.autoTable({
startY: y,
head: [['Key Performance Indicator', 'Value', 'Change (Q/Q)']],
body: currentData.kpis.map(k => [k.name, k.value, k.change]),
theme: 'striped',
headStyles: { fillColor: [22, 78, 216] }, // blue-700
didDrawPage: (data) => { y = data.cursor.y + 20; }
});
// Competitor Table
y = pdf.autoTable.previous.finalY + 30;
pdf.autoTable({
startY: y,
head: [['Competitor', 'Insight']],
body: currentData.competitors.map(c => [c.name, c.insight]),
theme: 'grid',
didDrawPage: (data) => { y = data.cursor.y; }
});
y = pdf.autoTable.previous.finalY + 30;
addSection("3. Opportunity Assessment", currentData.opportunity);
addSection("4. Risk Assessment", currentData.risk);
pdf.save('Market-Intelligence-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);
});