File size: 3,418 Bytes
22f8dde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# pdf_tool.py
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from crewai.tools.base_tool import BaseTool
from typing import Type
from pydantic import BaseModel, Field

class PDFToolInput(BaseModel):
    content: str = Field(..., description="Contenu de l'exposé en format Markdown")
    output_path: str = Field(..., description="Chemin de sortie du fichier PDF")

class PDFTool(BaseTool):
    name: str = "Generate PDF"
    description: str = "Outil pour générer un document PDF à partir de texte en format Markdown."
    args_schema: Type[BaseModel] = PDFToolInput

    def _run(
        self,
        content: str,
        output_path: str = "expose.pdf",
        **kwargs,
    ) -> str:
        try:
            doc = SimpleDocTemplate(output_path, pagesize=letter)
            styles = getSampleStyleSheet()

            # Ajout d'un style personnalisé pour le texte normal
            styles.add(ParagraphStyle(name='CustomBodyText',
                                      parent=styles['Normal'],
                                      fontSize=12,
                                      leading=14,
                                      spaceAfter=10))

            # Ajout d'un style personnalisé pour les titres de section
            styles.add(ParagraphStyle(name='CustomHeading1',
                                      parent=styles['Heading1'],
                                      fontSize=18,
                                      leading=22,
                                      spaceBefore=20,
                                      spaceAfter=6,
                                      textColor=colors.HexColor("#2c3e50"))) # Couleur bleu foncé

            # Ajout d'un style personnalisé pour les titres de sous-section
            styles.add(ParagraphStyle(name='CustomHeading2',
                                      parent=styles['Heading2'],
                                      fontSize=14,
                                      leading=18,
                                      spaceBefore=10,
                                      spaceAfter=4,
                                      textColor=colors.HexColor("#34495e"))) # Couleur bleu-gris

            # Séparer le contenu en sections en utilisant les sauts de ligne comme délimiteurs
            sections = content.split("\n\n")

            Story = []
            for section in sections:
                # Déterminer si la section est un titre de section, un titre de sous-section ou du texte normal
                if section.startswith("# "):
                    # Titre de section
                    title = section[2:].strip()
                    Story.append(Paragraph(title, styles["CustomHeading1"]))
                elif section.startswith("## "):
                    # Titre de sous-section
                    subtitle = section[3:].strip()
                    Story.append(Paragraph(subtitle, styles["CustomHeading2"]))
                else:
                    # Texte normal
                    Story.append(Paragraph(section, styles["CustomBodyText"]))

            doc.build(Story)
            return f"Fichier PDF généré avec succès : {output_path}"
        except Exception as e:
            return f"Erreur lors de la génération du PDF : {e}"