`;
let allocationTableHtml = `Asset Class Suggested Allocation (%)
Domestic Stocks ${suggestion.allocation.domesticStocks}%
International Stocks ${suggestion.allocation.internationalStocks}%
Bonds ${suggestion.allocation.bonds}%
Cash / Cash Equivalents ${suggestion.allocation.cash}%
${suggestion.allocation.alternatives > 0 ? `Alternatives ${suggestion.allocation.alternatives}% ` : ''}
`;
let rationaleHtml = `${allocationTableHtml}
- ${suggestion.rationale.map(r => `
- ${r.replace(/|<\/strong>/g, "")} `).join('')}
AI-Inspired Portfolio Allocation Suggestion
Your Investor Profile Summary
${inputsHtml}
Suggested Portfolio: ${suggestion.portfolioName}
Rationale for Suggestion
${rationaleHtml}
Important Disclaimer: ${disclaimer_pdf}
`;
document.body.appendChild(pdfContentEl);
html2canvas(pdfContentEl, { scale: 2, useCORS: true, logging:true, windowWidth: pdfContentEl.scrollWidth, windowHeight: pdfContentEl.scrollHeight }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
let numPages = Math.ceil(canvas.height / ( (pdfHeight - 40) * (canvas.width / (pdfWidth - 40)) ) );
let pageCanvasHeight = (pdfHeight - 40) * (canvas.width / (pdfWidth - 40));
for (let i = 0; i < numPages; i++) {
if (i > 0) pdf.addPage();
let sourceY = i * pageCanvasHeight;
let sourceHeight = Math.min(pageCanvasHeight, canvas.height - sourceY);
const tempCanvas = document.createElement('canvas');
tempCanvas.width = canvas.width;
tempCanvas.height = sourceHeight;
const ctx = tempCanvas.getContext('2d');
ctx.drawImage(canvas, 0, sourceY, canvas.width, sourceHeight, 0, 0, canvas.width, sourceHeight);
const pageImgData = tempCanvas.toDataURL('image/png');
const pageImgHeight = (sourceHeight * (pdfWidth - 40)) / canvas.width;
pdf.addImage(pageImgData, 'PNG', 20, 20, pdfWidth - 40, pageImgHeight, undefined, 'FAST');
}
pdf.save('Portfolio_Allocation_Suggestion.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("AIP PDF Error:", err); alert("Error generating PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
aip_openTab({}, 'aip_tab_profile');
if (!document.getElementById('aip_goal')) {
console.error("Critical input 'aip_goal' not found.");
}
});
