arasuezofis commited on
Commit
09ec541
·
verified ·
1 Parent(s): fd70e2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -24
app.py CHANGED
@@ -5,7 +5,7 @@ from reportlab.pdfgen import canvas
5
  from reportlab.lib import colors
6
  from io import BytesIO
7
 
8
- # Streamlit page config (IMPORTANT for HF)
9
  st.set_page_config(
10
  page_title="JSON to PDF Converter",
11
  layout="centered"
@@ -13,7 +13,6 @@ st.set_page_config(
13
 
14
  PAGE_WIDTH, PAGE_HEIGHT = letter
15
 
16
-
17
  def generate_pdf(data):
18
  pdf_output = BytesIO()
19
  c = canvas.Canvas(pdf_output, pagesize=letter)
@@ -29,7 +28,6 @@ def generate_pdf(data):
29
  continue
30
 
31
  page_created = True
32
-
33
  x1, y1, x2, y2 = bbox
34
 
35
  pdf_x = x1
@@ -38,15 +36,9 @@ def generate_pdf(data):
38
  box_height = y2 - y1
39
  font_size = max(6, min(18, box_height * 0.8))
40
 
41
- # Draw bounding box (debug)
42
  c.setStrokeColor(colors.lightgrey)
43
- c.rect(
44
- x1,
45
- PAGE_HEIGHT - y2,
46
- x2 - x1,
47
- box_height,
48
- fill=0
49
- )
50
 
51
  if "<b>" in text:
52
  c.setFont("Helvetica-Bold", font_size)
@@ -55,11 +47,7 @@ def generate_pdf(data):
55
  c.setFont("Helvetica", font_size)
56
 
57
  c.setFillColor(colors.black)
58
- c.drawString(
59
- pdf_x,
60
- pdf_y + (box_height - font_size),
61
- text
62
- )
63
 
64
  if page_created:
65
  c.showPage()
@@ -74,18 +62,13 @@ def generate_pdf(data):
74
  pdf_output.seek(0)
75
  return pdf_output
76
 
77
-
78
  def main():
79
  st.title("JSON to PDF Converter")
80
-
81
  st.write(
82
  "Upload a `results.json` file and download the generated PDF."
83
  )
84
 
85
- uploaded_file = st.file_uploader(
86
- "Choose a JSON file",
87
- type=["json"]
88
- )
89
 
90
  if uploaded_file is not None:
91
  try:
@@ -100,10 +83,8 @@ def main():
100
  file_name="output_fixed.pdf",
101
  mime="application/pdf"
102
  )
103
-
104
  except Exception as e:
105
  st.error(f"Error processing file: {e}")
106
 
107
-
108
  if __name__ == "__main__":
109
  main()
 
5
  from reportlab.lib import colors
6
  from io import BytesIO
7
 
8
+ # Streamlit page config
9
  st.set_page_config(
10
  page_title="JSON to PDF Converter",
11
  layout="centered"
 
13
 
14
  PAGE_WIDTH, PAGE_HEIGHT = letter
15
 
 
16
  def generate_pdf(data):
17
  pdf_output = BytesIO()
18
  c = canvas.Canvas(pdf_output, pagesize=letter)
 
28
  continue
29
 
30
  page_created = True
 
31
  x1, y1, x2, y2 = bbox
32
 
33
  pdf_x = x1
 
36
  box_height = y2 - y1
37
  font_size = max(6, min(18, box_height * 0.8))
38
 
39
+ # Draw bounding box for debugging (optional)
40
  c.setStrokeColor(colors.lightgrey)
41
+ c.rect(x1, PAGE_HEIGHT - y2, x2 - x1, box_height, fill=0)
 
 
 
 
 
 
42
 
43
  if "<b>" in text:
44
  c.setFont("Helvetica-Bold", font_size)
 
47
  c.setFont("Helvetica", font_size)
48
 
49
  c.setFillColor(colors.black)
50
+ c.drawString(pdf_x, pdf_y + (box_height - font_size), text)
 
 
 
 
51
 
52
  if page_created:
53
  c.showPage()
 
62
  pdf_output.seek(0)
63
  return pdf_output
64
 
 
65
  def main():
66
  st.title("JSON to PDF Converter")
 
67
  st.write(
68
  "Upload a `results.json` file and download the generated PDF."
69
  )
70
 
71
+ uploaded_file = st.file_uploader("Choose a JSON file", type=["json"])
 
 
 
72
 
73
  if uploaded_file is not None:
74
  try:
 
83
  file_name="output_fixed.pdf",
84
  mime="application/pdf"
85
  )
 
86
  except Exception as e:
87
  st.error(f"Error processing file: {e}")
88
 
 
89
  if __name__ == "__main__":
90
  main()