pythonweasyprint

How to display Chinese in PDF generated by weasyprint?


I try to set the font, but it still fails.

Python

from weasyprint import HTML, CSS
from weasyprint.fonts import FontConfiguration

font_config = FontConfiguration()

stylesheets=[CSS('https://fonts.googleapis.com/css?family=Noto+Sans+TC'), CSS("sample.css", font_config=font_config)]

html.write_pdf("test.pdf", stylesheets=stylesheets)

CSS

body {
  font-family: 'Noto Sans TC', '微軟正黑體', sans-serif;
  font-display: swap;
}

PDF

I hope that traditional Chinese can be displayed normally.


Solution

  • It just adds this, I don't even need to set fonts in weasyprint.

    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;500;700&display=swap" rel="stylesheet" />