markdown-to-pdf / app.py
Sugam7's picture
Rename index.py to app.py
225947b verified
raw
history blame
1.44 kB
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)