`;
}
const interpretationNote_pdf = `This analysis uses historical data. Past trends and SMA signals are not guarantees of future performance. Not financial advice.`;
pdfContentEl.innerHTML = `
Historical Price Trends Analysis: ${inputs.assetName}
Input Summary
${inputsHtml}
Overall Trend Statistics
${overallStatsHtml}
${smaHtml}
Note: ${interpretationNote_pdf}
`;
document.body.appendChild(pdfContentEl);
html2canvas(pdfContentEl, { scale: 2, useCORS: true, logging:true, width: 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();
const imgProps = pdf.getImageProperties(imgData);
let position = 0;
const pageMargin = 40;
const effectivePageHeight = pdfHeight - 2 * pageMargin;
const contentWidth = pdfWidth - 2 * pageMargin;
const contentHeight = (imgProps.height * contentWidth) / imgProps.width;
let heightLeft = contentHeight;
let pageNumber = 0;
while (heightLeft > 0) {
pageNumber++;
if (pageNumber > 1) {
pdf.addPage();
}
let sourceY = position * (imgProps.width / contentWidth);
let sourceHeight = effectivePageHeight * (imgProps.width / contentWidth);
sourceHeight = Math.min(sourceHeight, imgProps.height - sourceY);
const pageCanvas = document.createElement('canvas');
pageCanvas.width = imgProps.width;
pageCanvas.height = sourceHeight;
const ctx = pageCanvas.getContext('2d');
ctx.drawImage(canvas, 0, sourceY, imgProps.width, sourceHeight, 0, 0, imgProps.width, sourceHeight);
const pageImgData = pageCanvas.toDataURL('image/png');
const pageImgHeight = (sourceHeight * contentWidth) / imgProps.width;
pdf.addImage(pageImgData, 'PNG', pageMargin, pageMargin, contentWidth, pageImgHeight, undefined, 'FAST');
position += effectivePageHeight;
heightLeft -= effectivePageHeight;
}
pdf.save(`${inputs.assetName.replace(/[^a-z0-9]/gi, '_') || 'Price_Trend'}_Analysis.pdf`);
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("HPTA PDF Error:", err); alert("Error generating Trend Analysis PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
if (!document.getElementById('hpta_priceData')) {
console.error("Critical input 'hpta_priceData' not found. Tool may not function correctly.");
}
});
