-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_pdf.py
More file actions
138 lines (115 loc) · 4.29 KB
/
Copy pathgenerate_pdf.py
File metadata and controls
138 lines (115 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
"""Generate PDF from HTML using available tools."""
import subprocess
import sys
from pathlib import Path
def try_playwright():
"""Try using playwright for PDF generation."""
try:
from playwright.sync_api import sync_playwright
html_file = Path(__file__).parent / "cashflow-architecture.html"
pdf_file = Path(__file__).parent / "cashflow-architecture.pdf"
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto(f"file://{html_file.absolute()}")
# Wait for mermaid to be defined and rendered
page.wait_for_function("typeof mermaid !== 'undefined'")
page.wait_for_timeout(3000) # Extra time for rendering
# Wait for SVG to appear (mermaid renders to SVG)
page.wait_for_selector("svg", timeout=10000)
page.wait_for_timeout(1000) # Extra settling time
page.pdf(
path=str(pdf_file),
format="A4",
landscape=True,
print_background=True,
margin={
"top": "20px",
"right": "20px",
"bottom": "20px",
"left": "20px"
}
)
browser.close()
print(f"✅ PDF generated successfully: {pdf_file}")
return True
except ImportError:
print("⚠️ Playwright not available")
return False
except Exception as e:
print(f"⚠️ Playwright failed: {e}")
return False
def try_weasyprint():
"""Try using weasyprint for PDF generation."""
try:
from weasyprint import HTML
html_file = Path(__file__).parent / "cashflow-architecture.html"
pdf_file = Path(__file__).parent / "cashflow-architecture.pdf"
HTML(filename=str(html_file)).write_pdf(pdf_file)
print(f"✅ PDF generated successfully: {pdf_file}")
return True
except ImportError:
print("⚠️ WeasyPrint not available")
return False
except Exception as e:
print(f"⚠️ WeasyPrint failed: {e}")
return False
def try_chrome_headless():
"""Try using Chrome/Chromium in headless mode."""
chrome_paths = [
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"/Applications/Chromium.app/Contents/MacOS/Chromium",
"google-chrome",
"chromium",
"chromium-browser"
]
html_file = Path(__file__).parent / "cashflow-architecture.html"
pdf_file = Path(__file__).parent / "cashflow-architecture.pdf"
for chrome_path in chrome_paths:
try:
result = subprocess.run(
[
chrome_path,
"--headless",
"--disable-gpu",
"--print-to-pdf=" + str(pdf_file),
"--no-margins",
str(html_file.absolute())
],
capture_output=True,
timeout=10
)
if result.returncode == 0 and pdf_file.exists():
print(f"✅ PDF generated successfully: {pdf_file}")
return True
except (FileNotFoundError, subprocess.TimeoutExpired):
continue
except Exception as e:
print(f"⚠️ Chrome at {chrome_path} failed: {e}")
continue
print("⚠️ Chrome/Chromium not available or failed")
return False
def main():
"""Try different methods to generate PDF."""
print("Attempting to generate PDF from HTML...")
print()
methods = [
("Playwright", try_playwright),
("Chrome Headless", try_chrome_headless),
("WeasyPrint", try_weasyprint),
]
for name, method in methods:
print(f"Trying {name}...")
if method():
return 0
print()
print("❌ Could not generate PDF with any available method.")
print()
print("To generate the PDF, you can:")
print("1. Install playwright: pip install playwright && playwright install chromium")
print("2. Open cashflow-architecture.html in your browser and use Print to PDF")
print("3. Install Chrome and we'll use headless mode")
return 1
if __name__ == "__main__":
sys.exit(main())