-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.py
More file actions
335 lines (228 loc) · 7.87 KB
/
Copy pathcomponents.py
File metadata and controls
335 lines (228 loc) · 7.87 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from styles import (FIELD_BORDER, FIELD_BACKGROUND, FIELD_TEXT, FIELD_HEIGHT,
LABEL_FONT_SIZE, FIELD_FONT_SIZE, TABLE_HEADER_BACKGROUND, TABLE_HEADER_TEXT, TABLE_ROW_ODD, TABLE_ROW_EVEN,)
class PDFForm:
def __init__(self, output_path):
self.canvas = canvas.Canvas(output_path, pagesize=letter)
self.page_width, self.page_height = letter
self.margin = 40
self.content_width = self.page_width - (self.margin * 2)
# Layout
self.column_gap = 25
self.column_width = (self.content_width - self.column_gap) / 2
self.label_width = 70
self.field_height = 18
self.y = self.page_height - self.margin
def title(self, text):
c = self.canvas
c.setFillColor(colors.black)
c.setFont("Helvetica-Bold", 22)
title_y = self.y - 35
c.drawCentredString(
self.page_width / 2,
title_y,
text
)
line_y = title_y - 8
c.setLineWidth(1.5)
c.line(
self.margin,
line_y,
self.page_width - self.margin,
line_y
)
self.y = line_y - 25
def space(self, amount=15):
"""
Moves the cursor down by the specified amount.
"""
self.y -= amount
def label(self, text, x, y):
self.canvas.setFont("Helvetica-Bold", LABEL_FONT_SIZE)
self.canvas.drawString(x, y, text)
def textbox(self, name, x, y, width=150, height=None, border_color=None,multiline=False):
flags = "multiline" if multiline else ""
if height is None:
height = FIELD_HEIGHT
if border_color is None:
border_color = FIELD_BORDER
self.canvas.acroForm.textfield(
name=name,
x=x,
y=y,
width=width,
height=height,
fontName="Helvetica",
fontSize=FIELD_FONT_SIZE,
fieldFlags=flags,
borderStyle="underlined",
borderColor=border_color,
fillColor=FIELD_BACKGROUND,
textColor=FIELD_TEXT,
forceBorder=True
)
def signature_line(self, x, y, width):
self.canvas.setStrokeColor(FIELD_BORDER)
self.canvas.setLineWidth(1)
self.canvas.line(
x,
y - 2,
x + width,
y - 2
)
def row(self, *fields):
if len(fields) == 0:
return
normalized_fields = []
for field_info in fields:
border_color = None
if field_info is None:
normalized_fields.append((None, None, 1, None))
continue
if len(field_info) == 2:
label, field_name = field_info
weight = 1
border_color = None
elif len(field_info) == 3:
label, field_name, weight = field_info
border_color = None
else:
label, field_name, weight, border_color = field_info
normalized_fields.append((label, field_name, weight, border_color))
total_weight = sum(weight for _, _, weight, _ in normalized_fields)
total_gap = self.column_gap * (len(normalized_fields) - 1)
available_width = self.content_width - total_gap
current_x = self.margin
for label, field_name, weight, border_color in normalized_fields:
column_width = available_width * (weight / total_weight)
if label is not None:
self.label(label, current_x, self.y)
self.textbox(
field_name,
current_x + self.label_width,
self.y - 5,
width=column_width - self.label_width,
border_color=border_color
)
current_x += column_width + self.column_gap
self.space(20)
def section(self, title):
self.space(5)
self.canvas.setFont("Helvetica-Bold", 11)
self.canvas.drawCentredString(
self.page_width / 2,
self.y,
f"** {title.upper()} **"
)
self.space(10)
def parts_table(self, rows=30):
headers = ["QTY", "DESCRIPTION", "PART #", "FINISH", "NOTES"]
column_weights = [0.4, 2.7, 1.8, 0.8, 2.5]
row_height = 18
header_height = 18
table_width = self.content_width
total_weight = sum(column_weights)
column_widths = [
table_width * (weight / total_weight)
for weight in column_weights
]
x = self.margin
y = self.y
self.canvas.setLineWidth(0.5)
# Header row
self.canvas.setFont("Helvetica-Bold", 8)
current_x = x
for header, col_width in zip(headers, column_widths):
self.canvas.setFillColor(TABLE_HEADER_BACKGROUND)
self.canvas.rect(
current_x,
y - header_height,
col_width,
header_height,
fill=1
)
self.canvas.setFillColor(TABLE_HEADER_TEXT)
self.canvas.drawCentredString(
current_x + (col_width / 2),
y - 13,
header
)
current_x += col_width
# Body rows
y -= header_height
for row in range(rows):
current_x = x
# Alternate row colors
if row % 2 == 0:
self.canvas.setFillColor(TABLE_ROW_ODD)
else:
self.canvas.setFillColor(TABLE_ROW_EVEN)
for col_index, col_width in enumerate(column_widths):
self.canvas.rect(
current_x,
y - row_height,
col_width,
row_height,
fill=1
)
field_name = f"table_r{row + 1}_c{col_index + 1}"
self.canvas.acroForm.textfield(
name=field_name,
x=current_x + 1,
y=y - row_height + 1,
width=col_width - 2,
height=row_height - 2,
borderWidth=0,
fillColor=colors.transparent,
textColor=colors.black,
fontName="Helvetica",
fieldFlags="multiline",
fontSize=0,
forceBorder=False
)
current_x += col_width
y -= row_height
self.y = y - 10
def pulled_by_section(self):
self.space(15)
# Title
self.canvas.setFillColor(colors.black)
self.canvas.setFont("Helvetica-Bold", 10)
self.canvas.drawString(
self.margin + 120,
self.y,
"Pulled By:"
)
# ----- Name -----
name_x = self.margin + 185
self.label("Name:", name_x, self.y)
self.textbox(
"pulled_by_name",
name_x + 35,
self.y - 5,
width=180
)
# ----- Date -----
date_x = self.page_width - 145
self.label("Date:", date_x, self.y)
self.textbox(
"pulled_by_date",
date_x + 35,
self.y - 5,
width=70
)
self.space(25)
# ----- Signature -----
self.label("Signature:", self.margin + 120, self.y)
signature_x = self.margin + 120
signature_width = self.page_width - self.margin - signature_x
self.signature_line(
signature_x,
self.y - 5,
signature_width
)
self.space(20)
def save(self):
self.canvas.save()