Spaces:
Running
Running
# genesis/utils/pdf_export.py | |
import os | |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer | |
from reportlab.lib.pagesizes import A4 | |
from reportlab.lib.styles import getSampleStyleSheet | |
def export_report_to_pdf(filename: str, title: str, summary: str, citations: list = None) -> str: | |
""" | |
Export a research report to a PDF file. | |
Args: | |
filename (str): The name of the output PDF file. | |
title (str): The report title. | |
summary (str): The report summary text. | |
citations (list): Optional list of citation strings or dicts. | |
Returns: | |
str: Path to the generated PDF file. | |
""" | |
output_dir = "outputs" | |
os.makedirs(output_dir, exist_ok=True) | |
file_path = os.path.join(output_dir, filename) | |
styles = getSampleStyleSheet() | |
story = [] | |
# Title | |
story.append(Paragraph(f"<b>{title}</b>", styles["Title"])) | |
story.append(Spacer(1, 12)) | |
# Summary | |
story.append(Paragraph("<b>Summary:</b>", styles["Heading2"])) | |
story.append(Paragraph(summary.replace("\n", "<br/>"), styles["Normal"])) | |
story.append(Spacer(1, 12)) | |
# Citations | |
if citations: | |
story.append(Paragraph("<b>Citations:</b>", styles["Heading2"])) | |
for c in citations: | |
if isinstance(c, dict): | |
# Build a readable string from citation fields | |
citation_str = ", ".join( | |
str(c.get(k, "")) for k in ["title", "authors", "year", "journal"] if c.get(k) | |
) | |
else: | |
citation_str = str(c) | |
story.append(Paragraph(citation_str, styles["Normal"])) | |
story.append(Spacer(1, 6)) | |
# Build PDF | |
doc = SimpleDocTemplate(file_path, pagesize=A4) | |
doc.build(story) | |
return file_path | |