Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
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):
|
@@ -19,18 +19,38 @@ def convert_markdown_to_pdf(markdown_file):
|
|
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 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
</style>
|
35 |
</head>
|
36 |
<body>
|
@@ -39,22 +59,18 @@ def convert_markdown_to_pdf(markdown_file):
|
|
39 |
</html>
|
40 |
"""
|
41 |
|
42 |
-
# Create temporary
|
43 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.
|
44 |
-
|
45 |
-
html_path = html_file.name
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
# Convert HTML to PDF using pdfkit
|
50 |
try:
|
51 |
-
|
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(
|
57 |
-
os.unlink(
|
58 |
return None, None
|
59 |
|
60 |
def process_file(file):
|
|
|
1 |
# app.py
|
2 |
import gradio as gr
|
3 |
import markdown
|
|
|
4 |
import tempfile
|
5 |
import os
|
6 |
+
from weasyprint import HTML
|
7 |
from pathlib import Path
|
8 |
|
9 |
def convert_markdown_to_pdf(markdown_file):
|
|
|
19 |
# Convert markdown to HTML
|
20 |
html_content = markdown.markdown(markdown_content)
|
21 |
|
22 |
+
# Wrap HTML content with proper HTML structure and CSS
|
23 |
full_html = f"""
|
24 |
<!DOCTYPE html>
|
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>
|
|
|
59 |
</html>
|
60 |
"""
|
61 |
|
62 |
+
# Create temporary file for PDF
|
63 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as pdf_file:
|
64 |
+
pdf_path = pdf_file.name
|
|
|
65 |
|
66 |
+
# Convert HTML to PDF using WeasyPrint
|
|
|
|
|
67 |
try:
|
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 |
|
76 |
def process_file(file):
|