Spaces:
Sleeping
Sleeping
| # 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}" |