参考这里写了一段使用pandoc和latex的代码,这样markdown转换出来的pdf格式最佳
import pypandoc
def md_to_pdf_pandoc(input_md, output_pdf):
try:
# 使用自定义 LaTeX 模板或内联样式
output = pypandoc.convert_file(
input_md, 'pdf',
outputfile=output_pdf,
extra_args=[
'--pdf-engine=xelatex',
'-V', 'documentclass=ctexart',
'-V', 'CJKmainfont=PingFang SC', # 中文主字体
'-V', 'mainfont=XITS', # 主字体设为 XITS
'-V', 'mathfont=XITS Math', # 数学字体设为 XITS Math
'-V', 'monofont=DejaVu Sans Mono', # 等宽字体(代码块)
# '-V', 'geometry:margin=1in', # 设置页边距
# '--template=eisvogel', # 使用自定义模板
# '--listings',
# '--highlight-style=pygments' # 使用语法高亮风格(带背景色)
]
)
print(f"PDF 已生成:{output_pdf}")
except Exception as e:
print("转换失败:", str(e))
但是运行时会报错缺少字体
转换失败: 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}
那就很奇怪了,我Mac怎么会缺苹方呢?
在终端输入如下命令搜了一下,还真没有苹方
fc-list | grep -i "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
但是通过这个命令可以很明确看到有苹方字体
system_profiler SPFontsDataType > output.txt
这里只节选部分
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
和ai battle以后才知道, * 苹方在 macOS 系统里确实存在,但位于 PrivateFrameworks/FontServices,属于 私有系统字体,所以 fc-list 不会显示,也不在普通 Fontconfig 索引里。
那我想用怎么办?
凉拌,再下载一份苹方tff字体,不能用/System/Library/PrivateFrameworks/FontServices.framework/Resources/Reserved/PingFangUI.ttc,识别不了
把新的字体复制黏贴到~/Library/Fonts,就能用了