Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,17 @@
|
|
1 |
import gradio as gr
|
2 |
import base64
|
3 |
-
from weasyprint import HTML, CSS
|
4 |
import json
|
5 |
-
from datetime import datetime
|
6 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Default CSS for professional PDFs
|
9 |
DEFAULT_CSS = """
|
@@ -21,26 +29,35 @@ th { background-color: #f4f4f4; }
|
|
21 |
|
22 |
def html_to_pdf_ui(html_content, custom_css, use_default_css):
|
23 |
"""Convert HTML to PDF for UI"""
|
|
|
|
|
|
|
24 |
try:
|
25 |
if not html_content:
|
26 |
return None, "Please provide HTML content"
|
27 |
|
28 |
# Prepare CSS
|
29 |
-
|
30 |
if use_default_css:
|
31 |
-
|
32 |
-
if custom_css:
|
33 |
-
|
34 |
|
35 |
-
# Create HTML object
|
36 |
html_obj = HTML(string=html_content)
|
37 |
|
38 |
-
# Generate PDF
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Save to temp file
|
46 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as f:
|
@@ -52,6 +69,9 @@ def html_to_pdf_ui(html_content, custom_css, use_default_css):
|
|
52 |
|
53 |
def html_to_pdf_api(json_input):
|
54 |
"""Convert HTML to PDF for API - returns base64"""
|
|
|
|
|
|
|
55 |
try:
|
56 |
data = json.loads(json_input) if isinstance(json_input, str) else json_input
|
57 |
|
@@ -62,16 +82,14 @@ def html_to_pdf_api(json_input):
|
|
62 |
if not html:
|
63 |
return {"error": "No HTML content", "status": "error"}
|
64 |
|
|
|
|
|
|
|
|
|
|
|
65 |
# Generate PDF
|
66 |
html_obj = HTML(string=html)
|
67 |
-
|
68 |
-
# Prepare CSS
|
69 |
-
full_css = DEFAULT_CSS
|
70 |
-
if css:
|
71 |
-
full_css += "\n" + css
|
72 |
-
|
73 |
-
css_obj = CSS(string=full_css)
|
74 |
-
pdf_bytes = html_obj.write_pdf(stylesheets=[css_obj])
|
75 |
|
76 |
return {
|
77 |
"status": "success",
|
@@ -87,6 +105,10 @@ def html_to_pdf_api(json_input):
|
|
87 |
with gr.Blocks(title="HTML to PDF Converter") as app:
|
88 |
gr.Markdown("# 📄 HTML to PDF Converter\nConvert HTML to PDF with custom CSS. Perfect for n8n workflows!")
|
89 |
|
|
|
|
|
|
|
|
|
90 |
with gr.Tab("✏️ Convert"):
|
91 |
with gr.Row():
|
92 |
with gr.Column():
|
|
|
1 |
import gradio as gr
|
2 |
import base64
|
|
|
3 |
import json
|
|
|
4 |
import tempfile
|
5 |
+
import os
|
6 |
+
import sys
|
7 |
+
|
8 |
+
# Try to import WeasyPrint with error handling
|
9 |
+
try:
|
10 |
+
from weasyprint import HTML, CSS
|
11 |
+
WEASYPRINT_AVAILABLE = True
|
12 |
+
except ImportError as e:
|
13 |
+
print(f"WeasyPrint import error: {e}")
|
14 |
+
WEASYPRINT_AVAILABLE = False
|
15 |
|
16 |
# Default CSS for professional PDFs
|
17 |
DEFAULT_CSS = """
|
|
|
29 |
|
30 |
def html_to_pdf_ui(html_content, custom_css, use_default_css):
|
31 |
"""Convert HTML to PDF for UI"""
|
32 |
+
if not WEASYPRINT_AVAILABLE:
|
33 |
+
return None, "❌ WeasyPrint is not available. Please check installation."
|
34 |
+
|
35 |
try:
|
36 |
if not html_content:
|
37 |
return None, "Please provide HTML content"
|
38 |
|
39 |
# Prepare CSS
|
40 |
+
stylesheets = []
|
41 |
if use_default_css:
|
42 |
+
stylesheets.append(CSS(string=DEFAULT_CSS))
|
43 |
+
if custom_css and custom_css.strip():
|
44 |
+
stylesheets.append(CSS(string=custom_css))
|
45 |
|
46 |
+
# Create HTML object and generate PDF
|
47 |
html_obj = HTML(string=html_content)
|
48 |
|
49 |
+
# Generate PDF - handle different WeasyPrint versions
|
50 |
+
try:
|
51 |
+
if stylesheets:
|
52 |
+
pdf_bytes = html_obj.write_pdf(stylesheets=stylesheets)
|
53 |
+
else:
|
54 |
+
pdf_bytes = html_obj.write_pdf()
|
55 |
+
except TypeError:
|
56 |
+
# Fallback for older WeasyPrint versions
|
57 |
+
if stylesheets:
|
58 |
+
pdf_bytes = html_obj.write_pdf(stylesheets=stylesheets)
|
59 |
+
else:
|
60 |
+
pdf_bytes = html_obj.write_pdf()
|
61 |
|
62 |
# Save to temp file
|
63 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as f:
|
|
|
69 |
|
70 |
def html_to_pdf_api(json_input):
|
71 |
"""Convert HTML to PDF for API - returns base64"""
|
72 |
+
if not WEASYPRINT_AVAILABLE:
|
73 |
+
return {"error": "WeasyPrint is not available", "status": "error"}
|
74 |
+
|
75 |
try:
|
76 |
data = json.loads(json_input) if isinstance(json_input, str) else json_input
|
77 |
|
|
|
82 |
if not html:
|
83 |
return {"error": "No HTML content", "status": "error"}
|
84 |
|
85 |
+
# Prepare stylesheets
|
86 |
+
stylesheets = [CSS(string=DEFAULT_CSS)]
|
87 |
+
if css and css.strip():
|
88 |
+
stylesheets.append(CSS(string=css))
|
89 |
+
|
90 |
# Generate PDF
|
91 |
html_obj = HTML(string=html)
|
92 |
+
pdf_bytes = html_obj.write_pdf(stylesheets=stylesheets)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
return {
|
95 |
"status": "success",
|
|
|
105 |
with gr.Blocks(title="HTML to PDF Converter") as app:
|
106 |
gr.Markdown("# 📄 HTML to PDF Converter\nConvert HTML to PDF with custom CSS. Perfect for n8n workflows!")
|
107 |
|
108 |
+
# Show WeasyPrint status
|
109 |
+
if not WEASYPRINT_AVAILABLE:
|
110 |
+
gr.Markdown("⚠️ **WeasyPrint is not available. Please check the installation.**")
|
111 |
+
|
112 |
with gr.Tab("✏️ Convert"):
|
113 |
with gr.Row():
|
114 |
with gr.Column():
|