Refer to here for a piece of code using pandoc and LaTeX, which produces the best PDF format when converting markdown.
import pypandoc
def md_to_pdf_pandoc(input_md, output_pdf):
try:
# Use custom LaTeX template or inline styles
output = pypandoc.convert_file(
input_md, 'pdf',
outputfile=output_pdf,
extra_args=[
'--pdf-engine=xelatex',
'-V', 'documentclass=ctexart',
'-V', 'CJKmainfont=PingFang SC', # Chinese main font
'-V', 'mainfont=XITS', # Set main font to XITS
'-V', 'mathfont=XITS Math', # Set math font to XITS Math
'-V', 'monofont=DejaVu Sans Mono', # Monospaced font (code blocks)
# '-V', 'geometry:margin=1in', # Set page margin
# '--template=eisvogel', # Use custom template
# '--listings',
# '--highlight-style=pygments' # Use syntax highlighting style (with background)
]
)
print(f"PDF generated: {output_pdf}")
except Exception as e:
print("Conversion failed:", str(e))
But running it gives an error about missing fonts:
Conversion failed: Pandoc died with exitcode "43" during conversion: Error producing PDF.
! Package fontspec Error:
(fontspec) The font "PingFang SC" cannot be found; this may
(fontspec) be but usually is not a fontspec bug. Either there
(fontspec) is a typo in the font name/file, the font is not
(fontspec) installed (correctly), or there is a bug in the
(fontspec) underlying font loading engine (XeTeX/luaotfload).
For immediate help type H <return>.
...
l.28 \setCJKmainfont[]{PingFang SC}
That’s strange, how could my Mac not have PingFang?
I searched using the following command in the terminal, and indeed, PingFang is not found:
fc-list | grep -i "PingFang"
Then I used this command to check fonts that support Chinese, still no PingFang:
fc-list :lang=zh
/System/Library/Fonts/STHeiti Medium.ttc: Heiti SC,黑體\-簡,黒体\-簡,Heiti\-간체,黑体\-简:style=Medium,中黑,Halbfett,Normaali,Moyen,Medio,ミディアム,중간체,Médio,Средний,Normal,中等,Media
/System/Library/Fonts/STHeiti Light.ttc: Heiti TC,黑體\-繁,黒体\-繁,Heiti\-번체,黑体\-繁:style=Light,細體,Mager,Fein,Ohut,Fin,Leggero,ライト,가는체,Licht,Tynn,Leve,Светлый,细体,Fina
/System/Library/Fonts/Supplemental/Songti.ttc: Songti SC,宋體\-簡,宋体\-简:style=Light,細體,细体
/System/Library/Fonts/Supplemental/Songti.ttc: Songti TC,宋體\-繁,宋体\-繁:style=Regular,標準體,常规体
/System/Library/Fonts/Supplemental/Songti.ttc: Songti SC,宋體\-簡,宋体\-简:style=Regular,標準體,常规体
/System/Library/Fonts/Supplemental/Songti.ttc: Songti TC,宋體\-繁,宋体\-繁:style=Light,細體,细体
/System/Library/Fonts/Supplemental/Songti.ttc: Songti SC,宋體\-簡,宋体\-简:style=Black,黑體,黑体
/Library/Fonts/Arial Unicode.ttf: Arial Unicode MS:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/System/Library/Fonts/Hiragino Sans GB.ttc: .Hiragino Sans GB Interface:style=W6
/System/Library/Fonts/Hiragino Sans GB.ttc: .Hiragino Sans GB Interface:style=W3
/System/Library/Fonts/Supplemental/Arial Unicode.ttf: Arial Unicode MS:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/System/Library/Fonts/STHeiti Medium.ttc: Heiti TC,黑體\-繁,黒体\-繁,Heiti\-번체,黑体\-繁:style=Medium,中黑,Halbfett,Normaali,Moyen,Medio,ミディアム,중간체,Médio,Средний,Normal,中等,Media
/System/Library/Fonts/Supplemental/Songti.ttc: Songti SC,宋體\-簡,宋体\-简:style=Bold,粗體,粗体
/System/Library/Fonts/STHeiti Light.ttc: Heiti SC,黑體\-簡,黒体\-簡,Heiti\-간체,黑体\-简:style=Light,細體,Mager,Fein,Ohut,Fin,Leggero,ライト,가는체,Licht,Tynn,Leve,Светлый,细体,Fina
/System/Library/Fonts/Supplemental/Songti.ttc: Songti TC,宋體\-繁,宋体\-繁:style=Bold,粗體,粗体
/System/Library/Fonts/Hiragino Sans GB.ttc: Hiragino Sans GB,冬青黑體簡體中文,冬青黑体简体中文,Hiragino Sans GB W6,冬青黑體簡體中文 W6,冬青黑体简体中文 W6:style=W6,Bold
/System/Library/Fonts/Supplemental/Songti.ttc: STSong:style=Regular,標準體,Ordinær,Normal,Normaali,Regolare,レギュラー,일반체,Regulier,Обычный,常规体
/System/Library/Fonts/Hiragino Sans GB.ttc: Hiragino Sans GB,冬青黑體簡體中文,冬青黑体简体中文,Hiragino Sans GB W3,冬青黑體簡體中文 W3,冬青黑体简体中文 W3:style=W3,Regular
/System/Library/Fonts/LastResort.otf: .LastResort:style=Regular
However, by running this command, PingFang font can be clearly seen:
system_profiler SPFontsDataType > output.txt
Here is a partial excerpt:
PingFangUI.ttc:
Kind: TrueType
Valid: Yes
Enabled: Yes
Location: /System/Library/PrivateFrameworks/FontServices.framework/Resources/Reserved/PingFangUI.ttc
Typefaces:
PingFangSC-Semibold:
Full Name: PingFang SC Semibold
Family: PingFang SC
Style: Semibold
Version: 21.0d7e1
Unique Name: 蘋方-簡; 21.0d7e1; 2025-07-22
Copyright: © 2024-2025 Apple Inc. All rights reserved.
Outline: Yes
Valid: Yes
Enabled: Yes
Duplicate: No
Copy Protected: No
Embeddable: Yes
PingFangHK-Semibold:
Full Name: PingFang HK Semibold
Family: PingFang HK
Style: Semibold
Version: 21.0d7e1
Unique Name: 蘋方-港; 21.0d7e1; 2025-07-22
Copyright: © 2024-2025 Apple Inc. All rights reserved.
Outline: Yes
Valid: Yes
Enabled: Yes
Duplicate: No
Copy Protected: No
Embeddable: Yes
After discussion with AI battle, it turns out that * PingFang does exist in macOS, but it is located in PrivateFrameworks/FontServices, it’s a private system font, so fc-list won’t show it, nor is it in the regular Fontconfig index.
So what can I do if I want to use it?
Forget it, download a PingFang ttf font instead, you cannot use /System/Library/PrivateFrameworks/FontServices.framework/Resources/Reserved/PingFangUI.ttc, it won’t be recognized.
Copy and paste the new font into ~/Library/Fonts, then it can be used.