How to use CJK font to render text
Load the font with model.NewCompositePdfFontFile, which accepts .ttf and .otf, and
set it on the text.
cjkFont, err := model.NewCompositePdfFontFile("./rounded-mplus-1p-regular.ttf")
if err != nil {
return err
}
p := c.NewStyledParagraph()
p.SetText("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c") // "hello world" in Japanese
p.SetFont(cjkFont)
if err := c.Draw(p); err != nil {
return err
}Composite means a Type0 font with a multi-byte encoding, and that is the whole reason this
call exists. model.NewPdfFontFromTTFFile produces a simple font limited to a single-byte
encoding, which cannot carry CJK, and the standard 14 built-in fonts have no CJK glyphs at
all. Neither reports a problem; you get missing or wrong glyphs in the output.
The font has to have the glyphs. UniPDF embeds what you give it and does not fall back to another face, so a Latin font loaded as composite still renders nothing useful for Japanese.
Embed the subset. A CJK face carries tens of thousands of glyphs and takes the output file
past a megabyte on its own. c.EnableFontSubsetting(cjkFont) embeds only the runes the
document actually uses, and it has to be called before the write. The linked example drops
from 1.4 MB to 74 KB that way.
The worked example is text/pdf_using_unicode_font.go, which writes Japanese and a page of currency symbols with the same font. For right-to-left scripts and text shaping, see the text manipulation guides.