56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
import os
|
|
import ctypes
|
|
import ctypes.util
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def patch_weasyprint_libraries():
|
|
"""
|
|
Attempts to load required system libraries for WeasyPrint.
|
|
This helps on systems where libraries are installed but not in the standard search path
|
|
or have slightly different names.
|
|
"""
|
|
# Common library names for WeasyPrint dependencies
|
|
libs_to_load = [
|
|
('glib-2.0', ['libglib-2.0.so.0', 'libglib-2.0.so']),
|
|
('gobject-2.0', ['libgobject-2.0.so.0', 'libgobject-2.0.so', 'libgobject-2.0-0']),
|
|
('fontconfig', ['libfontconfig.so.1', 'libfontconfig.so']),
|
|
('cairo', ['libcairo.so.2', 'libcairo.so']),
|
|
('pango-1.0', ['libpango-1.0.so.0', 'libpango-1.0.so']),
|
|
('pangoft2-1.0', ['libpangoft2-1.0.so.0', 'libpangoft2-1.0.so']),
|
|
('harfbuzz', ['libharfbuzz.so.0', 'libharfbuzz.so']),
|
|
]
|
|
|
|
for lib_id, fallbacks in libs_to_load:
|
|
try:
|
|
# First try standard find_library
|
|
path = ctypes.util.find_library(lib_id)
|
|
if path:
|
|
ctypes.CDLL(path)
|
|
continue
|
|
|
|
# If not found, try fallbacks
|
|
for fallback in fallbacks:
|
|
try:
|
|
ctypes.CDLL(fallback)
|
|
break
|
|
except OSError:
|
|
continue
|
|
except Exception as e:
|
|
logger.debug(f"Failed to load library {lib_id}: {e}")
|
|
|
|
# Call it immediately when this module is imported
|
|
patch_weasyprint_libraries()
|
|
|
|
def get_weasyprint_html():
|
|
"""
|
|
Safe wrapper for importing WeasyPrint HTML.
|
|
"""
|
|
try:
|
|
from weasyprint import HTML
|
|
return HTML
|
|
except Exception as e:
|
|
logger.error(f"Failed to import WeasyPrint HTML: {e}")
|
|
raise
|