import gradio as gr import json from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas from reportlab.lib import colors import tempfile PAGE_WIDTH, PAGE_HEIGHT = letter # ------------------- # PDF generation function # ------------------- def generate_pdf(file): try: with open(file.name, "r", encoding="utf-8") as f: data = json.load(f) except Exception as e: return None, f"Error reading JSON: {e}" temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") pdf_path = temp_pdf.name temp_pdf.close() c = canvas.Canvas(pdf_path, pagesize=letter) page_created = False # Current vertical position current_y = PAGE_HEIGHT - 50 # start from top margin bottom_margin = 50 for ad_size, ad_data in data.items(): for item in ad_data: for line in item.get("text_lines", []): text = line.get("text", "") bbox = line.get("bbox") if not bbox: continue page_created = True x1, y1, x2, y2 = bbox # Height of text box box_height = y2 - y1 font_size = max(6, min(18, box_height * 0.8)) # Check if the next line fits in current page, else start new page if current_y - font_size < bottom_margin: c.showPage() current_y = PAGE_HEIGHT - 50 # reset for new page pdf_x = x1 pdf_y = current_y - box_height # Optional: draw bounding box c.setStrokeColor(colors.lightgrey) c.rect(pdf_x, pdf_y, x2 - x1, box_height, fill=0) if "" in text: c.setFont("Helvetica-Bold", font_size) text = text.replace("", "").replace("", "") else: c.setFont("Helvetica", font_size) c.setFillColor(colors.black) c.drawString(pdf_x, pdf_y + (box_height - font_size), text) current_y = pdf_y - 5 # move cursor down with small gap if not page_created: c.setFont("Helvetica", 12) c.drawString(50, 750, "No OCR text found") c.showPage() c.save() return pdf_path, "PDF generated successfully!" # ------------------- # Gradio Interface # ------------------- iface = gr.Interface( fn=generate_pdf, inputs=gr.File(file_types=[".json"], label="Upload JSON"), outputs=[gr.File(label="Download PDF"), gr.Textbox(label="Status")], title="JSON to PDF Converter", description="Upload a `results.json` file and download the generated PDF." ) # Launch the app iface.launch()