Spaces:
Sleeping
Sleeping
Update modules/pdf_report.py
Browse files- modules/pdf_report.py +14 -18
modules/pdf_report.py
CHANGED
|
@@ -13,10 +13,9 @@ def create_pdf_report(prompt, user_image, top_results):
|
|
| 13 |
# ---------------------------------
|
| 14 |
pdf.add_page()
|
| 15 |
|
| 16 |
-
# Title
|
| 17 |
pdf.set_font("Arial", 'B', 24)
|
| 18 |
pdf.cell(0, 15, "Search-Based Influence Analyzer", ln=True, align="L")
|
| 19 |
-
#pdf.image("assets/logo.png", x=160, y=10, w=30)
|
| 20 |
|
| 21 |
# Description
|
| 22 |
pdf.set_font("Arial", '', 12)
|
|
@@ -27,7 +26,7 @@ def create_pdf_report(prompt, user_image, top_results):
|
|
| 27 |
)
|
| 28 |
pdf.multi_cell(0, 10, description)
|
| 29 |
|
| 30 |
-
# Prompt
|
| 31 |
pdf.ln(8)
|
| 32 |
pdf.set_font("Arial", 'B', 14)
|
| 33 |
pdf.cell(0, 10, "Prompt:", ln=True)
|
|
@@ -35,21 +34,18 @@ def create_pdf_report(prompt, user_image, top_results):
|
|
| 35 |
pdf.set_font("Arial", '', 12)
|
| 36 |
pdf.multi_cell(0, 10, prompt, fill=True)
|
| 37 |
|
| 38 |
-
# Generated
|
| 39 |
pdf.ln(8)
|
| 40 |
pdf.set_font("Arial", 'B', 14)
|
| 41 |
pdf.cell(0, 10, "Generated Image:", ln=True)
|
| 42 |
|
| 43 |
-
# Save temp & get size to scale nicely to page width minus margins
|
| 44 |
user_image.save("temp_input.jpg")
|
| 45 |
img_w, img_h = user_image.size
|
| 46 |
-
max_w = 160
|
| 47 |
scale = max_w / img_w
|
| 48 |
scaled_w = img_w * scale
|
| 49 |
scaled_h = img_h * scale
|
| 50 |
-
|
| 51 |
-
# Center the image horizontally
|
| 52 |
-
x_center = (210 - scaled_w) / 2 # A4 page width = 210mm
|
| 53 |
pdf.image("temp_input.jpg", x=x_center, y=None, w=scaled_w)
|
| 54 |
|
| 55 |
# ---------------------------------
|
|
@@ -61,7 +57,7 @@ def create_pdf_report(prompt, user_image, top_results):
|
|
| 61 |
pdf.ln(5)
|
| 62 |
|
| 63 |
# Grid settings
|
| 64 |
-
col_width = 90
|
| 65 |
row_height = 60
|
| 66 |
margin = 10
|
| 67 |
spacing = 10
|
|
@@ -78,11 +74,8 @@ def create_pdf_report(prompt, user_image, top_results):
|
|
| 78 |
temp_filename = f"temp_sim_{i}.jpg"
|
| 79 |
img.save(temp_filename)
|
| 80 |
|
| 81 |
-
# auto-scale to column width keeping aspect ratio
|
| 82 |
-
img_w, img_h = img.size
|
| 83 |
-
scale = col_width / img_w
|
| 84 |
scaled_w = col_width
|
| 85 |
-
scaled_h =
|
| 86 |
|
| 87 |
x = x_start + col * (col_width + spacing)
|
| 88 |
pdf.image(temp_filename, x=x, y=y, w=scaled_w, h=scaled_h)
|
|
@@ -95,14 +88,17 @@ def create_pdf_report(prompt, user_image, top_results):
|
|
| 95 |
col += 1
|
| 96 |
if col >= 2:
|
| 97 |
col = 0
|
| 98 |
-
y += scaled_h + 20
|
| 99 |
pdf.set_y(y)
|
| 100 |
pdf.set_x(x_start)
|
| 101 |
-
else:
|
| 102 |
-
print(f"⚠️ Bad HTTP status for {url}")
|
| 103 |
-
continue
|
| 104 |
except Exception as e:
|
| 105 |
print(f"⚠️ Error loading image {url}: {e}")
|
| 106 |
continue
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
return pdf.output(dest="S").encode("latin1")
|
|
|
|
| 13 |
# ---------------------------------
|
| 14 |
pdf.add_page()
|
| 15 |
|
| 16 |
+
# Title
|
| 17 |
pdf.set_font("Arial", 'B', 24)
|
| 18 |
pdf.cell(0, 15, "Search-Based Influence Analyzer", ln=True, align="L")
|
|
|
|
| 19 |
|
| 20 |
# Description
|
| 21 |
pdf.set_font("Arial", '', 12)
|
|
|
|
| 26 |
)
|
| 27 |
pdf.multi_cell(0, 10, description)
|
| 28 |
|
| 29 |
+
# Prompt box
|
| 30 |
pdf.ln(8)
|
| 31 |
pdf.set_font("Arial", 'B', 14)
|
| 32 |
pdf.cell(0, 10, "Prompt:", ln=True)
|
|
|
|
| 34 |
pdf.set_font("Arial", '', 12)
|
| 35 |
pdf.multi_cell(0, 10, prompt, fill=True)
|
| 36 |
|
| 37 |
+
# Generated Image
|
| 38 |
pdf.ln(8)
|
| 39 |
pdf.set_font("Arial", 'B', 14)
|
| 40 |
pdf.cell(0, 10, "Generated Image:", ln=True)
|
| 41 |
|
|
|
|
| 42 |
user_image.save("temp_input.jpg")
|
| 43 |
img_w, img_h = user_image.size
|
| 44 |
+
max_w = 160
|
| 45 |
scale = max_w / img_w
|
| 46 |
scaled_w = img_w * scale
|
| 47 |
scaled_h = img_h * scale
|
| 48 |
+
x_center = (210 - scaled_w) / 2
|
|
|
|
|
|
|
| 49 |
pdf.image("temp_input.jpg", x=x_center, y=None, w=scaled_w)
|
| 50 |
|
| 51 |
# ---------------------------------
|
|
|
|
| 57 |
pdf.ln(5)
|
| 58 |
|
| 59 |
# Grid settings
|
| 60 |
+
col_width = 90
|
| 61 |
row_height = 60
|
| 62 |
margin = 10
|
| 63 |
spacing = 10
|
|
|
|
| 74 |
temp_filename = f"temp_sim_{i}.jpg"
|
| 75 |
img.save(temp_filename)
|
| 76 |
|
|
|
|
|
|
|
|
|
|
| 77 |
scaled_w = col_width
|
| 78 |
+
scaled_h = row_height
|
| 79 |
|
| 80 |
x = x_start + col * (col_width + spacing)
|
| 81 |
pdf.image(temp_filename, x=x, y=y, w=scaled_w, h=scaled_h)
|
|
|
|
| 88 |
col += 1
|
| 89 |
if col >= 2:
|
| 90 |
col = 0
|
| 91 |
+
y += scaled_h + 20
|
| 92 |
pdf.set_y(y)
|
| 93 |
pdf.set_x(x_start)
|
|
|
|
|
|
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
print(f"⚠️ Error loading image {url}: {e}")
|
| 96 |
continue
|
| 97 |
|
| 98 |
+
# Footer with date
|
| 99 |
+
pdf.set_y(-15)
|
| 100 |
+
pdf.set_font("Arial", 'I', 9)
|
| 101 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 102 |
+
pdf.cell(0, 10, f"Report generated on {timestamp}", 0, 0, "C")
|
| 103 |
+
|
| 104 |
return pdf.output(dest="S").encode("latin1")
|