Spaces:
Sleeping
Sleeping
Create pdf_tool.py
Browse files- pdf_tool.py +75 -0
pdf_tool.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pdf_tool.py
|
| 2 |
+
from reportlab.lib.pagesizes import letter
|
| 3 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph
|
| 4 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 5 |
+
from reportlab.lib import colors
|
| 6 |
+
from crewai.tools.base_tool import BaseTool
|
| 7 |
+
from typing import Type
|
| 8 |
+
from pydantic import BaseModel, Field
|
| 9 |
+
|
| 10 |
+
class PDFToolInput(BaseModel):
|
| 11 |
+
content: str = Field(..., description="Contenu de l'exposé en format Markdown")
|
| 12 |
+
output_path: str = Field(..., description="Chemin de sortie du fichier PDF")
|
| 13 |
+
|
| 14 |
+
class PDFTool(BaseTool):
|
| 15 |
+
name: str = "Generate PDF"
|
| 16 |
+
description: str = "Outil pour générer un document PDF à partir de texte en format Markdown."
|
| 17 |
+
args_schema: Type[BaseModel] = PDFToolInput
|
| 18 |
+
|
| 19 |
+
def _run(
|
| 20 |
+
self,
|
| 21 |
+
content: str,
|
| 22 |
+
output_path: str = "expose.pdf",
|
| 23 |
+
**kwargs,
|
| 24 |
+
) -> str:
|
| 25 |
+
try:
|
| 26 |
+
doc = SimpleDocTemplate(output_path, pagesize=letter)
|
| 27 |
+
styles = getSampleStyleSheet()
|
| 28 |
+
|
| 29 |
+
# Ajout d'un style personnalisé pour le texte normal
|
| 30 |
+
styles.add(ParagraphStyle(name='CustomBodyText',
|
| 31 |
+
parent=styles['Normal'],
|
| 32 |
+
fontSize=12,
|
| 33 |
+
leading=14,
|
| 34 |
+
spaceAfter=10))
|
| 35 |
+
|
| 36 |
+
# Ajout d'un style personnalisé pour les titres de section
|
| 37 |
+
styles.add(ParagraphStyle(name='CustomHeading1',
|
| 38 |
+
parent=styles['Heading1'],
|
| 39 |
+
fontSize=18,
|
| 40 |
+
leading=22,
|
| 41 |
+
spaceBefore=20,
|
| 42 |
+
spaceAfter=6,
|
| 43 |
+
textColor=colors.HexColor("#2c3e50"))) # Couleur bleu foncé
|
| 44 |
+
|
| 45 |
+
# Ajout d'un style personnalisé pour les titres de sous-section
|
| 46 |
+
styles.add(ParagraphStyle(name='CustomHeading2',
|
| 47 |
+
parent=styles['Heading2'],
|
| 48 |
+
fontSize=14,
|
| 49 |
+
leading=18,
|
| 50 |
+
spaceBefore=10,
|
| 51 |
+
spaceAfter=4,
|
| 52 |
+
textColor=colors.HexColor("#34495e"))) # Couleur bleu-gris
|
| 53 |
+
|
| 54 |
+
# Séparer le contenu en sections en utilisant les sauts de ligne comme délimiteurs
|
| 55 |
+
sections = content.split("\n\n")
|
| 56 |
+
|
| 57 |
+
Story = []
|
| 58 |
+
for section in sections:
|
| 59 |
+
# Déterminer si la section est un titre de section, un titre de sous-section ou du texte normal
|
| 60 |
+
if section.startswith("# "):
|
| 61 |
+
# Titre de section
|
| 62 |
+
title = section[2:].strip()
|
| 63 |
+
Story.append(Paragraph(title, styles["CustomHeading1"]))
|
| 64 |
+
elif section.startswith("## "):
|
| 65 |
+
# Titre de sous-section
|
| 66 |
+
subtitle = section[3:].strip()
|
| 67 |
+
Story.append(Paragraph(subtitle, styles["CustomHeading2"]))
|
| 68 |
+
else:
|
| 69 |
+
# Texte normal
|
| 70 |
+
Story.append(Paragraph(section, styles["CustomBodyText"]))
|
| 71 |
+
|
| 72 |
+
doc.build(Story)
|
| 73 |
+
return f"Fichier PDF généré avec succès : {output_path}"
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"Erreur lors de la génération du PDF : {e}"
|