don770omr commited on
Commit
9700fe0
·
verified ·
1 Parent(s): 3eb3165

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +112 -117
utils.py CHANGED
@@ -1,118 +1,113 @@
1
- from io import BytesIO
2
- from reportlab.lib.pagesizes import letter
3
- from reportlab.pdfgen import canvas
4
- from reportlab.platypus import Table, TableStyle
5
- from convertion import convert_feet_to_inches,convert_inches_to_feet
6
- from convertion import convert_meters_to_unit, convert_to_meters
7
- from io import BytesIO
8
- from reportlab.lib.pagesizes import letter
9
- from reportlab.lib import colors
10
- from reportlab.lib.styles import getSampleStyleSheet
11
- from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
12
- from reportlab.pdfgen import canvas
13
-
14
-
15
- # Common function for shutter and glass calculations
16
- def calculate_reduced_dimensions(width, height, unit, selection, object_item_value, item_type="Door"):
17
- # Use the selected unit for the width and height reduction
18
- if unit == 'Feet':
19
- reduction_width = convert_inches_to_feet(object_item_value[selection][item_type]['width'])
20
- reduction_height = convert_inches_to_feet(object_item_value[selection][item_type]['height'])
21
- else:
22
- reduction_width = object_item_value[selection][item_type]['width']
23
- reduction_height = object_item_value[selection][item_type]['height']
24
-
25
- # Calculate the reduced dimensions based on the width and height
26
- reduced_width = (width - reduction_width) / 2.0
27
- reduced_height = height - reduction_height
28
- return reduced_width, reduced_height
29
-
30
- # Functions for door and window area/frame calculation
31
- def calculate_area_and_frame(width, height, unit, item_type="Door"):
32
- # Convert width and height to meters for area and frame calculation
33
- width_in_meters = convert_to_meters(width, unit)
34
- height_in_meters = convert_to_meters(height, unit)
35
- area = width_in_meters * height_in_meters
36
- frame_thickness = {'Door': 0.1, 'Window': 0.05}
37
- frame_area = 2 * (width_in_meters + height_in_meters) * frame_thickness.get(item_type, 0.1)
38
-
39
- # Convert area to square feet if necessary
40
- if unit == "Feet":
41
- area = area * 10.7639 # Convert square meters to square feet
42
- frame_area = frame_area * 10.7639 # Convert frame area to square feet
43
- # If the unit is inches, convert to square feet
44
- elif unit == "Inches":
45
- area = (area * 0.092903) * 10.7639 # Convert inches squared to square feet
46
- frame_area = (frame_area * 0.092903) * 10.7639 # Convert frame area to square feet
47
-
48
- return area, frame_area
49
-
50
- def generate_pdf(dfs, company_name, address, unit, items):
51
- buffer = BytesIO()
52
-
53
- # Create a canvas for the PDF
54
- c = canvas.Canvas(buffer, pagesize=letter)
55
- width, height = letter
56
-
57
- # Company details
58
- c.setFont("Helvetica", 12)
59
- c.drawString(30, height - 40, f"Company Name: {company_name}")
60
- c.drawString(30, height - 60, f"Address: {address}")
61
-
62
- # Draw a line under the address
63
- c.setStrokeColor(colors.black)
64
- c.setLineWidth(1)
65
- c.line(30, height - 70, width - 30, height - 70) # Draw line under the address
66
-
67
-
68
- # Title
69
- c.setFont("Helvetica-Bold", 16)
70
- c.drawString(30, height - 100, f"Dimensions Calculation Results: for units ({unit})")
71
-
72
- # Calculate the y-position to avoid overlapping
73
- y_position = height - 150
74
-
75
- # Iterate over each item in the items list
76
- for i, item in enumerate(items):
77
- # Add a new page if the y-position gets too low
78
- if y_position < 100:
79
- c.showPage()
80
- y_position = height - 40 # Reset the y-position
81
-
82
- # Display item label (e.g., Item 1, Item 2)
83
- c.setFont("Helvetica-Bold", 12)
84
- c.drawString(30, y_position, f"Item {i+1}: {item}")
85
- y_position -= 130 # Move down after the label
86
-
87
- # Retrieve the corresponding dataframe for this item
88
- df = dfs[i]
89
-
90
- # Create and style the table
91
- data = [["Dimension", "Outer Frame", "Shutter", "Glass"]] # Table Header
92
- for index, row in df.iterrows():
93
- data.append([index] + [str(value) for value in row]) # Use row values directly for each column
94
-
95
- # Create the table
96
- table = Table(data, colWidths=[100, 100, 100, 100], rowHeights=30)
97
- table.setStyle(TableStyle([
98
- ('TEXTCOLOR', (0, 0), (-1, 0), (0, 0, 0)),
99
- ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
100
- ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
101
- ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
102
- ('BACKGROUND', (0, 0), (-1, 0), (0.7, 0.7, 0.7)),
103
- ('GRID', (0, 0), (-1, -1), 0.5, (0, 0, 0))
104
- ]))
105
-
106
- # Position the table just below the item label
107
- table.wrapOn(c, width, height)
108
- table.drawOn(c, 30, y_position)
109
-
110
- # Adjust the y-position for the next table
111
- y_position -= len(data) + 40 # Move down for next table
112
- c.line(30, y_position , width - 30, y_position) # Draw line under the address
113
-
114
- # Save the PDF
115
- c.save()
116
-
117
- buffer.seek(0)
118
  return buffer
 
1
+ from io import BytesIO
2
+ from reportlab.lib.pagesizes import letter
3
+ from reportlab.pdfgen import canvas
4
+ from reportlab.lib.styles import getSampleStyleSheet
5
+ from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
6
+ from convertion import convert_feet_to_inches,convert_inches_to_feet
7
+ from convertion import convert_meters_to_unit, convert_to_meters
8
+
9
+
10
+ # Common function for shutter and glass calculations
11
+ def calculate_reduced_dimensions(width, height, unit, selection, object_item_value, item_type="Door"):
12
+ # Use the selected unit for the width and height reduction
13
+ if unit == 'Feet':
14
+ reduction_width = convert_inches_to_feet(object_item_value[selection][item_type]['width'])
15
+ reduction_height = convert_inches_to_feet(object_item_value[selection][item_type]['height'])
16
+ else:
17
+ reduction_width = object_item_value[selection][item_type]['width']
18
+ reduction_height = object_item_value[selection][item_type]['height']
19
+
20
+ # Calculate the reduced dimensions based on the width and height
21
+ reduced_width = (width - reduction_width) / 2.0
22
+ reduced_height = height - reduction_height
23
+ return reduced_width, reduced_height
24
+
25
+ # Functions for door and window area/frame calculation
26
+ def calculate_area_and_frame(width, height, unit, item_type="Door"):
27
+ # Convert width and height to meters for area and frame calculation
28
+ width_in_meters = convert_to_meters(width, unit)
29
+ height_in_meters = convert_to_meters(height, unit)
30
+ area = width_in_meters * height_in_meters
31
+ frame_thickness = {'Door': 0.1, 'Window': 0.05}
32
+ frame_area = 2 * (width_in_meters + height_in_meters) * frame_thickness.get(item_type, 0.1)
33
+
34
+ # Convert area to square feet if necessary
35
+ if unit == "Feet":
36
+ area = area * 10.7639 # Convert square meters to square feet
37
+ frame_area = frame_area * 10.7639 # Convert frame area to square feet
38
+ # If the unit is inches, convert to square feet
39
+ elif unit == "Inches":
40
+ area = (area * 0.092903) * 10.7639 # Convert inches squared to square feet
41
+ frame_area = (frame_area * 0.092903) * 10.7639 # Convert frame area to square feet
42
+
43
+ return area, frame_area
44
+
45
+ def generate_pdf(dfs, company_name, address, unit, items):
46
+ buffer = BytesIO()
47
+
48
+ # Create a canvas for the PDF
49
+ c = canvas.Canvas(buffer, pagesize=letter)
50
+ width, height = letter
51
+
52
+ # Company details
53
+ c.setFont("Helvetica", 12)
54
+ c.drawString(30, height - 40, f"Company Name: {company_name}")
55
+ c.drawString(30, height - 60, f"Address: {address}")
56
+
57
+ # Draw a line under the address
58
+ c.setStrokeColor(colors.black)
59
+ c.setLineWidth(1)
60
+ c.line(30, height - 70, width - 30, height - 70) # Draw line under the address
61
+
62
+
63
+ # Title
64
+ c.setFont("Helvetica-Bold", 16)
65
+ c.drawString(30, height - 100, f"Dimensions Calculation Results: for units ({unit})")
66
+
67
+ # Calculate the y-position to avoid overlapping
68
+ y_position = height - 150
69
+
70
+ # Iterate over each item in the items list
71
+ for i, item in enumerate(items):
72
+ # Add a new page if the y-position gets too low
73
+ if y_position < 100:
74
+ c.showPage()
75
+ y_position = height - 40 # Reset the y-position
76
+
77
+ # Display item label (e.g., Item 1, Item 2)
78
+ c.setFont("Helvetica-Bold", 12)
79
+ c.drawString(30, y_position, f"Item {i+1}: {item}")
80
+ y_position -= 130 # Move down after the label
81
+
82
+ # Retrieve the corresponding dataframe for this item
83
+ df = dfs[i]
84
+
85
+ # Create and style the table
86
+ data = [["Dimension", "Outer Frame", "Shutter", "Glass"]] # Table Header
87
+ for index, row in df.iterrows():
88
+ data.append([index] + [str(value) for value in row]) # Use row values directly for each column
89
+
90
+ # Create the table
91
+ table = Table(data, colWidths=[100, 100, 100, 100], rowHeights=30)
92
+ table.setStyle(TableStyle([
93
+ ('TEXTCOLOR', (0, 0), (-1, 0), (0, 0, 0)),
94
+ ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
95
+ ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
96
+ ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
97
+ ('BACKGROUND', (0, 0), (-1, 0), (0.7, 0.7, 0.7)),
98
+ ('GRID', (0, 0), (-1, -1), 0.5, (0, 0, 0))
99
+ ]))
100
+
101
+ # Position the table just below the item label
102
+ table.wrapOn(c, width, height)
103
+ table.drawOn(c, 30, y_position)
104
+
105
+ # Adjust the y-position for the next table
106
+ y_position -= len(data) + 20 # Move down for next table
107
+ c.line(30, y_position , width - 30, y_position) # Draw line under the address
108
+
109
+ # Save the PDF
110
+ c.save()
111
+
112
+ buffer.seek(0)
 
 
 
 
 
113
  return buffer