`;
downloadPdfButton.style.display = 'block'; // Show PDF button
if (switchToResultsTab) switchTab('pmia-tab-2');
return true;
}
// PDF Download Functionality
downloadPdfButton.addEventListener('click', function() {
const { jsPDF } = window.jspdf;
const contentToCapture = document.getElementById('pmia-pdf-content-wrapper');
if (!contentToCapture) {
alert("Error: Could not find content to generate PDF.");
return;
}
// Temporarily apply a class for PDF generation to ensure consistent styling if needed
// contentToCapture.classList.add('pmia-pdf-export-styles');
html2canvas(contentToCapture, {
scale: 2, // Higher scale for better quality
useCORS: true, // If any images were involved (not here, but good practice)
logging: false
}).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'p',
unit: 'mm',
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasWidth / canvasHeight;
let imgWidth = pdfWidth - 20; // With margin
let imgHeight = imgWidth / ratio;
if (imgHeight > pdfHeight - 20) { // If height exceeds page, scale by height
imgHeight = pdfHeight - 20;
imgWidth = imgHeight * ratio;
}
const x = (pdfWidth - imgWidth) / 2;
const y = 10; // Top margin
pdf.setFontSize(18);
pdf.setTextColor(parseInt("0073e6".substring(0,2),16), parseInt("0073e6".substring(2,4),16), parseInt("0073e6".substring(4,6),16)); // Using #0073e6
pdf.text("Precious Metals Investment Analysis", pdfWidth / 2, y + 5, { align: 'center' });
pdf.addImage(imgData, 'PNG', x, y + 15, imgWidth, imgHeight);
pdf.save('Precious_Metals_Investment_Analysis.pdf');
// contentToCapture.classList.remove('pmia-pdf-export-styles');
}).catch(err => {
console.error("Error generating PDF:", err);
alert("An error occurred while generating the PDF. Please try again.");
});
});
// Initial setup
updateNavButtons(); // Set initial state of nav buttons
switchTab('pmia-tab-1'); // Ensure first tab is active by default
});