Disclaimer: This calculator provides an estimate for illustrative purposes only and does not constitute financial advice. Market reactions to buybacks can vary.
Download Impact PDF
`;
resultsArea.style.display = 'block';
resultsArea.scrollIntoView({ behavior: 'smooth' });
}
async function generateBuybackPdf() {
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');
const screenHeaderElement = pdfContent.querySelector('.screen-only');
if (pdfHeaderElement) pdfHeaderElement.style.display = 'block';
if (screenHeaderElement) screenHeaderElement.style.display = 'none';
try {
const canvas = await html2canvas(pdfContent, {
scale: 1.5,
useCORS: true,
logging: false,
onclone: (doc) => {
const clonedHeader = doc.querySelector('.pdf-header');
if (clonedHeader) clonedHeader.style.display = 'block';
const clonedScreenHeader = doc.querySelector('.screen-only');
if (clonedScreenHeader) clonedScreenHeader.style.display = 'none';
}
});
if (pdfHeaderElement) pdfHeaderElement.style.display = 'none';
if (screenHeaderElement) screenHeaderElement.style.display = 'block';
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;
if (contentHeight <= pdfHeight - 2 * margin) {
pdf.addImage(imgData, 'JPEG', margin, margin, contentWidth, contentHeight);
} else {
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 companyName = document.getElementById('companyName').value.replace(/\s+/g, '_') || "Company";
pdf.save(`${companyName}_Buyback_Impact.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';
if (screenHeaderElement) screenHeaderElement.style.display = 'block';
}
}