-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_invoice.py
More file actions
285 lines (219 loc) · 7.35 KB
/
generate_invoice.py
File metadata and controls
285 lines (219 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from reportlab.pdfgen import canvas
from reportlab.platypus import (
SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle,
Image, PageBreak
)
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors
from reportlab.lib.units import mm
from reportlab.lib.pagesizes import A4
from reportlab.lib.enums import TA_RIGHT
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus import CondPageBreak
from datetime import date, timedelta
from pathlib import Path
import random
OUTPUT_FILE = "invoice_sample1.pdf"
# ------------------------
# Header and Footer
# ------------------------
def header_footer(canvas, doc):
canvas.saveState()
canvas.setFont("Helvetica", 9)
# Top header line
canvas.drawString(25*mm, 285*mm, "Atlantic Services Lda")
canvas.drawRightString(190*mm, 285*mm, "Invoice")
# separator line
canvas.line(25*mm, 280*mm, 190*mm, 280*mm)
# footer
canvas.setFont("Helvetica", 8)
canvas.drawString(25*mm, 12*mm, "Atlantic Services Lda - email[at]example.com")
canvas.restoreState()
# ------------------------
# Page numbering
# ------------------------
class PageNumCanvas(canvas.Canvas):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._saved_page_states = []
def showPage(self):
self._saved_page_states.append(dict(self.__dict__))
self._startPage()
def save(self):
"""Add page numbers to all pages."""
page_count = len(self._saved_page_states)
for state in self._saved_page_states:
self.__dict__.update(state)
self.draw_page_number(page_count)
super().showPage()
super().save()
def draw_page_number(self, total_pages):
page = self._pageNumber
text = f"Page {page} of {total_pages}"
self.setFont("Helvetica", 9)
self.drawRightString(200 * mm, 15 * mm, text)
# ------------------------
# Sample dynamic data
# ------------------------
def get_items():
"""Generate many rows to force multipage."""
items = []
services = [
"Monthly SaaS Subscription",
"Cloud Storage Usage",
"API Requests Package",
"Data Processing",
"Priority Support",
"Analytics Processing",
"System Integration",
"Custom Automation",
"Monitoring Service",
"Backup Retention"
]
for i in range(50): # enough to force page break
qty = random.randint(1, 5)
price = random.randint(20, 120)
items.append((random.choice(services), qty, price, qty * price))
return items
# ------------------------
# Document building
# ------------------------
def build_invoice():
OUTPUT_DIR = Path("output")
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
file_path = OUTPUT_DIR / OUTPUT_FILE
doc = SimpleDocTemplate(
str(file_path),
pagesize=A4,
rightMargin=25*mm,
leftMargin=25*mm,
topMargin=15*mm,
bottomMargin=20*mm
)
doc.title = "Invoice Sample"
styles = getSampleStyleSheet()
normal = styles["Normal"]
bold = styles["Heading3"]
right = ParagraphStyle(
name="right",
parent=normal,
alignment=TA_RIGHT
)
rightBold = ParagraphStyle(
name="right",
parent=bold,
alignment=TA_RIGHT
)
story = []
# ------------------------
# Logo - Company
# ------------------------
logo = Image("logo.png", width=60*mm, height=40*mm)
story.insert(0, logo)
story.insert(1, Spacer(1, 12))
# ------------------------
# Header - Company
# ------------------------
story.append(Paragraph("<b>Atlantic Services Lda</b>", bold))
story.append(Paragraph("Rua Exemplo 123", normal))
story.append(Paragraph("1200-001 Lisboa, Portugal", normal))
story.append(Paragraph("Email: email[at]example.com", normal))
story.append(Paragraph("NIF: 509999999", normal))
story.append(Spacer(1, 10))
# ------------------------
# Client block
# ------------------------
story.append(Paragraph("<b>Bill To:</b>", rightBold))
story.append(Paragraph("TechSolutions GmbH", right))
story.append(Paragraph("Alexanderplatz 3", right))
story.append(Paragraph("10178 Berlin, Germany", right))
story.append(Paragraph("VAT: DE123456789", right))
story.append(Spacer(1, 25))
# ------------------------
# Invoice info
# ------------------------
invoice_number = f"INV-{date.today().year}-{random.randint(1000,9999)}"
due_date = date.today() + timedelta(days=14)
info_data = [
["Invoice Number:", invoice_number],
["Invoice Date:", str(date.today())],
["Due Date:", str(due_date)],
]
info_table = Table(info_data, colWidths=[120, 200])
info_table.setStyle(TableStyle([
("ALIGN", (1, 0), (1, -1), "RIGHT"),
("BOTTOMPADDING", (0, 0), (-1, -1), 6),
]))
story.append(info_table)
story.append(Spacer(1, 20))
# ------------------------
# Items table
# ------------------------
items = get_items()
table_data = [["Description", "Qty", "Unit Price (€)", "Total (€)"]]
subtotal = 0
for desc, qty, price, total in items:
table_data.append([
Paragraph(desc, normal),
qty,
f"{price:.2f}",
f"{total:.2f}"]
)
subtotal += total
items_table = Table(
table_data,
repeatRows=1,
colWidths=[90*mm, 20*mm, 30*mm, 30*mm]
)
items_table.setStyle(TableStyle([
("GRID", (0, 0), (-1, -1), 0.25, colors.grey),
("BACKGROUND", (0, 0), (-1, 0), colors.HexColor("#EAEAEA")),
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
("ALIGN", (1, 1), (-1, -1), "RIGHT"),
("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
("BOTTOMPADDING", (0, 0), (-1, 0), 8),
]))
story.append(items_table)
story.append(Spacer(1, 20))
# ------------------------
# Totals
# ------------------------
vat = subtotal * 0.23
total = subtotal + vat
totals_data = [
["Subtotal:", f"{subtotal:.2f} €"],
["VAT (23%):", f"{vat:.2f} €"],
["Total:", f"{total:.2f} €"],
]
totals_table = Table(totals_data, colWidths=[350, 100])
totals_table.setStyle(TableStyle([
("ALIGN", (1, 0), (1, -1), "RIGHT"),
("FONTNAME", (0, -1), (-1, -1), "Helvetica-Bold"),
("TOPPADDING", (0, 0), (-1, -1), 6),
]))
story.append(totals_table)
story.append(Spacer(1, 24))
story.append(CondPageBreak(80*mm)) # Force page break to show multipage handling
# ------------------------
# Payment info
# ------------------------
story.append(Paragraph("<b>Payment Details</b>", bold))
story.append(Paragraph("IBAN: PT50 0000 0000 0000 0000 0000 0", normal))
story.append(Paragraph("BIC/SWIFT: BCOMPTPL", normal))
story.append(Spacer(1, 24))
# ------------------------
# Footer text
# ------------------------
story.append(Paragraph(
"This invoice was generated electronically and is valid without signature.",
right
))
# Build
doc.build(story,
onFirstPage=header_footer,
onLaterPages=header_footer,
canvasmaker=PageNumCanvas
)
if __name__ == "__main__":
build_invoice()
print("Invoice generated:", OUTPUT_FILE)