`;
summaryHTML += `
`;
summaryHTML += `
Adjustment Factors Applied:
| Factor | Selection | Multiplier |
|---|---|---|
| ${adj.name} | ${adj.text} | ${adj.value.toFixed(2)}x |
Estimated Total Land Value: $${finalValue.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
`;
valuationSummaryOutput.innerHTML = summaryHTML;
}
if (downloadPdfButton) {
downloadPdfButton.addEventListener('click', function() {
if (!window.jspdf || !window.html2canvas) {
alert("PDF generation libraries are not loaded. Please check your internet connection or contact support.");
return;
}
const { jsPDF } = window.jspdf;
const pdfContent = pdfExportContent; // The div containing the summary
if (!pdfContent) {
alert("Error: PDF content area not found.");
return;
}
// Temporarily ensure full visibility for capture if any issues arise from hidden elements
// const originalDisplay = pdfContent.style.display;
// pdfContent.style.display = 'block'; // Ensure it's block for rendering
html2canvas(pdfContent, {
scale: 2, // Increase scale for better quality
useCORS: true, // If images from other domains are ever used
onclone: (document) => { // Ensure styles are applied if they are complex
// This can be used to re-apply styles in the cloned document if needed
// For instance, if styles are very dynamic or depend on classes outside the captured element.
}
}).then(canvas => {
// pdfContent.style.display = originalDisplay; // Restore original display if changed
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'p', // portrait
unit: 'pt', // points
format: 'a4' // A4 page size
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
// Calculate the aspect ratio
const ratio = canvasWidth / canvasHeight;
let imgWidth = pdfWidth - 40; // A4 width in points minus margins
let imgHeight = imgWidth / ratio;
// If the image height is still too large for the page, scale by height
if (imgHeight > pdfHeight - 40) {
imgHeight = pdfHeight - 40; // A4 height minus margins
imgWidth = imgHeight * ratio;
}
const x = (pdfWidth - imgWidth) / 2; // Center the image
const y = 20; // Margin from top
pdf.addImage(imgData, 'PNG', x, y, imgWidth, imgHeight);
const valuationDateForFile = valuationDateInput.value ? valuationDateInput.value : new Date().toISOString().split('T')[0];
pdf.save(`Land_Valuation_Summary_${valuationDateForFile}.pdf`);
}).catch(err => {
// pdfContent.style.display = originalDisplay; // Restore display in case of error
console.error("Error generating PDF:", err);
alert("An error occurred while generating the PDF. Please try again.");
});
});
}
// Initialize with the first tab active
switchTab(0);
});