`;
document.getElementById('complaintPreview').innerHTML = previewHTML;
// Also populate the hidden div for better PDF rendering
document.getElementById('pdf-render-area').innerHTML = previewHTML;
}
/**
* Downloads the generated complaint as a PDF file.
*/
async function downloadPDF() {
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-render-area');
if (!pdfContent) {
console.error('PDF content area not found.');
return;
}
// Temporarily make the render area visible for html2canvas
pdfContent.classList.remove('hidden');
try {
const canvas = await html2canvas(pdfContent, {
scale: 2 // Increase scale for better resolution
});
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'portrait',
unit: 'in',
format: 'letter'
});
const imgProps = pdf.getImageProperties(imgData);
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save('Harassment_Complaint_Form.pdf');
} catch (error) {
console.error('Error generating PDF:', error);
} finally {
// Hide the render area again
pdfContent.classList.add('hidden');
}
}
// --- EVENT LISTENERS ---
if (downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', downloadPDF);
}
// --- INITIALIZATION ---
showTab(currentTab);
});
