`;
buyerListEl.innerHTML += buyerCard;
});
};
/**
* Populates the final recommendations tab with simulated AI insights.
*/
const populateRecommendations = () => {
// Ensure the chart data is available
if (!valuationChartInstance) {
generateValuationModel();
}
// Get company name
document.getElementById('pdfCompanyName').textContent = companyNameInput.value || "your company";
const strategicData = valuationChartInstance.data.datasets[0].data;
const ipoData = valuationChartInstance.data.datasets[1].data;
// Find optimal exit window (simplified logic)
const maxValuation = Math.max(...strategicData);
const optimalYearIndex = strategicData.indexOf(String(maxValuation));
document.getElementById('pdfExitWindow').textContent = `Year ${optimalYearIndex + 1}-${optimalYearIndex + 2}`;
// Recommend best route (simplified logic)
document.getElementById('pdfExitRoute').textContent = Math.max(...strategicData) > Math.max(...ipoData) ? "Strategic Sale" : "IPO";
// Populate valuation table for PDF
const pdfValuationSection = document.getElementById('pdfValuationSection');
let valuationTable = `
| Exit Type |
Year 3 |
Year 4 |
Year 5 |
`;
valuationChartInstance.data.datasets.forEach(dataset => {
valuationTable += `
| ${dataset.label.replace(' Valuation ($)', '')} |
$${(dataset.data[2] / 1e6).toFixed(2)}M |
$${(dataset.data[3] / 1e6).toFixed(2)}M |
$${(dataset.data[4] / 1e6).toFixed(2)}M |
`;
});
valuationTable += `
`;
pdfValuationSection.querySelector('table')?.remove();
pdfValuationSection.insertAdjacentHTML('beforeend', valuationTable);
// Populate buyer table for PDF
if (!document.getElementById('buyerList').innerHTML) {
generateBuyerList();
}
const pdfBuyerSection = document.getElementById('pdfBuyerSection');
const buyers = Array.from(document.querySelectorAll('#buyerList > div'));
let buyerTable = `
| Acquirer Name |
Type |
Strategic Fit |
AI Notes |
`;
buyers.forEach(buyerCard => {
const name = buyerCard.querySelector('.text-indigo-700').innerText;
const type = buyerCard.querySelector('.text-sm.text-gray-500').innerText;
const fit = buyerCard.querySelector('.bg-blue-100').innerText.replace('Fit: ', '');
const notes = buyerCard.querySelector('.mt-2.text-gray-600').innerText;
buyerTable += `
| ${name} |
${type} |
${fit} |
${notes} |
`;
});
buyerTable += `
`;
pdfBuyerSection.querySelector('table')?.remove();
pdfBuyerSection.insertAdjacentHTML('beforeend', buyerTable);
};
/**
* Handles the PDF download functionality.
*/
const downloadPDF = async () => {
// Use the globally available jsPDF constructor
const { jsPDF } = window.jspdf;
const pdfElement = document.getElementById('pdf-output');
// Temporarily set a white background for capture if it's not already
const originalBg = pdfElement.style.backgroundColor;
pdfElement.style.backgroundColor = 'white';
// Use html2canvas to render the element to a canvas
const canvas = await html2canvas(pdfElement, {
scale: 2, // Improve resolution
useCORS: true
});
// Restore original background color
pdfElement.style.backgroundColor = originalBg;
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'p',
unit: 'mm',
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasWidth / canvasHeight;
const imgWidth = pdfWidth - 20; // with margin
const imgHeight = imgWidth / ratio;
let position = 10;
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
const companyName = companyNameInput.value || 'ExitPlan';
const safeCompanyName = companyName.replace(/[^a-zA-Z0-9]/g, '_'); // Sanitize for filename
pdf.save(`AI_Exit_Strategy_${safeCompanyName}.pdf`);
};
// --- Event Listeners ---
if(downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', downloadPDF);
} else {
console.error("PDF Download button not found.");
}
});