wiki/offerings/main.py
2025-09-22 00:39:23 +02:00

146 lines
4.2 KiB
Python

#!/usr/bin/env python3
"""
Simple HTML to PDF Generation using Chrome headless
Renders HTML file to PDF without TOC or page analysis
"""
import os
import sys
import subprocess
import platform
from pathlib import Path
def find_chrome_executable():
"""Find Chrome/Chromium executable based on platform"""
system = platform.system()
if system == "Windows":
# Common Chrome paths on Windows
chrome_paths = [
r"C:\Program Files\Google\Chrome\Application\chrome.exe",
r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
r"C:\Users\{}\AppData\Local\Google\Chrome\Application\chrome.exe".format(os.getenv('USERNAME')),
]
for path in chrome_paths:
if os.path.exists(path):
return path
# Try to find from registry or PATH
try:
result = subprocess.run(['where', 'chrome'], capture_output=True, text=True)
if result.returncode == 0:
return result.stdout.strip().split('\n')[0]
except:
pass
elif system == "Darwin": # macOS
chrome_paths = [
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"/Applications/Chromium.app/Contents/MacOS/Chromium"
]
for path in chrome_paths:
if os.path.exists(path):
return path
else: # Linux
chrome_paths = [
"google-chrome",
"chromium-browser",
"chromium",
"/usr/bin/google-chrome",
"/usr/bin/chromium-browser"
]
for path in chrome_paths:
try:
result = subprocess.run(['which', path], capture_output=True)
if result.returncode == 0:
return path
except:
continue
return None
def generate_pdf_chrome(html_path, output_path, chrome_path):
"""Generate PDF using Chrome headless"""
try:
# Convert to absolute paths
html_path = Path(html_path).resolve()
output_path = Path(output_path).resolve()
# Chrome headless command
cmd = [
chrome_path,
"--headless",
"--disable-gpu",
"--no-pdf-header-footer", # Remove Chrome's default headers
f"--print-to-pdf={output_path}",
f"--print-to-pdf-no-header",
f"file://{html_path}"
]
print(f"🔄 Running Chrome headless...")
print(f" Input: {html_path}")
print(f" Output: {output_path}")
# Run Chrome
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
print(f"✅ PDF generated successfully!")
print(f" Location: {output_path}")
return True
else:
print(f"❌ Chrome error: {result.stderr}")
return False
except Exception as e:
print(f"❌ Error: {e}")
return False
def main():
"""Main function"""
# Get script directory
script_dir = Path(__file__).parent
energie360_dir = script_dir / 'energie360'
# File paths
html_file = energie360_dir / 'energie360-offerte.html'
output_pdf = energie360_dir / 'energie360-offerte.pdf'
# Check if HTML file exists
if not html_file.exists():
print(f"❌ HTML file not found: {html_file}")
return False
print("🚀 HTML to PDF Generation")
print("=" * 30)
# Try to find Chrome
chrome_path = find_chrome_executable()
if chrome_path:
print(f"✅ Chrome found: {chrome_path}")
# Generate PDF
print("\n🔄 Generating PDF...")
success = generate_pdf_chrome(html_file, output_pdf, chrome_path)
if success:
print(f"\n🎉 PDF generated successfully!")
print(f"📄 Output: {output_pdf}")
return True
else:
print("\n❌ PDF generation failed")
return False
else:
print("⚠️ Chrome not found, cannot generate PDF")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)