Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import markdown
|
| 4 |
+
import pdfkit
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
def convert_markdown_to_pdf(markdown_file):
|
| 10 |
+
"""
|
| 11 |
+
Convert uploaded markdown file to PDF and return both preview HTML and PDF path
|
| 12 |
+
"""
|
| 13 |
+
if markdown_file is None:
|
| 14 |
+
return None, None
|
| 15 |
+
|
| 16 |
+
# Read the markdown content
|
| 17 |
+
markdown_content = markdown_file.decode('utf-8')
|
| 18 |
+
|
| 19 |
+
# Convert markdown to HTML
|
| 20 |
+
html_content = markdown.markdown(markdown_content)
|
| 21 |
+
|
| 22 |
+
# Wrap HTML content with proper HTML structure
|
| 23 |
+
full_html = f"""
|
| 24 |
+
<!DOCTYPE html>
|
| 25 |
+
<html>
|
| 26 |
+
<head>
|
| 27 |
+
<meta charset="UTF-8">
|
| 28 |
+
<style>
|
| 29 |
+
body {{ font-family: Arial, sans-serif; line-height: 1.6; max-width: 800px; margin: 0 auto; padding: 20px; }}
|
| 30 |
+
h1 {{ color: #2c3e50; }}
|
| 31 |
+
h2 {{ color: #34495e; }}
|
| 32 |
+
code {{ background-color: #f7f7f7; padding: 2px 5px; border-radius: 3px; }}
|
| 33 |
+
pre {{ background-color: #f7f7f7; padding: 15px; border-radius: 5px; overflow-x: auto; }}
|
| 34 |
+
</style>
|
| 35 |
+
</head>
|
| 36 |
+
<body>
|
| 37 |
+
{html_content}
|
| 38 |
+
</body>
|
| 39 |
+
</html>
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
# Create temporary files for HTML and PDF
|
| 43 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.html') as html_file:
|
| 44 |
+
html_file.write(full_html.encode('utf-8'))
|
| 45 |
+
html_path = html_file.name
|
| 46 |
+
|
| 47 |
+
pdf_path = html_path.replace('.html', '.pdf')
|
| 48 |
+
|
| 49 |
+
# Convert HTML to PDF using pdfkit
|
| 50 |
+
try:
|
| 51 |
+
pdfkit.from_file(html_path, pdf_path)
|
| 52 |
+
os.unlink(html_path) # Clean up temporary HTML file
|
| 53 |
+
return full_html, pdf_path
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Error converting to PDF: {e}")
|
| 56 |
+
if os.path.exists(html_path):
|
| 57 |
+
os.unlink(html_path)
|
| 58 |
+
return None, None
|
| 59 |
+
|
| 60 |
+
def process_file(file):
|
| 61 |
+
"""
|
| 62 |
+
Process the uploaded file and return preview and PDF download
|
| 63 |
+
"""
|
| 64 |
+
if file is None:
|
| 65 |
+
return None, None
|
| 66 |
+
|
| 67 |
+
preview_html, pdf_path = convert_markdown_to_pdf(file)
|
| 68 |
+
|
| 69 |
+
if preview_html is None or pdf_path is None:
|
| 70 |
+
return "Error converting file", None
|
| 71 |
+
|
| 72 |
+
return preview_html, pdf_path
|
| 73 |
+
|
| 74 |
+
# Create Gradio interface
|
| 75 |
+
with gr.Blocks() as app:
|
| 76 |
+
gr.Markdown("# Markdown to PDF Converter")
|
| 77 |
+
gr.Markdown("Upload a markdown file to convert it to PDF. You can preview the result and download the PDF.")
|
| 78 |
+
|
| 79 |
+
with gr.Row():
|
| 80 |
+
file_input = gr.File(label="Upload Markdown File", file_types=[".md", ".markdown"])
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
preview = gr.HTML(label="Preview")
|
| 84 |
+
pdf_output = gr.File(label="Download PDF")
|
| 85 |
+
|
| 86 |
+
file_input.upload(
|
| 87 |
+
fn=process_file,
|
| 88 |
+
inputs=[file_input],
|
| 89 |
+
outputs=[preview, pdf_output]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
app.launch()
|