Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,70 +6,76 @@ import os
|
|
6 |
from weasyprint import HTML
|
7 |
from pathlib import Path
|
8 |
|
9 |
-
def convert_markdown_to_pdf(
|
10 |
"""
|
11 |
Convert uploaded markdown file to PDF and return both preview HTML and PDF path
|
12 |
"""
|
13 |
-
if
|
14 |
return None, None
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
<html>
|
26 |
-
<head>
|
27 |
-
<meta charset="UTF-8">
|
28 |
-
<style>
|
29 |
-
@page {{
|
30 |
-
margin: 2.5cm;
|
31 |
-
}}
|
32 |
-
body {{
|
33 |
-
font-family: Arial, sans-serif;
|
34 |
-
line-height: 1.6;
|
35 |
-
max-width: 800px;
|
36 |
-
margin: 0 auto;
|
37 |
-
padding: 20px;
|
38 |
-
}}
|
39 |
-
h1 {{ color: #2c3e50; margin-top: 1em; }}
|
40 |
-
h2 {{ color: #34495e; margin-top: 0.8em; }}
|
41 |
-
code {{
|
42 |
-
background-color: #f7f7f7;
|
43 |
-
padding: 2px 5px;
|
44 |
-
border-radius: 3px;
|
45 |
-
font-family: monospace;
|
46 |
-
}}
|
47 |
-
pre {{
|
48 |
-
background-color: #f7f7f7;
|
49 |
-
padding: 15px;
|
50 |
-
border-radius: 5px;
|
51 |
-
white-space: pre-wrap;
|
52 |
-
font-family: monospace;
|
53 |
-
}}
|
54 |
-
</style>
|
55 |
-
</head>
|
56 |
-
<body>
|
57 |
-
{html_content}
|
58 |
-
</body>
|
59 |
-
</html>
|
60 |
-
"""
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
pdf_path = pdf_file.name
|
65 |
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
HTML(string=full_html).write_pdf(pdf_path)
|
69 |
return full_html, pdf_path
|
70 |
except Exception as e:
|
71 |
print(f"Error converting to PDF: {e}")
|
72 |
-
if os.path.exists(pdf_path):
|
73 |
os.unlink(pdf_path)
|
74 |
return None, None
|
75 |
|
@@ -106,4 +112,4 @@ with gr.Blocks() as app:
|
|
106 |
)
|
107 |
|
108 |
if __name__ == "__main__":
|
109 |
-
app.launch()
|
|
|
6 |
from weasyprint import HTML
|
7 |
from pathlib import Path
|
8 |
|
9 |
+
def convert_markdown_to_pdf(file_obj):
|
10 |
"""
|
11 |
Convert uploaded markdown file to PDF and return both preview HTML and PDF path
|
12 |
"""
|
13 |
+
if file_obj is None:
|
14 |
return None, None
|
15 |
|
16 |
+
try:
|
17 |
+
# Read the markdown content from the file object
|
18 |
+
if hasattr(file_obj, 'name'):
|
19 |
+
# If it's a file path
|
20 |
+
with open(file_obj.name, 'r', encoding='utf-8') as f:
|
21 |
+
markdown_content = f.read()
|
22 |
+
else:
|
23 |
+
# If it's already string content
|
24 |
+
markdown_content = str(file_obj)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# Convert markdown to HTML
|
27 |
+
html_content = markdown.markdown(markdown_content)
|
|
|
28 |
|
29 |
+
# Wrap HTML content with proper HTML structure and CSS
|
30 |
+
full_html = f"""
|
31 |
+
<!DOCTYPE html>
|
32 |
+
<html>
|
33 |
+
<head>
|
34 |
+
<meta charset="UTF-8">
|
35 |
+
<style>
|
36 |
+
@page {{
|
37 |
+
margin: 2.5cm;
|
38 |
+
}}
|
39 |
+
body {{
|
40 |
+
font-family: Arial, sans-serif;
|
41 |
+
line-height: 1.6;
|
42 |
+
max-width: 800px;
|
43 |
+
margin: 0 auto;
|
44 |
+
padding: 20px;
|
45 |
+
}}
|
46 |
+
h1 {{ color: #2c3e50; margin-top: 1em; }}
|
47 |
+
h2 {{ color: #34495e; margin-top: 0.8em; }}
|
48 |
+
code {{
|
49 |
+
background-color: #f7f7f7;
|
50 |
+
padding: 2px 5px;
|
51 |
+
border-radius: 3px;
|
52 |
+
font-family: monospace;
|
53 |
+
}}
|
54 |
+
pre {{
|
55 |
+
background-color: #f7f7f7;
|
56 |
+
padding: 15px;
|
57 |
+
border-radius: 5px;
|
58 |
+
white-space: pre-wrap;
|
59 |
+
font-family: monospace;
|
60 |
+
}}
|
61 |
+
</style>
|
62 |
+
</head>
|
63 |
+
<body>
|
64 |
+
{html_content}
|
65 |
+
</body>
|
66 |
+
</html>
|
67 |
+
"""
|
68 |
+
|
69 |
+
# Create temporary file for PDF
|
70 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as pdf_file:
|
71 |
+
pdf_path = pdf_file.name
|
72 |
+
|
73 |
+
# Convert HTML to PDF using WeasyPrint
|
74 |
HTML(string=full_html).write_pdf(pdf_path)
|
75 |
return full_html, pdf_path
|
76 |
except Exception as e:
|
77 |
print(f"Error converting to PDF: {e}")
|
78 |
+
if 'pdf_path' in locals() and os.path.exists(pdf_path):
|
79 |
os.unlink(pdf_path)
|
80 |
return None, None
|
81 |
|
|
|
112 |
)
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
+
app.launch(share=True) # Added share=True for public access
|