File size: 1,098 Bytes
51551c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import gradio as gr
import tempfile
from playwright.sync_api import sync_playwright

# --------------------------------
# HTML → PNG function
# --------------------------------
def html_to_png(html_path):
    output_png = tempfile.NamedTemporaryFile(
        delete=False, suffix=".png"
    ).name

    with sync_playwright() as p:
        browser = p.chromium.launch(
            args=["--no-sandbox", "--disable-dev-shm-usage"]
        )
        page = browser.new_page(
            viewport={"width": 2000, "height": 1414}
        )
        page.goto(f"file://{html_path}", wait_until="networkidle")
        page.screenshot(path=output_png)
        browser.close()

    return output_png  # ✅ MUST RETURN FILE PATH

# --------------------------------
# Gradio Interface (API)
# --------------------------------
demo = gr.Interface(
    fn=html_to_png,
    inputs=gr.File(type="filepath", label="HTML Input"),
    outputs=gr.File(type="filepath", label="PNG Output"),
    title="HTML → PNG Certificate API",
    description="Production-ready HTML to PNG API"
)

demo.launch(ssr_mode=False)