alpha / utils.py
don770omr's picture
Update utils.py
f04d03a verified
from io import BytesIO
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from convertion import convert_feet_to_inches,convert_inches_to_feet
from convertion import convert_meters_to_unit, convert_to_meters
# Common function for shutter and glass calculations
def calculate_reduced_dimensions(width, height, unit, selection, object_item_value, item_type="Door"):
# Use the selected unit for the width and height reduction
if unit == 'Feet':
reduction_width = convert_inches_to_feet(object_item_value[selection][item_type]['width'])
reduction_height = convert_inches_to_feet(object_item_value[selection][item_type]['height'])
else:
reduction_width = object_item_value[selection][item_type]['width']
reduction_height = object_item_value[selection][item_type]['height']
selection_div = "divider_sh" if selection == 'shutter' else 'divider_gl'
divider_h = object_item_value[selection_div][item_type]['height']
divider_w = object_item_value[selection_div][item_type]['width']
# Calculate the reduced dimensions based on the width and height
reduced_width = (width - reduction_width) / divider_w
reduced_height = (height - reduction_height) / divider_h
area,_ = calculate_area_and_frame(reduced_width,reduced_height,unit,item_type=item_type)
return reduced_width, reduced_height, area
# Functions for door and window area/frame calculation
def calculate_area_and_frame(width, height, unit, item_type="Door"):
# Convert width and height to meters for area and frame calculation
if unit == 'Inches':
width = convert_inches_to_feet(width)
height = convert_inches_to_feet(height)
area = width * height
frame_thickness = {'Door': 0.1, 'Window': 0.05}
frame_area = 2 * (width + height) * frame_thickness.get(item_type, 0.1)
return area, frame_area
def generate_pdf(df_combined, company_name, address, unit):
buffer = BytesIO()
# Create a canvas for the PDF
c = canvas.Canvas(buffer, pagesize=letter)
width, height = letter
y_position = height - 40
# Company details
c.setFont("Helvetica", 12)
c.drawString(30, y_position, f"Company Name: {company_name}")
y_position -= 20
c.drawString(30, y_position, f"Address: {address}")
# Draw a line under the address
y_position -= 10
c.setStrokeColor(colors.black)
c.setLineWidth(1)
c.line(30, y_position, width - 30, y_position) # Draw line under the address
# Title
y_position -= 30
c.setFont("Helvetica-Bold", 16)
c.drawString(30, y_position, f"Dimensions Calculation Results: for units ({unit})")
# Calculate the y-position to avoid overlapping
y_position -= 20
# Create and style the table
data = [["Sl no", "Type", "Outer Frame", "Shutter", "Glass", "Area"]] # Table Header
for index, row in df_combined.iterrows():
data.append([row['Sl no'], row['Type'], row['Outer Frame'], row['Shutter'], row['Glass'], row['Area']])
# Create the table
row_height = 30
table = Table(data, colWidths=[50, 100, 100, 100, 100, 100], rowHeights=row_height)
table.setStyle(TableStyle([
('TEXTCOLOR', (0, 0), (-1, 0), (0, 0, 0)),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 0), (-1, 0), (0.7, 0.7, 0.7)),
('GRID', (0, 0), (-1, -1), 0.5, (0, 0, 0))
]))
# Position the table just below the title
table.wrapOn(c, width, height)
table.drawOn(c, 30, y_position-(len(df_combined)+1)*row_height)
# Save the PDF
c.save()
buffer.seek(0)
return buffer