Update app.py
Browse files
app.py
CHANGED
|
@@ -3,20 +3,25 @@ import json
|
|
| 3 |
from reportlab.lib.pagesizes import letter
|
| 4 |
from reportlab.pdfgen import canvas
|
| 5 |
from reportlab.lib import colors
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
PAGE_WIDTH, PAGE_HEIGHT = letter
|
| 9 |
|
| 10 |
def generate_pdf(file):
|
| 11 |
try:
|
| 12 |
-
#
|
| 13 |
with open(file.name, "r", encoding="utf-8") as f:
|
| 14 |
data = json.load(f)
|
| 15 |
except Exception as e:
|
| 16 |
return None, f"Error reading JSON: {e}"
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
page_created = False
|
| 21 |
|
| 22 |
for ad_size, ad_data in data.items():
|
|
@@ -33,7 +38,6 @@ def generate_pdf(file):
|
|
| 33 |
box_height = y2 - y1
|
| 34 |
font_size = max(6, min(18, box_height * 0.8))
|
| 35 |
|
| 36 |
-
# Draw bounding box (optional for debug)
|
| 37 |
c.setStrokeColor(colors.lightgrey)
|
| 38 |
c.rect(x1, PAGE_HEIGHT - y2, x2 - x1, box_height, fill=0)
|
| 39 |
|
|
@@ -49,23 +53,11 @@ def generate_pdf(file):
|
|
| 49 |
if page_created:
|
| 50 |
c.showPage()
|
| 51 |
|
| 52 |
-
# Guarantee at least one page
|
| 53 |
if not page_created:
|
| 54 |
c.setFont("Helvetica", 12)
|
| 55 |
c.drawString(50, 750, "No OCR text found")
|
| 56 |
c.showPage()
|
| 57 |
|
| 58 |
c.save()
|
| 59 |
-
pdf_output.seek(0)
|
| 60 |
-
return pdf_output, "PDF generated successfully!"
|
| 61 |
-
|
| 62 |
-
# Gradio interface
|
| 63 |
-
iface = gr.Interface(
|
| 64 |
-
fn=generate_pdf,
|
| 65 |
-
inputs=gr.File(file_types=[".json"], label="Upload JSON"),
|
| 66 |
-
outputs=[gr.File(label="Download PDF"), gr.Textbox(label="Status")],
|
| 67 |
-
title="JSON to PDF Converter",
|
| 68 |
-
description="Upload a `results.json` file and download the generated PDF."
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
|
|
|
|
| 3 |
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 a temporary file for the PDF
|
| 20 |
+
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 21 |
+
pdf_path = temp_pdf.name
|
| 22 |
+
temp_pdf.close() # ReportLab will write to this path
|
| 23 |
+
|
| 24 |
+
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 25 |
page_created = False
|
| 26 |
|
| 27 |
for ad_size, ad_data in data.items():
|
|
|
|
| 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 |
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")
|
| 59 |
c.showPage()
|
| 60 |
|
| 61 |
c.save()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
return pdf_path, "PDF generated successfully!"
|