File size: 1,785 Bytes
c5cb534
0897b0a
b5b5722
c0d7423
da2c28a
c5cb534
c0d7423
 
 
 
 
 
 
 
a0f88c0
c0d7423
 
 
 
 
 
 
 
da2c28a
 
 
c0d7423
 
 
 
 
 
 
 
0897b0a
c0d7423
 
 
 
a0f88c0
 
 
 
 
 
 
 
c0d7423
da2c28a
c0d7423
 
da2c28a
c5cb534
c0d7423
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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