Update app.py
Browse files
app.py
CHANGED
|
@@ -4,22 +4,23 @@ from reportlab.lib.pagesizes import letter
|
|
| 4 |
from reportlab.pdfgen import canvas
|
| 5 |
from reportlab.lib import colors
|
| 6 |
import tempfile
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
PAGE_WIDTH, PAGE_HEIGHT = letter
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
def generate_pdf(file):
|
| 12 |
try:
|
| 13 |
-
# Read uploaded JSON file
|
| 14 |
with open(file.name, "r", encoding="utf-8") as f:
|
| 15 |
data = json.load(f)
|
| 16 |
except Exception as e:
|
| 17 |
return None, f"Error reading JSON: {e}"
|
| 18 |
|
| 19 |
-
# Create
|
| 20 |
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 21 |
pdf_path = temp_pdf.name
|
| 22 |
-
temp_pdf.close()
|
| 23 |
|
| 24 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 25 |
page_created = False
|
|
@@ -38,6 +39,7 @@ def generate_pdf(file):
|
|
| 38 |
box_height = y2 - y1
|
| 39 |
font_size = max(6, min(18, box_height * 0.8))
|
| 40 |
|
|
|
|
| 41 |
c.setStrokeColor(colors.lightgrey)
|
| 42 |
c.rect(x1, PAGE_HEIGHT - y2, x2 - x1, box_height, fill=0)
|
| 43 |
|
|
@@ -53,6 +55,7 @@ def generate_pdf(file):
|
|
| 53 |
if page_created:
|
| 54 |
c.showPage()
|
| 55 |
|
|
|
|
| 56 |
if not page_created:
|
| 57 |
c.setFont("Helvetica", 12)
|
| 58 |
c.drawString(50, 750, "No OCR text found")
|
|
@@ -61,3 +64,17 @@ def generate_pdf(file):
|
|
| 61 |
c.save()
|
| 62 |
|
| 63 |
return pdf_path, "PDF generated successfully!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from reportlab.pdfgen import canvas
|
| 5 |
from reportlab.lib import colors
|
| 6 |
import tempfile
|
|
|
|
| 7 |
|
| 8 |
PAGE_WIDTH, PAGE_HEIGHT = letter
|
| 9 |
|
| 10 |
+
# -------------------
|
| 11 |
+
# PDF generation function
|
| 12 |
+
# -------------------
|
| 13 |
def generate_pdf(file):
|
| 14 |
try:
|
|
|
|
| 15 |
with open(file.name, "r", encoding="utf-8") as f:
|
| 16 |
data = json.load(f)
|
| 17 |
except Exception as e:
|
| 18 |
return None, f"Error reading JSON: {e}"
|
| 19 |
|
| 20 |
+
# Create temporary PDF file
|
| 21 |
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 22 |
pdf_path = temp_pdf.name
|
| 23 |
+
temp_pdf.close()
|
| 24 |
|
| 25 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 26 |
page_created = False
|
|
|
|
| 39 |
box_height = y2 - y1
|
| 40 |
font_size = max(6, min(18, box_height * 0.8))
|
| 41 |
|
| 42 |
+
# Draw bounding box for debug
|
| 43 |
c.setStrokeColor(colors.lightgrey)
|
| 44 |
c.rect(x1, PAGE_HEIGHT - y2, x2 - x1, box_height, fill=0)
|
| 45 |
|
|
|
|
| 55 |
if page_created:
|
| 56 |
c.showPage()
|
| 57 |
|
| 58 |
+
# Guarantee at least one page
|
| 59 |
if not page_created:
|
| 60 |
c.setFont("Helvetica", 12)
|
| 61 |
c.drawString(50, 750, "No OCR text found")
|
|
|
|
| 64 |
c.save()
|
| 65 |
|
| 66 |
return pdf_path, "PDF generated successfully!"
|
| 67 |
+
|
| 68 |
+
# -------------------
|
| 69 |
+
# Gradio Interface
|
| 70 |
+
# -------------------
|
| 71 |
+
iface = gr.Interface(
|
| 72 |
+
fn=generate_pdf,
|
| 73 |
+
inputs=gr.File(file_types=[".json"], label="Upload JSON"),
|
| 74 |
+
outputs=[gr.File(label="Download PDF"), gr.Textbox(label="Status")],
|
| 75 |
+
title="JSON to PDF Converter",
|
| 76 |
+
description="Upload a `results.json` file and download the generated PDF."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Launch the app
|
| 80 |
+
iface.launch()
|