Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import io
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from groq import Groq
|
| 8 |
from reportlab.lib.pagesizes import letter
|
| 9 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 10 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 11 |
|
| 12 |
# ======================
|
|
@@ -57,22 +57,29 @@ def process_image(uploaded_file):
|
|
| 57 |
|
| 58 |
|
| 59 |
def generate_pdf(report_text, logo_b64):
|
| 60 |
-
"""Generate a PDF report"""
|
| 61 |
buffer = io.BytesIO()
|
| 62 |
doc = SimpleDocTemplate(buffer, pagesize=letter)
|
| 63 |
styles = getSampleStyleSheet()
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
logo_width, logo_height = logo_image.size
|
| 68 |
logo_aspect = logo_height / logo_width
|
| 69 |
max_logo_width = 150 # Adjust as needed
|
| 70 |
logo_width = min(logo_width, max_logo_width)
|
| 71 |
logo_height = int(logo_width * logo_aspect)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
story = [
|
| 74 |
-
|
| 75 |
-
Spacer(1, 12),
|
| 76 |
Paragraph("<b>Nutrition Analysis Report</b>", styles['Title']),
|
| 77 |
Spacer(1, 12),
|
| 78 |
Paragraph(report_text.replace('\n', '<br/>'), styles['BodyText'])
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from groq import Groq
|
| 8 |
from reportlab.lib.pagesizes import letter
|
| 9 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as ReportLabImage
|
| 10 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 11 |
|
| 12 |
# ======================
|
|
|
|
| 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 as needed
|
| 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'])
|