`;
doc.innerHTML = html;
}
function toRoman(num) {
const roman = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1};
let str = '';
for (let i of Object.keys(roman)) {
let q = Math.floor(num / roman[i]);
num -= q * roman[i];
str += i.repeat(q);
}
return str;
}
// --- PDF Export ---
window.generatePDF = async function() {
const btn = document.getElementById('wt-export-pdf');
const originalText = btn.innerHTML;
btn.innerHTML = ' Generating...';
const element = document.getElementById('wt-document-render');
try {
const canvas = await html2canvas(element, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'mm', 'a4');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
// Logic to split pages is complex in simple JS, so we scale to fit logic or single page snapshot
// For a will, font readability is key.
// We will simply print the captured image. Real world apps generate text directly for better pagination.
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
// Basic multi-page handling if image is too long
let heightLeft = imgHeight;
let position = 0;
pdf.addImage(imgData, 'PNG', 0, position, pdfWidth, imgHeight);
heightLeft -= pdfHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(imgData, 'PNG', 0, position, pdfWidth, imgHeight);
heightLeft -= pdfHeight;
}
const name = document.getElementById('in-name').value.replace(/ /g, '_') || "Testator";
pdf.save(`Last_Will_${name}.pdf`);
} catch (err) {
console.error(err);
alert("Error generating PDF. Please try again.");
} finally {
btn.innerHTML = originalText;
}
};
});
