Update pdf_generator.py
Browse files- pdf_generator.py +47 -60
pdf_generator.py
CHANGED
|
@@ -1,13 +1,17 @@
|
|
| 1 |
"""
|
| 2 |
Module pour la génération des contrats en format PDF.
|
|
|
|
| 3 |
"""
|
| 4 |
import io
|
| 5 |
from reportlab.pdfgen import canvas
|
| 6 |
from reportlab.lib.pagesizes import A4
|
| 7 |
from reportlab.lib.units import mm
|
| 8 |
-
from reportlab.platypus import SimpleDocTemplate
|
| 9 |
from reportlab.pdfbase import pdfform
|
| 10 |
from reportlab.lib.colors import black
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
from config import PDF_CONFIG
|
| 13 |
from contract_builder import ContractBuilder
|
|
@@ -19,6 +23,7 @@ def generate_pdf(contract_type, is_free, author_type, author_info,
|
|
| 19 |
additional_rights, remuneration, is_exclusive):
|
| 20 |
"""
|
| 21 |
Génère un PDF du contrat avec des champs interactifs.
|
|
|
|
| 22 |
|
| 23 |
Args:
|
| 24 |
contract_type (list): Liste des types de contrats sélectionnés
|
|
@@ -45,29 +50,34 @@ def generate_pdf(contract_type, is_free, author_type, author_info,
|
|
| 45 |
# Créer un nom de fichier temporaire pour le PDF
|
| 46 |
output_filename = create_temp_file(prefix="contrat_cession_", suffix=".pdf")
|
| 47 |
|
| 48 |
-
# Générer le contenu du contrat
|
| 49 |
contract_elements = ContractBuilder.build_contract_elements(
|
| 50 |
contract_type, is_free_bool, author_type, author_info,
|
| 51 |
work_description, image_description, final_supports,
|
| 52 |
additional_rights, remuneration, is_exclusive_bool
|
| 53 |
)
|
| 54 |
|
| 55 |
-
# Créer un document PDF
|
| 56 |
buffer = io.BytesIO()
|
| 57 |
-
doc = SimpleDocTemplate(buffer, pagesize=A4,
|
| 58 |
-
rightMargin=PDF_CONFIG['margin_right']*mm,
|
| 59 |
-
leftMargin=PDF_CONFIG['margin_left']*mm,
|
| 60 |
-
topMargin=PDF_CONFIG['margin_top']*mm,
|
| 61 |
-
bottomMargin=PDF_CONFIG['margin_bottom']*mm)
|
| 62 |
|
| 63 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
doc.build(contract_elements)
|
| 65 |
|
| 66 |
# Ajouter des champs interactifs pour les signatures
|
| 67 |
buffer.seek(0)
|
| 68 |
-
buffer_champs = io.BytesIO()
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
form = p.acroForm
|
| 72 |
|
| 73 |
# Déterminer le nom du cédant en fonction du type de contrat
|
|
@@ -78,27 +88,23 @@ def generate_pdf(contract_type, is_free, author_type, author_info,
|
|
| 78 |
else:
|
| 79 |
cedant_label = "le Modèle"
|
| 80 |
|
| 81 |
-
#
|
| 82 |
form.textfield(name='lieu', tooltip='Lieu de signature',
|
| 83 |
-
x=80, y=
|
| 84 |
borderWidth=0, forceBorder=True)
|
| 85 |
|
| 86 |
-
# Champ pour la date
|
| 87 |
form.textfield(name='date', tooltip='Date de signature',
|
| 88 |
-
x=230, y=
|
| 89 |
borderWidth=0, forceBorder=True)
|
| 90 |
|
| 91 |
-
# Champ pour la mention "Lu et approuvé" du cédant
|
| 92 |
form.textfield(name='mention_cedant', tooltip='Mention "Lu et approuvé"',
|
| 93 |
x=70, y=95, width=150, height=15,
|
| 94 |
borderWidth=0, forceBorder=True)
|
| 95 |
|
| 96 |
-
# Champ pour la mention "Lu et approuvé" du cessionnaire
|
| 97 |
form.textfield(name='mention_cessionnaire', tooltip='Mention "Lu et approuvé"',
|
| 98 |
x=350, y=95, width=150, height=15,
|
| 99 |
borderWidth=0, forceBorder=True)
|
| 100 |
|
| 101 |
-
# Champs pour les signatures
|
| 102 |
form.textfield(name='signature_cedant', tooltip=f'Signature de {cedant_label}',
|
| 103 |
x=70, y=60, width=150, height=30,
|
| 104 |
borderWidth=0, forceBorder=True)
|
|
@@ -107,59 +113,40 @@ def generate_pdf(contract_type, is_free, author_type, author_info,
|
|
| 107 |
x=350, y=60, width=150, height=30,
|
| 108 |
borderWidth=0, forceBorder=True)
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
#
|
| 112 |
-
page_count = doc.page_count
|
| 113 |
-
for page in range(1, page_count):
|
| 114 |
-
p.showPage() # Aller à la page suivante
|
| 115 |
-
form.textfield(name=f'paraphe_cedant_{page}', tooltip=f'Paraphe {cedant_label} - page {page}',
|
| 116 |
-
x=70, y=30, width=50, height=20,
|
| 117 |
-
borderWidth=0, forceBorder=True)
|
| 118 |
-
form.textfield(name=f'paraphe_cessionnaire_{page}', tooltip=f'Paraphe Cessionnaire - page {page}',
|
| 119 |
-
x=350, y=30, width=50, height=20,
|
| 120 |
-
borderWidth=0, forceBorder=True)
|
| 121 |
|
| 122 |
# Finaliser et sauvegarder le PDF
|
| 123 |
p.save()
|
| 124 |
|
| 125 |
-
# Sauvegarder le PDF dans un fichier
|
| 126 |
with open(output_filename, 'wb') as f:
|
| 127 |
f.write(buffer.getvalue())
|
| 128 |
-
f.write(buffer_champs.getvalue())
|
| 129 |
|
| 130 |
return output_filename
|
| 131 |
|
| 132 |
|
| 133 |
-
def
|
| 134 |
"""
|
| 135 |
-
|
| 136 |
|
| 137 |
-
Args:
|
| 138 |
-
pdf_path (str): Chemin vers le PDF
|
| 139 |
-
contract_type (list): Liste des types de contrats sélectionnés
|
| 140 |
-
|
| 141 |
Returns:
|
| 142 |
-
|
| 143 |
"""
|
| 144 |
-
|
| 145 |
-
#
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
# Sauvegarder le PDF
|
| 163 |
-
p.save()
|
| 164 |
-
|
| 165 |
-
return output_filename
|
|
|
|
| 1 |
"""
|
| 2 |
Module pour la génération des contrats en format PDF.
|
| 3 |
+
Module optimisé pour une génération plus rapide et efficace.
|
| 4 |
"""
|
| 5 |
import io
|
| 6 |
from reportlab.pdfgen import canvas
|
| 7 |
from reportlab.lib.pagesizes import A4
|
| 8 |
from reportlab.lib.units import mm
|
| 9 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 10 |
from reportlab.pdfbase import pdfform
|
| 11 |
from reportlab.lib.colors import black
|
| 12 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 13 |
+
from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY
|
| 14 |
+
import time
|
| 15 |
|
| 16 |
from config import PDF_CONFIG
|
| 17 |
from contract_builder import ContractBuilder
|
|
|
|
| 23 |
additional_rights, remuneration, is_exclusive):
|
| 24 |
"""
|
| 25 |
Génère un PDF du contrat avec des champs interactifs.
|
| 26 |
+
Version optimisée pour une génération plus rapide.
|
| 27 |
|
| 28 |
Args:
|
| 29 |
contract_type (list): Liste des types de contrats sélectionnés
|
|
|
|
| 50 |
# Créer un nom de fichier temporaire pour le PDF
|
| 51 |
output_filename = create_temp_file(prefix="contrat_cession_", suffix=".pdf")
|
| 52 |
|
| 53 |
+
# Générer le contenu du contrat - version simplifiée pour plus de rapidité
|
| 54 |
contract_elements = ContractBuilder.build_contract_elements(
|
| 55 |
contract_type, is_free_bool, author_type, author_info,
|
| 56 |
work_description, image_description, final_supports,
|
| 57 |
additional_rights, remuneration, is_exclusive_bool
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# Créer un document PDF avec moins d'options pour accélérer la génération
|
| 61 |
buffer = io.BytesIO()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# Utiliser des marges plus petites et des réglages plus simples
|
| 64 |
+
doc = SimpleDocTemplate(
|
| 65 |
+
buffer,
|
| 66 |
+
pagesize=A4,
|
| 67 |
+
rightMargin=15*mm,
|
| 68 |
+
leftMargin=15*mm,
|
| 69 |
+
topMargin=15*mm,
|
| 70 |
+
bottomMargin=15*mm
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Construire le document en une seule passe
|
| 74 |
doc.build(contract_elements)
|
| 75 |
|
| 76 |
# Ajouter des champs interactifs pour les signatures
|
| 77 |
buffer.seek(0)
|
|
|
|
| 78 |
|
| 79 |
+
# Version simplifiée des champs interactifs dans un second fichier
|
| 80 |
+
p = canvas.Canvas(output_filename, pagesize=A4)
|
| 81 |
form = p.acroForm
|
| 82 |
|
| 83 |
# Déterminer le nom du cédant en fonction du type de contrat
|
|
|
|
| 88 |
else:
|
| 89 |
cedant_label = "le Modèle"
|
| 90 |
|
| 91 |
+
# Ajouter seulement les champs essentiels
|
| 92 |
form.textfield(name='lieu', tooltip='Lieu de signature',
|
| 93 |
+
x=80, y=120, width=100, height=15,
|
| 94 |
borderWidth=0, forceBorder=True)
|
| 95 |
|
|
|
|
| 96 |
form.textfield(name='date', tooltip='Date de signature',
|
| 97 |
+
x=230, y=120, width=100, height=15,
|
| 98 |
borderWidth=0, forceBorder=True)
|
| 99 |
|
|
|
|
| 100 |
form.textfield(name='mention_cedant', tooltip='Mention "Lu et approuvé"',
|
| 101 |
x=70, y=95, width=150, height=15,
|
| 102 |
borderWidth=0, forceBorder=True)
|
| 103 |
|
|
|
|
| 104 |
form.textfield(name='mention_cessionnaire', tooltip='Mention "Lu et approuvé"',
|
| 105 |
x=350, y=95, width=150, height=15,
|
| 106 |
borderWidth=0, forceBorder=True)
|
| 107 |
|
|
|
|
| 108 |
form.textfield(name='signature_cedant', tooltip=f'Signature de {cedant_label}',
|
| 109 |
x=70, y=60, width=150, height=30,
|
| 110 |
borderWidth=0, forceBorder=True)
|
|
|
|
| 113 |
x=350, y=60, width=150, height=30,
|
| 114 |
borderWidth=0, forceBorder=True)
|
| 115 |
|
| 116 |
+
# Ne pas essayer d'ajouter des champs de paraphe sur chaque page
|
| 117 |
+
# Cela causait une erreur car SimpleDocTemplate n'a pas d'attribut page_count
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# Finaliser et sauvegarder le PDF
|
| 120 |
p.save()
|
| 121 |
|
| 122 |
+
# Sauvegarder le PDF dans un fichier
|
| 123 |
with open(output_filename, 'wb') as f:
|
| 124 |
f.write(buffer.getvalue())
|
|
|
|
| 125 |
|
| 126 |
return output_filename
|
| 127 |
|
| 128 |
|
| 129 |
+
def get_simplified_styles():
|
| 130 |
"""
|
| 131 |
+
Retourne des styles simplifiés pour une génération plus rapide.
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
Returns:
|
| 134 |
+
dict: Dictionnaire des styles simplifiés
|
| 135 |
"""
|
| 136 |
+
styles = getSampleStyleSheet()
|
| 137 |
+
# Utiliser des styles plus simples avec moins d'options
|
| 138 |
+
styles.add(ParagraphStyle(name='ContractTitle',
|
| 139 |
+
fontName='Helvetica-Bold',
|
| 140 |
+
fontSize=14,
|
| 141 |
+
alignment=TA_CENTER,
|
| 142 |
+
spaceAfter=10))
|
| 143 |
+
styles.add(ParagraphStyle(name='ContractText',
|
| 144 |
+
fontName='Helvetica',
|
| 145 |
+
fontSize=10,
|
| 146 |
+
alignment=TA_JUSTIFY,
|
| 147 |
+
spaceAfter=5))
|
| 148 |
+
styles.add(ParagraphStyle(name='ContractArticle',
|
| 149 |
+
fontName='Helvetica-Bold',
|
| 150 |
+
fontSize=11,
|
| 151 |
+
spaceAfter=5))
|
| 152 |
+
return styles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|