developer28's picture
Update app.py
dc946ef verified
raw
history blame
840 Bytes
import gradio as gr
from xhtml2pdf import pisa
from io import BytesIO
import os
import uuid
def generate_dummy_pdf():
html = """
<html>
<body>
<h1>Hello, PDF!</h1>
<p>This is a test PDF file.</p>
</body>
</html>
"""
result = BytesIO()
pisa_status = pisa.CreatePDF(html, dest=result)
if pisa_status.err:
print("PDF generation failed")
return None
pdf_path = os.path.join(os.path.expanduser("~"), "Downloads", f"test_pdf_{uuid.uuid4().hex}.pdf")
with open(pdf_path, "wb") as f:
f.write(result.getvalue())
print("βœ… PDF saved at:", pdf_path)
return pdf_path
with gr.Blocks() as demo:
btn = gr.Button("πŸ“„ Generate PDF")
output = gr.File(label="πŸ“₯ Download PDF")
btn.click(fn=generate_dummy_pdf, inputs=[], outputs=output)
demo.launch()