fix(core): 增强WeasyPrint可用性检测逻辑

添加实际PDF生成测试以验证WeasyPrint依赖库是否真正可用
捕获并记录所有异常情况,确保PDF功能状态准确
This commit is contained in:
2026-01-21 22:04:18 +08:00
parent eee44ad342
commit 32da4c9cf1

View File

@@ -18,10 +18,20 @@ def is_weasyprint_available():
if WEASYPRINT_AVAILABLE is None:
try:
from weasyprint import HTML
# 测试WeasyPrint是否真正可用检查依赖库
test_html = HTML(string="<html><body>Test</body></html>")
# 尝试生成PDF以验证依赖库是否可用
import tempfile
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=True) as tmp:
test_html.write_pdf(tmp.name)
WEASYPRINT_AVAILABLE = True
logger.info("WeasyPrint库可用PDF功能正常")
except ImportError:
logger.warning("WeasyPrint库无法导入PDF功能将不可用")
WEASYPRINT_AVAILABLE = False
except Exception as e:
logger.warning(f"WeasyPrint依赖库不可用: {str(e)}PDF功能将不可用")
WEASYPRINT_AVAILABLE = False
return WEASYPRINT_AVAILABLE
from .models import (