SURESHBEEKHANI commited on
Commit
54b8e46
·
verified ·
1 Parent(s): 222be2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -56,18 +56,41 @@ def process_image(uploaded_file):
56
  return None, None
57
 
58
 
59
- def generate_pdf(report_text):
60
- """Generate a PDF report"""
61
  buffer = io.BytesIO()
62
  doc = SimpleDocTemplate(buffer, pagesize=letter)
63
  styles = getSampleStyleSheet()
64
- story = [Paragraph("<b>Nutrition Analysis Report</b>", styles['Title']), Spacer(1, 12),
65
- Paragraph(report_text.replace('\n', '<br/>'), styles['BodyText'])]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  doc.build(story)
67
  buffer.seek(0)
68
  return buffer
69
 
70
 
 
71
  def generate_analysis(uploaded_file, client):
72
  """Generate AI-powered food analysis"""
73
  base64_image, img_format = process_image(uploaded_file)
 
56
  return None, None
57
 
58
 
59
+ def generate_pdf(report_text, logo_b64):
60
+ """Generate a PDF report with logo"""
61
  buffer = io.BytesIO()
62
  doc = SimpleDocTemplate(buffer, pagesize=letter)
63
  styles = getSampleStyleSheet()
64
+
65
+ # Decode the base64 logo image
66
+ logo_data = base64.b64decode(logo_b64)
67
+ logo_image = Image.open(io.BytesIO(logo_data))
68
+
69
+ # Resize the logo to fit the page width (you can adjust size if necessary)
70
+ logo_width, logo_height = logo_image.size
71
+ logo_aspect = logo_height / logo_width
72
+ max_logo_width = 150 # Adjust this value to change the max width of the logo
73
+ logo_width = min(logo_width, max_logo_width)
74
+ logo_height = int(logo_width * logo_aspect)
75
+
76
+ # Create a ReportLab Image element to add the logo to the PDF
77
+ logo = ReportLabImage(io.BytesIO(logo_data), width=logo_width, height=logo_height)
78
+
79
+ # Build the PDF content
80
+ story = [
81
+ logo, # Add the logo at the top of the page
82
+ Spacer(1, 12), # Space after the logo
83
+ Paragraph("<b>Nutrition Analysis Report</b>", styles['Title']),
84
+ Spacer(1, 12),
85
+ Paragraph(report_text.replace('\n', '<br/>'), styles['BodyText'])
86
+ ]
87
+
88
  doc.build(story)
89
  buffer.seek(0)
90
  return buffer
91
 
92
 
93
+
94
  def generate_analysis(uploaded_file, client):
95
  """Generate AI-powered food analysis"""
96
  base64_image, img_format = process_image(uploaded_file)