teoaivalis commited on
Commit
f670341
·
verified ·
1 Parent(s): 12ec423

Update modules/pdf_report.py

Browse files
Files changed (1) hide show
  1. modules/pdf_report.py +52 -55
modules/pdf_report.py CHANGED
@@ -3,22 +3,19 @@ import requests
3
  from PIL import Image
4
  from io import BytesIO
5
  import datetime
 
6
 
7
  def create_pdf_report(prompt, user_image, top_results):
8
  pdf = FPDF()
9
  pdf.set_auto_page_break(auto=True, margin=15)
10
 
11
- # ---------------------------------
12
- # Page 1: Cover + Prompt + Gen Image
13
- # ---------------------------------
14
  pdf.add_page()
15
-
16
- # Title & Logo
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)
23
  pdf.ln(10)
24
  description = (
@@ -27,7 +24,7 @@ def create_pdf_report(prompt, user_image, top_results):
27
  )
28
  pdf.multi_cell(0, 10, description)
29
 
30
- # Prompt (box)
31
  pdf.ln(8)
32
  pdf.set_font("Arial", 'B', 14)
33
  pdf.cell(0, 10, "Prompt:", ln=True)
@@ -35,75 +32,75 @@ 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 image — make it centered & proper size
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 # max allowed width in mm
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
- # ---------------------------------
56
  # Page 2: Top Similar Images
57
- # ---------------------------------
58
  pdf.add_page()
59
  pdf.set_font("Arial", 'B', 16)
60
  pdf.cell(0, 10, "Top Similar Images:", ln=True)
61
  pdf.ln(5)
62
 
63
- # Grid settings
64
- col_width = 90 # 2 columns
65
- row_height = 60
66
- margin = 10
67
- spacing = 10
68
 
69
- x_start = margin
70
  y = pdf.get_y()
71
  col = 0
72
 
73
  for i, (url, sim) in enumerate(top_results):
74
  try:
75
  response = requests.get(url, timeout=5)
76
- if response.status_code == 200:
77
- img = Image.open(BytesIO(response.content)).convert("RGB")
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 = img_h * scale
86
-
87
- x = x_start + col * (col_width + spacing)
88
- pdf.image(temp_filename, x=x, y=y, w=scaled_w, h=scaled_h)
89
-
90
- # Caption
91
- pdf.set_xy(x, y + scaled_h + 2)
92
- pdf.set_font("Arial", '', 12)
93
- pdf.cell(col_width, 10, f"Similarity: {sim:.3f}", ln=0, align="C")
94
-
95
- col += 1
96
- if col >= 2:
97
- col = 0
98
- y += scaled_h + 20 # add some spacing between rows
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")
 
 
 
 
 
109
 
 
 
 
3
  from PIL import Image
4
  from io import BytesIO
5
  import datetime
6
+ import os
7
 
8
  def create_pdf_report(prompt, user_image, top_results):
9
  pdf = FPDF()
10
  pdf.set_auto_page_break(auto=True, margin=15)
11
 
12
+ # -------------
13
+ # Page 1: Cover
14
+ # -------------
15
  pdf.add_page()
 
 
16
  pdf.set_font("Arial", 'B', 24)
17
  pdf.cell(0, 15, "Search-Based Influence Analyzer", ln=True, align="L")
 
18
 
 
19
  pdf.set_font("Arial", '', 12)
20
  pdf.ln(10)
21
  description = (
 
24
  )
25
  pdf.multi_cell(0, 10, description)
26
 
27
+ # Prompt section
28
  pdf.ln(8)
29
  pdf.set_font("Arial", 'B', 14)
30
  pdf.cell(0, 10, "Prompt:", ln=True)
 
32
  pdf.set_font("Arial", '', 12)
33
  pdf.multi_cell(0, 10, prompt, fill=True)
34
 
35
+ # Generated image
36
  pdf.ln(8)
37
  pdf.set_font("Arial", 'B', 14)
38
  pdf.cell(0, 10, "Generated Image:", ln=True)
39
 
40
+ temp_user_img = "temp_input.jpg"
41
+ user_image.convert("RGB").save(temp_user_img)
42
+ max_img_width = 160
43
  img_w, img_h = user_image.size
44
+ scale = max_img_width / img_w
 
45
  scaled_w = img_w * scale
46
  scaled_h = img_h * scale
47
+ x_center = (210 - scaled_w) / 2
48
+ pdf.image(temp_user_img, x=x_center, w=scaled_w)
49
+ os.remove(temp_user_img)
50
 
51
+ # --------------------------
 
 
 
 
52
  # Page 2: Top Similar Images
53
+ # --------------------------
54
  pdf.add_page()
55
  pdf.set_font("Arial", 'B', 16)
56
  pdf.cell(0, 10, "Top Similar Images:", ln=True)
57
  pdf.ln(5)
58
 
59
+ img_box_w = 90
60
+ img_box_h = 70
61
+ margin_x = 10
62
+ spacing_x = 10
63
+ spacing_y = 10
64
 
65
+ x = margin_x
66
  y = pdf.get_y()
67
  col = 0
68
 
69
  for i, (url, sim) in enumerate(top_results):
70
  try:
71
  response = requests.get(url, timeout=5)
72
+ if response.status_code != 200:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  continue
74
+
75
+ img = Image.open(BytesIO(response.content)).convert("RGB")
76
+ img = img.resize((int(img_box_w * 3.78), int(img_box_h * 3.78))) # Resize in pixels (approx. dpi=96)
77
+ temp_img = f"temp_sim_{i}.jpg"
78
+ img.save(temp_img)
79
+
80
+ pdf.image(temp_img, x=x, y=y, w=img_box_w, h=img_box_h)
81
+ pdf.set_xy(x, y + img_box_h + 2)
82
+ pdf.set_font("Arial", '', 12)
83
+ pdf.cell(img_box_w, 8, f"Similarity: {sim:.3f}", ln=0, align="C")
84
+ os.remove(temp_img)
85
+
86
+ col += 1
87
+ if col == 2:
88
+ col = 0
89
+ x = margin_x
90
+ y += img_box_h + 15 # move down for new row
91
+ pdf.set_y(y)
92
+ else:
93
+ x += img_box_w + spacing_x
94
+
95
+ except Exception:
96
  continue
97
 
98
+ # -----------------
99
+ # Footer
100
+ # -----------------
101
+ pdf.set_y(-20)
102
+ pdf.set_font("Arial", 'I', 9)
103
+ pdf.cell(0, 10, f"Generated by Influence Analyzer • {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}", 0, 0, "C")
104
 
105
+ # Return PDF bytes
106
+ return pdf.output(dest="S").encode("latin1")