MISSAOUI commited on
Commit
104d480
·
verified ·
1 Parent(s): 79c8c22

Update pdf_generator.py

Browse files
Files changed (1) hide show
  1. pdf_generator.py +62 -64
pdf_generator.py CHANGED
@@ -1,85 +1,83 @@
1
  from reportlab.lib.pagesizes import A5
2
  from reportlab.lib import colors
3
- from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
4
  from reportlab.lib.styles import getSampleStyleSheet
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- def create_receipt(text, file_name):
7
- # Nettoyage basique
8
- clean = text.replace('*', '').replace('#', '').replace('Reçu', '')
9
-
10
- # Extraction des champs simples
11
- lines = [l.strip() for l in clean.split("\n") if l.strip()]
12
- data_map = {}
13
- description_lines = []
14
-
15
- for l in lines:
16
- if ":" in l:
17
- key, val = l.split(":", 1)
18
- data_map[key.strip()] = val.strip()
19
- else:
20
- description_lines.append(l)
21
-
22
- # Données extraites
23
- client = data_map.get("Client", "")
24
- telephone = data_map.get("Téléphone", "")
25
- adresse = data_map.get("Adresse", "")
26
- date_commande = data_map.get("Date de commande", "")
27
- delai = data_map.get("Délai de livraison estimé", "")
28
-
29
- # Description brute (ex: "3 iPhone (1 rouge, 2 blancs)")
30
- description = " ".join(description_lines)
31
-
32
- # Styles
33
  styles = getSampleStyleSheet()
34
- title_style = styles["Title"]
35
- normal = styles["Normal"]
36
-
37
- # Création du document
38
- doc = SimpleDocTemplate(file_name, pagesize=A5,
39
- leftMargin=25, rightMargin=25, topMargin=25, bottomMargin=25)
40
-
41
  elements = []
42
 
43
  # Titre
44
- elements.append(Paragraph("FACTURE", title_style))
45
- elements.append(Spacer(1, 12))
 
46
 
47
  # Informations client
48
- info_block = (
49
- f"<b>Client :</b> {client}<br/>"
50
- f"<b>Téléphone :</b> {telephone}<br/>"
51
  f"<b>Adresse :</b> {adresse}<br/>"
52
- f"<b>Date de commande :</b> {date_commande}<br/>"
53
- f"<b>Délai de livraison estimé :</b> {delai}<br/>"
54
  )
55
- elements.append(Paragraph(info_block, normal))
56
- elements.append(Spacer(1, 18))
57
 
58
- # Tableau détaillé
59
  table_data = [
60
- ["Description", "Quantité"],
61
- [description, description.split()[0] if description.split()[0].isdigit() else ""]
62
  ]
63
 
64
- table = Table(table_data, colWidths=[200, 60])
65
-
66
- table.setStyle(
67
- TableStyle([
68
- ("BACKGROUND", (0, 0), (-1, 0), colors.lightgrey),
69
- ("TEXTCOLOR", (0, 0), (-1, 0), colors.black),
70
- ("ALIGN", (1, 1), (-1, -1), "CENTER"),
71
- ("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
72
- ("BOX", (0, 0), (-1, -1), 1, colors.black),
73
- ("GRID", (0, 0), (-1, -1), 0.5, colors.grey),
74
- ("FONTSIZE", (0, 0), (-1, -1), 10),
75
- ])
76
- )
77
 
78
  elements.append(table)
79
- elements.append(Spacer(1, 24))
80
 
81
- # Mention finale
82
- elements.append(Paragraph("Merci pour votre commande.", normal))
 
 
 
 
83
 
84
- # Génération du PDF
85
  doc.build(elements)
 
1
  from reportlab.lib.pagesizes import A5
2
  from reportlab.lib import colors
 
3
  from reportlab.lib.styles import getSampleStyleSheet
4
+ from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
5
+ from reportlab.lib.units import mm
6
+
7
+ def create_invoice(text, file_name):
8
+ # Nettoyage du texte source
9
+ text = text.replace('*', '').replace('#', '')
10
+ text = text.replace('Reçu', '').strip()
11
+
12
+ # Extraction des champs
13
+ lines = [l.strip() for l in text.split("\n") if l.strip()]
14
+ data_dict = {}
15
+
16
+ for line in lines:
17
+ if ":" in line:
18
+ key, val = line.split(":", 1)
19
+ data_dict[key.strip()] = val.strip()
20
+
21
+ # Récupération des informations
22
+ nom = data_dict.get("Nom", "")
23
+ adresse = data_dict.get("Adresse", "")
24
+ commande = data_dict.get("Commande", "")
25
+ prix_piece = data_dict.get("Prix par pièce", "")
26
+ prix_total = data_dict.get("Prix total", "")
27
+ date_livraison = data_dict.get("Date de livraison", "")
28
+
29
+ # Structure du PDF
30
+ doc = SimpleDocTemplate(
31
+ file_name,
32
+ pagesize=A5,
33
+ rightMargin=20,
34
+ leftMargin=20,
35
+ topMargin=20,
36
+ bottomMargin=20
37
+ )
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  styles = getSampleStyleSheet()
 
 
 
 
 
 
 
40
  elements = []
41
 
42
  # Titre
43
+ titre = Paragraph("<b>FACTURE</b>", styles["Title"])
44
+ elements.append(titre)
45
+ elements.append(Spacer(1, 8 * mm))
46
 
47
  # Informations client
48
+ info_client = (
49
+ f"<b>Client :</b> {nom}<br/>"
 
50
  f"<b>Adresse :</b> {adresse}<br/>"
51
+ f"<b>Date de livraison :</b> {date_livraison}<br/>"
 
52
  )
53
+ elements.append(Paragraph(info_client, styles["Normal"]))
54
+ elements.append(Spacer(1, 8 * mm))
55
 
56
+ # Tableau du détail de commande
57
  table_data = [
58
+ ["Description", "Qté", "Prix unitaire", "Total"],
59
+ [commande, "", prix_piece, prix_total]
60
  ]
61
 
62
+ table = Table(table_data, colWidths=[60*mm, 15*mm, 30*mm, 30*mm])
63
+ table.setStyle(TableStyle([
64
+ ("BACKGROUND", (0,0), (-1,0), colors.lightgrey),
65
+ ("TEXTCOLOR", (0,0), (-1,0), colors.black),
66
+ ("ALIGN", (1,1), (-1,-1), "CENTER"),
67
+ ("FONTNAME", (0,0), (-1,0), "Helvetica-Bold"),
68
+ ("BOTTOMPADDING", (0,0), (-1,0), 8),
69
+ ("GRID", (0,0), (-1,-1), 0.5, colors.black),
70
+ ]))
 
 
 
 
71
 
72
  elements.append(table)
73
+ elements.append(Spacer(1, 12 * mm))
74
 
75
+ # Total général
76
+ total_paragraph = Paragraph(
77
+ f"<b>Total dû : {prix_total}</b>",
78
+ styles["Heading3"]
79
+ )
80
+ elements.append(total_paragraph)
81
 
82
+ # Génération PDF
83
  doc.build(elements)