`;
};
// --- PDF Download Logic ---
const downloadPdf = () => {
if (typeof window.jspdf === 'undefined') { return; }
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'letter' });
const page = { width: doc.internal.pageSize.getWidth(), height: doc.internal.pageSize.getHeight(), margin: 72 };
page.contentWidth = page.width - page.margin * 2;
let y = page.margin;
// Header
doc.setFont('helvetica', 'bold');
doc.setFontSize(24);
doc.text('Ethical Writing Compliance Report', page.width / 2, y, { align: 'center' });
y += 20;
doc.setFontSize(10);
doc.setFont('helvetica', 'normal');
doc.text(`Date Generated: ${new Date().toLocaleDateString()}`, page.width / 2, y, { align: 'center' });
y += 40;
// Overall Score
doc.setFontSize(14);
doc.setFont('helvetica', 'bold');
doc.text(`Overall Compliance Score: ${analysisResults.overallScore}%`, page.margin, y);
y += 30;
// Section Helper
const addSection = (title, score, content) => {
doc.setFontSize(14);
doc.setFont('helvetica', 'bold');
doc.text(`${title} (Score: ${score}/100)`, page.margin, y);
y += 20;
doc.setFontSize(10);
doc.setFont('helvetica', 'normal');
const lines = doc.splitTextToSize(content, page.contentWidth);
doc.text(lines, page.margin, y);
y += lines.length * 12 + 20;
};
// Report Sections
addSection('Bias Check', analysisResults.biasScore,
analysisResults.foundBiasTerms.length > 0
? 'The following terms could be replaced with more inclusive language:\n' + analysisResults.foundBiasTerms.map(t => ` - '${t.term}' (suggestion: ${t.suggestion})`).join('\n')
: 'No potential bias was detected in the text.'
);
addSection('Readability', analysisResults.readabilityScore,
`The analysis indicates the text's readability is ${analysisResults.readabilityScore > 80 ? 'good' : 'in need of improvement'}. Consider using shorter sentences and simpler words to reach a broader audience.`
);
addSection('Originality', analysisResults.clicheScore,
analysisResults.foundCliches.length > 0
? 'The following clichés were found and could be replaced with more original phrasing:\n' + analysisResults.foundCliches.map(c => ` - '${c}'`).join('\n')
: 'No common clichés were detected.'
);
addSection('Professional Tone', analysisResults.toneScore,
analysisResults.toneScore > 80
? 'The text maintains a professional and respectful tone.'
: 'Some words in the text may be perceived as negative. A review is recommended to ensure a professional tone.'
);
doc.addPage();
y = page.margin;
doc.setFontSize(14);
doc.setFont('helvetica', 'bold');
doc.text('Original Text Submitted for Analysis', page.margin, y);
y += 20;
doc.setFontSize(9);
doc.setFont('helvetica', 'normal');
const textLines = doc.splitTextToSize(analysisResults.originalText, page.contentWidth);
doc.text(textLines, page.margin, y);
doc.save('Ethical-Writing-Report.pdf');
};
// --- Event Listeners ---
analyzeBtn.addEventListener('click', analyzeText);
downloadPdfBtn.addEventListener('click', downloadPdf);
});