`;
} else {
resultsArea.innerHTML = `
Data not available for ticker: ${ticker}. Try SPY or QQQ.
`; } } async function generatePdf(type) { // type can be 'stock' or 'etf' const contentId = type === 'stock' ? 'pdf-content-stock' : 'pdf-content-etf'; const pdfContent = document.getElementById(contentId); const downloadBtn = event.target; // Get the button that was clicked if (!pdfContent) { console.error(`PDF content area #${contentId} 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, // Reduced scale useCORS: true, logging: false, onclone: (doc) => { const clonedHeader = doc.querySelector('.pdf-header'); if (clonedHeader) clonedHeader.style.display = 'block'; } }); if (pdfHeaderElement) pdfHeaderElement.style.display = 'none'; // Changed to JPEG with quality 0.8 const imgData = canvas.toDataURL('image/jpeg', 0.8); 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; let currentPosition = margin; if (contentHeight <= pdfHeight - 2 * margin) { pdf.addImage(imgData, 'JPEG', margin, currentPosition, contentWidth, contentHeight); // Changed to JPEG } else { let remainingImgHeight = imgProps.height; // Use original image height from canvas for calculations let sourceY = 0; const sourceWidth = imgProps.width; // Calculate how many canvas pixels correspond to one PDF page's height (considering aspect ratio and PDF content 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'); // Draw the chunk from the original large canvas to the temporary canvas tempCtx.drawImage(canvas, 0, sourceY, sourceWidth, currentChunkHeight, 0, 0, sourceWidth, currentChunkHeight); const chunkImgData = tempCanvas.toDataURL('image/jpeg', 0.8); // Changed to JPEG const chunkImgProps = pdf.getImageProperties(chunkImgData); const chunkDisplayHeight = (chunkImgProps.height * contentWidth) / chunkImgProps.width; pdf.addImage(chunkImgData, 'JPEG', margin, currentPosition, contentWidth, chunkDisplayHeight); // Changed to JPEG remainingImgHeight -= currentChunkHeight; sourceY += currentChunkHeight; if (remainingImgHeight > 0) { pdf.addPage(); currentPosition = margin; } } } const tickerInput = type === 'stock' ? document.getElementById('stockTickerInput').value : document.getElementById('etfTickerInput').value; pdf.save(`${tickerInput.toUpperCase()}_${type}_research.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'; } } // Initialize first tab document.addEventListener('DOMContentLoaded', () => { showResearchTab(0, researchTabLinks[0]); });