from fpdf import FPDF from datetime import datetime class ReportGenerator: def __init__(self): self.pdf = FPDF() self.pdf.add_page() self.pdf.set_font("Helvetica", size=12) def add_header(self): self.pdf.set_font("Helvetica", style="B", size=16) self.pdf.cell(200, 10, txt="Marketing Content Validation Report", ln=True, align='C') self.pdf.set_font("Helvetica", size=10) self.pdf.cell(200, 10, txt=f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='R') self.pdf.ln(10) def add_input_text(self, text): self.pdf.set_font("Helvetica", style="B", size=12) self.pdf.cell(200, 10, txt="Input Marketing Content:", ln=True) self.pdf.set_font("Helvetica", size=12) # Clean and encode the text cleaned_text = (text .replace('"', '"') .replace('"', '"') .replace(''', "'") .replace(''', "'") .replace('—', '-') .replace('–', '-') .encode('ascii', 'replace') .decode() ) self.pdf.multi_cell(0, 10, txt=cleaned_text) self.pdf.ln(10) def add_check_result(self, check_name, status, details=None): status_symbols = { "pass": "(PASS)", "fail": "(FAIL)", "warning": "(WARNING)" } self.pdf.set_font("Helvetica", style="B", size=12) status_symbol = status_symbols.get(status.lower(), "(?)") self.pdf.cell(0, 10, txt=f"{check_name} {status_symbol}", ln=True) if details: self.pdf.set_font("Helvetica", size=10) # Clean and encode the details cleaned_details = (details .replace('"', '"') .replace('"', '"') .replace(''', "'") .replace(''', "'") .replace('—', '-') .replace('–', '-') .encode('ascii', 'replace') .decode() ) self.pdf.multi_cell(0, 10, txt=cleaned_details) self.pdf.ln(5) def save_report(self): timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"marketing_report_{timestamp}.pdf" try: self.pdf.output(filename) return filename except Exception as e: print(f"Error saving PDF: {str(e)}") return None