`;
html += `
`;
outputDiv.innerHTML = html;
document.getElementById('aip_downloadPdfButton').style.display = 'block';
}
function aip_downloadPDF() {
if (typeof jspdf === 'undefined' || typeof jspdf.jsPDF === 'undefined' || typeof html2canvas === 'undefined') {
alert('PDF generation libraries not loaded.'); return;
}
if (!aip_pdfDataStore || !aip_pdfDataStore.suggestion || !aip_pdfDataStore.suggestion.portfolioName) {
alert('Please get a suggestion first to generate a report.'); return;
}
console.log("aip_downloadPDF called, data:", aip_pdfDataStore);
const { jsPDF } = jspdf;
const pdf = new jsPDF('p', 'pt', 'a4');
const pdfCreationTimestamp = new Date().toLocaleString();
const pdfContentEl = document.createElement('div');
pdfContentEl.style.position = 'absolute'; pdfContentEl.style.left = '-9999px';
pdfContentEl.style.width = '550pt'; pdfContentEl.style.fontFamily = 'Arial, sans-serif';
const { inputs, suggestion } = aip_pdfDataStore;
let inputsHtml = `
`;
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}
Suggested Portfolio Archetype: ${portfolioName}
| Asset Class | Suggested Allocation (%) | Visualization |
|---|---|---|
| Domestic Stocks | ${allocation.domesticStocks}% | |
| International Stocks | ${allocation.internationalStocks}% | |
| Bonds | ${allocation.bonds}% | |
| Cash / Cash Equivalents | ${allocation.cash}% | |
| Alternatives | ${allocation.alternatives}% |
Rationale for this Suggestion:
- ${rationale.map(r => `
- ${r} `).join('')}
Primary Goal: ${inputs.goal}
Time Horizon: ${inputs.timeHorizon}
Risk Tolerance: ${inputs.riskTolerance}
Investment Knowledge: ${inputs.knowledge}
Liquidity Need: ${inputs.liquidityNeed}
- ${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.");
}
});
