${predictionCategory}
Confidence: ${confidenceLevel}
Inputs Summary:
${inputsSummaryHTML}
Disclaimer: This is a simulated prediction based on a heuristic model and does not constitute financial advice.
`;
resultsArea.style.display = 'block';
// Scroll to results
resultsArea.scrollIntoView({ behavior: 'smooth' });
}
async function generatePredictionPdf() {
const pdfContent = document.getElementById('pdf-content');
const downloadBtn = event.target;
if (!pdfContent) {
console.error("PDF content area not found.");
if(typeof alert === 'function') alert("Error: Could not find content to generate PDF.");
return;
}
if (!html2canvas || !jsPDF) {
console.error("html2canvas or jsPDF library not found.");
if(typeof alert === 'function') alert("Error: PDF generation library not loaded.");
return;
}
const originalButtonText = downloadBtn.textContent;
downloadBtn.textContent = "Generating...";
downloadBtn.disabled = true;
const pdfHeaderElement = pdfContent.querySelector('.pdf-header');
if (pdfHeaderElement) pdfHeaderElement.style.display = 'block';
try {
const canvas = await html2canvas(pdfContent, {
scale: 1.5, // Adjusted for file size
useCORS: true,
logging: false,
onclone: (doc) => {
const clonedHeader = doc.querySelector('.pdf-header');
if (clonedHeader) clonedHeader.style.display = 'block';
}
});
if (pdfHeaderElement) pdfHeaderElement.style.display = 'none';
const imgData = canvas.toDataURL('image/jpeg', 0.8); // Use JPEG for smaller size
const pdf = new jsPDF({
orientation: 'p',
unit: 'mm',
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgProps = pdf.getImageProperties(imgData);
const margin = 10;
const contentWidth = pdfWidth - 2 * margin;
const contentHeight = (imgProps.height * contentWidth) / imgProps.width;
if (contentHeight <= pdfHeight - 2 * margin) {
pdf.addImage(imgData, 'JPEG', margin, margin, contentWidth, contentHeight);
} else { // Basic multi-page handling for very tall content
let remainingImgHeight = imgProps.height;
let sourceY = 0;
const sourceWidth = imgProps.width;
const pageCanvasHeight = ((pdfHeight - 2 * margin) / contentWidth) * sourceWidth;
while (remainingImgHeight > 0) {
const currentChunkHeight = Math.min(remainingImgHeight, pageCanvasHeight);
const tempCanvas = document.createElement('canvas');
tempCanvas.width = sourceWidth;
tempCanvas.height = currentChunkHeight;
const tempCtx = tempCanvas.getContext('2d');
tempCtx.drawImage(canvas, 0, sourceY, sourceWidth, currentChunkHeight, 0, 0, sourceWidth, currentChunkHeight);
const chunkImgData = tempCanvas.toDataURL('image/jpeg', 0.8);
pdf.addImage(chunkImgData, 'JPEG', margin, margin, contentWidth, (currentChunkHeight * contentWidth) / sourceWidth);
remainingImgHeight -= currentChunkHeight;
sourceY += currentChunkHeight;
if (remainingImgHeight > 0) {
pdf.addPage();
}
}
}
const tickerSymbol = document.getElementById('tickerSymbol').value.toUpperCase() || "REPORT";
pdf.save(`${tickerSymbol}_Earnings_Surprise_Prediction.pdf`);
} catch (error) {
console.error("Error generating PDF:", error);
if(typeof alert === 'function') alert("An error occurred while generating the PDF: " + error.message);
} finally {
downloadBtn.textContent = originalButtonText;
downloadBtn.disabled = false;
if (pdfHeaderElement) pdfHeaderElement.style.display = 'none';
}
}