2929from src .database import Database # noqa: E402
3030from src .emailer import _EMAIL_CSS , _PYGMENTS_CSS , _md_to_html # noqa: E402
3131
32+ # Override hardcoded pixel dimensions for PDF rendering.
33+ # WeasyPrint maps CSS px to physical size at 96 DPI, which makes the
34+ # pre-scaled latex images appear too small. This CSS lets the renderer
35+ # size them naturally based on the image's intrinsic dimensions instead.
36+ _PDF_LATEX_CSS = "img { max-width: 100% !important; height: auto !important; }"
3237
33- def _build_html (course_title : str , teacher : str , lectures : list [dict ]) -> str :
38+
39+ def _build_html (course_title : str , teacher : str , lectures : list [dict ],
40+ pdf : bool = False ) -> str :
3441 """Build a complete styled HTML document from course summaries."""
3542 body_parts = [
3643 f"<h1>{ escape (course_title )} </h1>" ,
@@ -45,10 +52,11 @@ def _build_html(course_title: str, teacher: str, lectures: list[dict]) -> str:
4552 body_parts .append (_md_to_html (lec ["summary" ]))
4653 body_parts .append ("<hr>" )
4754
55+ extra_css = f"\n { _PDF_LATEX_CSS } " if pdf else ""
4856 return (
4957 "<!DOCTYPE html>"
5058 "<html><head><meta charset='utf-8'>"
51- f"<style>{ _EMAIL_CSS } \n { _PYGMENTS_CSS } </style>"
59+ f"<style>{ _EMAIL_CSS } \n { _PYGMENTS_CSS } { extra_css } </style>"
5260 "</head><body>"
5361 + "\n " .join (body_parts )
5462 + "</body></html>"
@@ -155,7 +163,7 @@ def main():
155163 print ("Email configuration incomplete. Set SMTP_EMAIL, SMTP_PASSWORD, RECEIVER_EMAIL." )
156164 sys .exit (1 )
157165
158- html = _build_html (course_title , teacher , lectures )
166+ html = _build_html (course_title , teacher , lectures , pdf = args . pdf )
159167 subject = f"[iCourse 课程摘要导出] { course_title } "
160168
161169 if args .pdf :
0 commit comments