Update pdf_generator.py
Browse files- pdf_generator.py +61 -7
pdf_generator.py
CHANGED
|
@@ -4,6 +4,7 @@ Module optimisé pour une génération plus rapide et efficace.
|
|
| 4 |
Version corrigée pour l'encodage des caractères accentués et UTF-8.
|
| 5 |
"""
|
| 6 |
import io
|
|
|
|
| 7 |
from reportlab.pdfgen import canvas
|
| 8 |
from reportlab.lib.pagesizes import A4
|
| 9 |
from reportlab.lib.units import mm
|
|
@@ -21,10 +22,47 @@ from contract_builder import ContractBuilder
|
|
| 21 |
from utils import create_temp_file, ensure_default_supports
|
| 22 |
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
def generate_pdf(contract_type, is_free, author_type, author_info,
|
|
@@ -141,19 +179,35 @@ def get_simplified_styles():
|
|
| 141 |
dict: Dictionnaire des styles simplifiés
|
| 142 |
"""
|
| 143 |
styles = getSampleStyleSheet()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
# Utiliser des styles plus simples avec moins d'options
|
| 145 |
styles.add(ParagraphStyle(name='ContractTitle',
|
| 146 |
-
fontName=
|
| 147 |
fontSize=14,
|
| 148 |
alignment=TA_CENTER,
|
| 149 |
spaceAfter=10))
|
| 150 |
styles.add(ParagraphStyle(name='ContractText',
|
| 151 |
-
fontName=
|
| 152 |
fontSize=10,
|
| 153 |
alignment=TA_JUSTIFY,
|
| 154 |
spaceAfter=5))
|
| 155 |
styles.add(ParagraphStyle(name='ContractArticle',
|
| 156 |
-
fontName=
|
| 157 |
fontSize=11,
|
| 158 |
spaceAfter=5))
|
| 159 |
return styles
|
|
|
|
| 4 |
Version corrigée pour l'encodage des caractères accentués et UTF-8.
|
| 5 |
"""
|
| 6 |
import io
|
| 7 |
+
import os
|
| 8 |
from reportlab.pdfgen import canvas
|
| 9 |
from reportlab.lib.pagesizes import A4
|
| 10 |
from reportlab.lib.units import mm
|
|
|
|
| 22 |
from utils import create_temp_file, ensure_default_supports
|
| 23 |
|
| 24 |
|
| 25 |
+
# Définir le chemin vers le répertoire des polices
|
| 26 |
+
# Vous devrez ajuster ce chemin selon votre installation
|
| 27 |
+
FONT_DIR = os.path.join(os.path.dirname(__file__), 'fonts')
|
| 28 |
+
|
| 29 |
+
# Vérifier si le répertoire des polices existe, sinon le créer
|
| 30 |
+
if not os.path.exists(FONT_DIR):
|
| 31 |
+
os.makedirs(FONT_DIR)
|
| 32 |
+
|
| 33 |
+
# Fonction pour enregistrer une police si le fichier existe
|
| 34 |
+
def register_font_if_exists(font_name, font_file):
|
| 35 |
+
font_path = os.path.join(FONT_DIR, font_file)
|
| 36 |
+
if os.path.exists(font_path):
|
| 37 |
+
pdfmetrics.registerFont(TTFont(font_name, font_path))
|
| 38 |
+
return True
|
| 39 |
+
return False
|
| 40 |
+
|
| 41 |
+
# Essayer d'enregistrer les polices personnalisées, sinon utiliser les polices par défaut
|
| 42 |
+
use_custom_fonts = False
|
| 43 |
+
|
| 44 |
+
# Tentative d'enregistrement des polices personnalisées
|
| 45 |
+
if register_font_if_exists('Helvetica', 'Helvetica.ttf') and \
|
| 46 |
+
register_font_if_exists('Helvetica-Bold', 'Helvetica-Bold.ttf'):
|
| 47 |
+
use_custom_fonts = True
|
| 48 |
+
else:
|
| 49 |
+
# Utiliser la police DejaVu qui est souvent disponible sur les systèmes Linux et supporte l'UTF-8
|
| 50 |
+
dejavu_fonts = {
|
| 51 |
+
'DejaVuSans': 'DejaVuSans.ttf',
|
| 52 |
+
'DejaVuSans-Bold': 'DejaVuSans-Bold.ttf'
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
for font_name, font_file in dejavu_fonts.items():
|
| 56 |
+
register_font_if_exists(font_name, font_file)
|
| 57 |
+
|
| 58 |
+
# Si on ne trouve pas DejaVu, on peut essayer avec Liberation qui est aussi courante
|
| 59 |
+
liberation_fonts = {
|
| 60 |
+
'LiberationSans': 'LiberationSans-Regular.ttf',
|
| 61 |
+
'LiberationSans-Bold': 'LiberationSans-Bold.ttf'
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
for font_name, font_file in liberation_fonts.items():
|
| 65 |
+
register_font_if_exists(font_name, font_file)
|
| 66 |
|
| 67 |
|
| 68 |
def generate_pdf(contract_type, is_free, author_type, author_info,
|
|
|
|
| 179 |
dict: Dictionnaire des styles simplifiés
|
| 180 |
"""
|
| 181 |
styles = getSampleStyleSheet()
|
| 182 |
+
|
| 183 |
+
# Déterminer quelles polices utiliser en fonction de ce qui est disponible
|
| 184 |
+
if use_custom_fonts:
|
| 185 |
+
base_font = 'Helvetica'
|
| 186 |
+
bold_font = 'Helvetica-Bold'
|
| 187 |
+
elif register_font_if_exists('DejaVuSans', 'DejaVuSans.ttf'):
|
| 188 |
+
base_font = 'DejaVuSans'
|
| 189 |
+
bold_font = 'DejaVuSans-Bold'
|
| 190 |
+
elif register_font_if_exists('LiberationSans', 'LiberationSans-Regular.ttf'):
|
| 191 |
+
base_font = 'LiberationSans'
|
| 192 |
+
bold_font = 'LiberationSans-Bold'
|
| 193 |
+
else:
|
| 194 |
+
# Utiliser les polices par défaut de ReportLab
|
| 195 |
+
base_font = 'Helvetica'
|
| 196 |
+
bold_font = 'Helvetica-Bold'
|
| 197 |
+
|
| 198 |
# Utiliser des styles plus simples avec moins d'options
|
| 199 |
styles.add(ParagraphStyle(name='ContractTitle',
|
| 200 |
+
fontName=bold_font,
|
| 201 |
fontSize=14,
|
| 202 |
alignment=TA_CENTER,
|
| 203 |
spaceAfter=10))
|
| 204 |
styles.add(ParagraphStyle(name='ContractText',
|
| 205 |
+
fontName=base_font,
|
| 206 |
fontSize=10,
|
| 207 |
alignment=TA_JUSTIFY,
|
| 208 |
spaceAfter=5))
|
| 209 |
styles.add(ParagraphStyle(name='ContractArticle',
|
| 210 |
+
fontName=bold_font,
|
| 211 |
fontSize=11,
|
| 212 |
spaceAfter=5))
|
| 213 |
return styles
|