File size: 979 Bytes
c676e5d
056a86a
 
c676e5d
 
056a86a
 
 
 
c676e5d
056a86a
 
c676e5d
056a86a
c676e5d
056a86a
c676e5d
056a86a
c676e5d
 
 
 
 
056a86a
 
 
 
 
 
 
 
 
 
 
 
c676e5d
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
import os
import re
from fpdf import FPDF
from datetime import datetime

def generate_pdf(title: str, content: str) -> str:
    # Sanitize and truncate title for filename
    safe_title = re.sub(r'[\\/*?:"<>|\n]', "_", title)
    safe_title = safe_title.strip().replace(" ", "_").lower()

    # Truncate to max 50 characters to avoid long path errors
    safe_title = safe_title[:50]

    # Add timestamp to ensure uniqueness
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = f"{safe_title}_{timestamp}.pdf"

    # Ensure export directory exists
    export_dir = "exports"
    os.makedirs(export_dir, exist_ok=True)

    path = os.path.join(export_dir, filename)

    # Create the PDF
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)

    pdf.cell(200, 10, txt=title.upper(), ln=True, align="C")
    pdf.ln(10)

    for line in content.split("\n"):
        pdf.multi_cell(0, 10, txt=line.strip())

    pdf.output(path)
    return path