diff --git a/core/views.py b/core/views.py index 948e9c8..596271e 100644 --- a/core/views.py +++ b/core/views.py @@ -18,10 +18,20 @@ def is_weasyprint_available(): if WEASYPRINT_AVAILABLE is None: try: from weasyprint import HTML + # 测试WeasyPrint是否真正可用,检查依赖库 + test_html = HTML(string="Test") + # 尝试生成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 (