`;
}
// Function to handle the PDF download process
function downloadPDF() {
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-output');
if (!pdfContent) {
console.error("PDF content area not found.");
return;
}
// Use html2canvas to render the div to a canvas
html2canvas(pdfContent, { scale: 2 }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
// A4 dimensions in mm
const pdfWidth = 210;
const pdfHeight = 297;
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const canvasAspectRatio = canvasWidth / canvasHeight;
// Calculate image dimensions to fit A4 page width
const imgWidth = pdfWidth - 20; // with margin
const imgHeight = imgWidth / canvasAspectRatio;
const doc = new jsPDF({
orientation: 'p',
unit: 'mm',
format: 'a4'
});
let position = 10; // top margin
// If content is taller than one page, split it
if (imgHeight > pdfHeight - 20) {
let heightLeft = imgHeight;
let currentPosition = 0;
while(heightLeft > 0) {
doc.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight, undefined, 'FAST');
heightLeft -= (pdfHeight - 20);
if (heightLeft > 0) {
doc.addPage();
// The position needs to be negative to show the next part of the image
position -= (pdfHeight - 15);
}
}
} else {
doc.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight);
}
doc.save('Lease-Agreement.pdf');
});
}
});