File size: 3,539 Bytes
e2d8fbc
 
 
 
 
81b49c9
e2d8fbc
 
219e60c
e2d8fbc
 
 
219e60c
e2d8fbc
 
219e60c
 
 
 
 
 
 
 
 
e2d8fbc
219e60c
 
e2d8fbc
219e60c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81b49c9
e2d8fbc
 
 
219e60c
81b49c9
e2d8fbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219e60c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# app.py
import gradio as gr
import markdown
import tempfile
import os
from weasyprint import HTML
from pathlib import Path

def convert_markdown_to_pdf(file_obj):
    """
    Convert uploaded markdown file to PDF and return both preview HTML and PDF path
    """
    if file_obj is None:
        return None, None
    
    try:
        # Read the markdown content from the file object
        if hasattr(file_obj, 'name'):
            # If it's a file path
            with open(file_obj.name, 'r', encoding='utf-8') as f:
                markdown_content = f.read()
        else:
            # If it's already string content
            markdown_content = str(file_obj)
    
        # Convert markdown to HTML
        html_content = markdown.markdown(markdown_content)
        
        # Wrap HTML content with proper HTML structure and CSS
        full_html = f"""
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <style>
                @page {{
                    margin: 2.5cm;
                }}
                body {{
                    font-family: Arial, sans-serif;
                    line-height: 1.6;
                    max-width: 800px;
                    margin: 0 auto;
                    padding: 20px;
                }}
                h1 {{ color: #2c3e50; margin-top: 1em; }}
                h2 {{ color: #34495e; margin-top: 0.8em; }}
                code {{ 
                    background-color: #f7f7f7;
                    padding: 2px 5px;
                    border-radius: 3px;
                    font-family: monospace;
                }}
                pre {{ 
                    background-color: #f7f7f7;
                    padding: 15px;
                    border-radius: 5px;
                    white-space: pre-wrap;
                    font-family: monospace;
                }}
            </style>
        </head>
        <body>
            {html_content}
        </body>
        </html>
        """
        
        # Create temporary file for PDF
        with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as pdf_file:
            pdf_path = pdf_file.name
            
        # Convert HTML to PDF using WeasyPrint
        HTML(string=full_html).write_pdf(pdf_path)
        return full_html, pdf_path
    except Exception as e:
        print(f"Error converting to PDF: {e}")
        if 'pdf_path' in locals() and os.path.exists(pdf_path):
            os.unlink(pdf_path)
        return None, None

def process_file(file):
    """
    Process the uploaded file and return preview and PDF download
    """
    if file is None:
        return None, None
    
    preview_html, pdf_path = convert_markdown_to_pdf(file)
    
    if preview_html is None or pdf_path is None:
        return "Error converting file", None
        
    return preview_html, pdf_path

# Create Gradio interface
with gr.Blocks() as app:
    gr.Markdown("# Markdown to PDF Converter")
    gr.Markdown("Upload a markdown file to convert it to PDF. You can preview the result and download the PDF.")
    
    with gr.Row():
        file_input = gr.File(label="Upload Markdown File", file_types=[".md", ".markdown"])
    
    with gr.Row():
        preview = gr.HTML(label="Preview")
        pdf_output = gr.File(label="Download PDF")
    
    file_input.upload(
        fn=process_file,
        inputs=[file_input],
        outputs=[preview, pdf_output]
    )

if __name__ == "__main__":
    app.launch(share=True)  # Added share=True for public access