Anticipated Opponent Strategy
${opponentStrategy.replace(/\n/g, '
')}
Recommended Next Steps
${nextSteps.map(step => `- ${step}
`).join('')}
`;
document.getElementById('report-output').innerHTML = reportHTML;
document.getElementById('pdf-preview').innerHTML = `
AI-Based Legal Strategy Report
` + reportHTML;
}
// --- PDF Download ---
const downloadPdfBtn = document.getElementById('download-pdf-btn');
async function downloadPDF() {
const { jsPDF } = window.jspdf;
const content = document.getElementById('pdf-preview');
const container = document.getElementById('pdf-container');
downloadPdfBtn.textContent = 'Generating...';
downloadPdfBtn.disabled = true;
container.classList.remove('hidden');
container.style.position = 'absolute';
container.style.left = '-9999px';
container.style.top = '0';
try {
const canvas = await html2canvas(content, { scale: 2, useCORS: true });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const margin = 40;
const usableWidth = pdfWidth - margin * 2;
const imgScaledHeight = (canvas.height * usableWidth) / canvas.width;
let heightLeft = imgScaledHeight;
let position = 0;
pdf.addImage(imgData, 'PNG', margin, margin, usableWidth, imgScaledHeight);
heightLeft -= (pdfHeight - margin * 2);
while (heightLeft > 0) {
position -= (pdfHeight - margin * 2);
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position + margin, usableWidth, imgScaledHeight);
heightLeft -= (pdfHeight - margin * 2);
}
pdf.save('AI-Legal-Strategy-Report.pdf');
} catch (error) {
console.error("Error generating PDF:", error);
} finally {
container.classList.add('hidden');
container.style.position = '';
container.style.left = '';
container.style.top = '';
downloadPdfBtn.textContent = 'Download PDF Report';
downloadPdfBtn.disabled = false;
}
}
downloadPdfBtn.addEventListener('click', downloadPDF);
updateDisplay();
});