Spaces:
Paused
Paused
| import gradio as gr | |
| from weasyprint import HTML, CSS | |
| def generate_pdf(html_content): | |
| """ | |
| Takes HTML content and generates a PDF using a self-contained CSS file. | |
| """ | |
| # We let WeasyPrint handle the CSS file directly. | |
| # The font is embedded in the CSS, so no other paths are needed. | |
| html = HTML(string=html_content) | |
| pdf_bytes = html.write_pdf(stylesheets=[CSS('style.css')]) | |
| output_filename = "final_document.pdf" | |
| with open(output_filename, "wb") as f: | |
| f.write(pdf_bytes) | |
| return output_filename | |
| example_html = """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head><title>MCQ Document</title></head> | |
| <body> | |
| <div class="content-wrapper"> | |
| <h2>अध्याय 1: इतिहास</h2> | |
| <div class="mcq-item"> | |
| <p class="question-text">1. इस अंतिम प्रयास से बनाया गया प्रश्न।</p> | |
| <div class="options-grid"> | |
| <div class="option-item">(A) ऑप्शन A</div><div class="option-item">(B) ऑप्शन B</div> | |
| <div class="option-item">(C) ऑप्शन C</div><div class="option-item">(D) ऑप्शन D</div> | |
| </div> | |
| </div> | |
| <h2>अध्याय 2: भूगोल</h2> | |
| <div class="mcq-item"> | |
| <p class="question-text">2. भूगोल के इस प्रश्न का उत्तर क्या है?</p> | |
| <div class="options-grid"> | |
| <div class="option-item">(A) ऑप्शन A</div><div class="option-item">(B) ऑप्शन B</div> | |
| <div class="option-item">(C) ऑप्शन C</div><div class="option-item">(D) ऑप्शन D</div> | |
| </div> | |
| </div> | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| iface = gr.Interface( | |
| fn=generate_pdf, | |
| inputs=gr.Code(value=example_html, language="html", label="HTML Input"), | |
| outputs=gr.File(label="Download PDF"), | |
| title="MCQ PDF Generator (Built-in Font)", | |
| description="This version has the Devanagari font embedded directly. No separate font file is needed.", | |
| allow_flagging="never" | |
| ) | |
| iface.launch() |