Spaces:
Sleeping
Sleeping
File size: 1,167 Bytes
e1c4426 dc946ef 4fde749 a6e9713 39fa25b dc946ef a6e9713 dc946ef a6e9713 dc946ef e1c4426 a6e9713 39fa25b a6e9713 2c78092 a6e9713 39fa25b a6e9713 dc946ef a6e9713 39fa25b a6e9713 95cc944 dc946ef |
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 39 40 41 42 43 44 45 |
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()
|