How to display Chinese in PDF generated by weasyprint?

132 Views Asked by At

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.

1

There are 1 best solutions below

2
Roy On BEST ANSWER

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" />