File size: 1,442 Bytes
b070cfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
import markdown2
from weasyprint import HTML
import gradio as gr
import tempfile

# Function to convert Markdown to PDF with CSS styling
def convert_markdown_to_pdf(markdown_text, css_styles):
    # Convert Markdown to HTML
    html_content = markdown2.markdown(markdown_text)
    
    # Add CSS styling to the HTML content
    styled_html_content = f"""
    <html>
    <head>
        <style>
        {css_styles}
        </style>
    </head>
    <body>
        {html_content}
    </body>
    </html>
    """
    
    # Convert HTML to PDF
    pdf_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
    HTML(string=styled_html_content).write_pdf(pdf_file.name)
    
    return pdf_file.name

# Define Gradio interface
def markdown_to_pdf_interface(markdown_text, css_styles):
    pdf_path = convert_markdown_to_pdf(markdown_text, css_styles)
    return pdf_path

# Create and launch Gradio interface
iface = gr.Interface(
    fn=markdown_to_pdf_interface,
    inputs=[
        gr.Textbox(lines=20, placeholder="Paste your Markdown text here...", label="Markdown"),
        gr.Textbox(lines=10, placeholder="Enter CSS styles here...", label="CSS Styles")
    ],
    outputs=gr.File(label="Download PDF"),
    title="Markdown to PDF Converter with Custom Styling",
    description="Paste Markdown text and CSS styles, then click 'Submit' to generate and download the styled PDF."
)

# Launch the interface
iface.launch(share=True)