Asciidoc 的 Idea 插件版本输出 pdf 乱码

最近正在研究 Asciidoc 这款生成文档的工具,其代码风格和 markdown 类似,最大的区别就是其有配套的默认工具来识别相应的结构目录,并根据目录结构来生成 html, doc, pdf。

语法简单,工具操作简单,可以解决现在 markdown 碰到的部分问题,且还支持 Antora 这样的更加灵活的工具,使得文档或者技术书籍的写作变得更加的简单。

今天这篇文章主要来解决使用过程中碰到的问题:非拉丁语系文字在生成 pdf 时,会变成乱码。

解决方案

官方在文档中其实已经给出了问题的原因和解决方案,原因就是因为字体兼容性,在导出时使用的默认字体不兼容非拉丁语系文字。

那么解决方案也很明确了,更换导出时的字体,使用兼容本地文字的字体,保证导出时显示正常。

下面是官方文档和 Idea 插件文档中的解释

Idea 插件文档中提到了一个文件.asciidoctorconfig, 这个文件是给 asciidoctor 使用,也就是导出时使用的。

并且也给出了下载地址 KaiGenGothicCN 字体下载

下载完成后形成如下目录

1
2
3
4
5
6
7
8
9
10
11
12
13
├──document
│ ├──config
│ │ ├──fonts
│ │ │ ├──KaiGenGothicCN-Bold.ttf
│ │ │ ├──KaiGenGothicCN-ExtraLight.ttf
│ │ │ ├──KaiGenGothicCN-Heavy.ttf
│ │ │ ├──KaiGenGothicCN-Light.ttf
│ │ │ ├──KaiGenGothicCN-Medium.ttf
│ │ │ ├──KaiGenGothicCN-Normal.ttf
│ │ │ ├──KaiGenGothicCN-Regular.ttf
│ │ ├──themes
│ │ │ ├──zh_CN-theme.yml
--.asciidoctorconfig

.asciidoctorconfig 类容如下

1
2
3
:pdf-fontsdir: {asciidoctorconfigdir}/config/fonts
:pdf-themesdir: {asciidoctorconfigdir}/config/themes
:pdf-theme: zh_CN

zh_CN-theme.yml 类容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# default theme at https://github.com/asciidoctor/asciidoctor-pdf/blob/master/data/themes/default-theme.yml
#https://intellij-asciidoc-plugin.ahus1.de/docs/users-guide/features/advanced/pdf-non-latin-languages
extends: default
font:
fallbacks:
- kaigen-gothic-cn
catalog:
# These are the KaiGen Gothic CN fonts, download them from
# https://github.com/minjiex/kaigen-gothic/tree/master/dist/CN
kaigen-gothic-cn:
normal: KaiGenGothicCN-Regular.ttf
bold: KaiGenGothicCN-Bold.ttf
italic: KaiGenGothicCN-Regular.ttf
bold_italic: KaiGenGothicCN-Bold.ttf
base:
font_family: kaigen-gothic-cn