developer28's picture
Update app.py
a6e9713 verified
raw
history blame
1.17 kB
import gradio as gr
from xhtml2pdf import pisa
from io import BytesIO
import os
import uuid
import tempfile
def generate_dummy_pdf():
# βœ… Step 1: Create simple HTML
html = """
<html>
<body>
<h1>Hello PDF</h1>
<p>This is a test PDF generated by xhtml2pdf.</p>
</body>
</html>
"""
# βœ… Step 2: Generate PDF into memory
pdf_data = BytesIO()
result = pisa.CreatePDF(html, dest=pdf_data)
# βœ… Step 3: Error handling
if result.err:
return "❌ PDF generation failed"
# βœ… Step 4: Write PDF to a real file
pdf_data.seek(0)
output_path = os.path.join(tempfile.gettempdir(), f"test_report_{uuid.uuid4().hex}.pdf")
with open(output_path, "wb") as f:
f.write(pdf_data.read())
print("βœ… PDF generated at:", output_path)
return output_path # βœ… Gradio File component works with actual file path
# βœ… Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## πŸ§ͺ PDF Generation Test")
btn = gr.Button("πŸ“„ Generate PDF Report")
file_output = gr.File(label="πŸ“₯ Download PDF")
btn.click(fn=generate_dummy_pdf, inputs=[], outputs=file_output)
demo.launch()