orzih / mkdocs-with-pdf

Generate a single PDF file from MkDocs repository.
MIT License
332 stars 77 forks source link

中文乱码 Chinese is garbled after the generated PDF #55

Closed you-can-change closed 3 years ago

you-can-change commented 3 years ago

Question

My markdown doc contains Chinese, Chinese is garbled after the generated PDF. What should I do? What are the possible problems? I'm a new guy, and I don't have any idea.

Environment

[root@centos7 ~]# python -V
Python 3.7.5
[root@centos7 ~]# pip list
Package                    Version  
-------------------------- ---------
beautifulsoup4             4.9.3    
cairocffi                  1.2.0    
CairoSVG                   2.5.2    
cffi                       1.14.5   
click                      7.1.2    
cssselect2                 0.4.1    
defusedxml                 0.7.1    
future                     0.18.2   
html5lib                   1.1      
importlib-metadata         3.7.3    
Jinja2                     2.11.3   
joblib                     1.0.1    
libsass                    0.20.1   
livereload                 2.6.3    
lunr                       0.5.8    
Markdown                   3.3.4    
MarkupSafe                 1.1.1    
mkdocs                     1.1.2    
mkdocs-material            7.0.6    
mkdocs-material-extensions 1.0.1    
mkdocs-pdf-export-plugin   0.5.8    
mkdocs-with-pdf            0.8.3    
nltk                       3.5      
Pillow                     8.1.2    
pip                        19.2.3   
pycparser                  2.20     
Pygments                   2.8.1    
pymarkdown                 0.1.4    
pymdown-extensions         8.1.1    
Pyphen                     0.10.0   
PyYAML                     5.4.1    
regex                      2021.3.17
setuptools                 41.2.0   
six                        1.15.0   
soupsieve                  2.2.1    
tinycss2                   1.1.0    
toolz                      0.11.1   
tornado                    6.1      
tqdm                       4.59.0   
typing-extensions          3.7.4.3  
WeasyPrint                 52.4     
webencodings               0.5.1    
zipp                       3.4.1    

Repro steps

1. Create a new project.

[root@centos7 ~]# mkdocs new test
[root@centos7 ~]# cd test/

2. Using the plugin.

[root@centos7 test]# cat > mkdocs.yml <<EOF
site_name: 我的测试
dev_addr: 0.0.0.0:8000
plugins:
  - with-pdf:
EOF

3. Rewrite index.md with Chinese

[root@centos7 test]# cat > docs/index.md <<"EOF"
完整文档请访问:[mkdocs.org](https://www.mkdocs.org).

* `mkdocs new [dir-name]` - 创建一个新项目
* `mkdocs serve` - 启动实时重新加载文档服务器
* `mkdocs build` - 建立文档站点
* `mkdocs -h` - 打印这个帮助信息

    mkdocs.yml    # 配置文件、
    docs/
        index.md  # 文档根页面
        ...       # 其他markdown页面, 镜像或其他问题
EOF

4. mkdocs build

[root@centos7 test]# mkdocs build
WARNING -  Config value: 'dev_addr'. Warning: The use of the IP address '0.0.0.0' suggests a production environment or the use of a proxy to connect to the MkDocs server. However, the MkDocs' server is intended for local development purposes only. Please use a third party production-ready server instead. 
INFO    -  Cleaning site directory 
INFO    -  Building documentation to directory: /root/test/site 
INFO    -  Number headings up to level 3. 
INFO    -  Generate a table of contents up to heading level 2. 
INFO    -  Generate a cover page with "default_cover.html.j2". 
INFO    -  Converting <img> alignment(workaround). 
INFO    -  Rendering for PDF. 
INFO    -  Output a PDF to "/root/test/site/pdf/document.pdf". 
INFO    -  Converting 1 articles to PDF took 2.2s 
INFO    -  Documentation built in 2.47 seconds 

5. Download and open the pdf

image

orzih commented 3 years ago

Hi @you-can-change.

I think you need to setup the Chinese environment and fonts(required only when generating PDF).

I'm sorry, I don't know how to do it, so the search keywords are:

you-can-change commented 3 years ago

Thanks a lots,I follow your step, It work now :heart:.

# install the fonts
[root@centos7 test]#  yum groupinstall Fonts

# check fonts
[root@centos7 test]#   fc-match serif:lang=zh
DejaVuSerif.ttf: "DejaVu Serif" "Book"
[root@centos7 test]#   fc-match sans-serif:lang=zh
DejaVuSans.ttf: "DejaVu Sans" "Book"

# set locale
[root@centos7 test]#  localectl set-locale LANG=zh_CN.UTF-8

# show locale settings
[root@centos7 test]# localectl status
   System Locale: LANG=zh_CN.UTF-8
       VC Keymap: us
      X11 Layout: us

# then build
[root@centos7 test]#  mkdocs build

But not work well :sob:, The pdf doesn't have table of contents, and some content didn't show as expected.

It seems to be caused by not being able to handle Chinese normally, Shoud I close the issue?

image

orzih commented 3 years ago
[root@centos7 test]#   fc-match serif:lang=zh
DejaVuSerif.ttf: "DejaVu Serif" "Book"
[root@centos7 test]#   fc-match sans-serif:lang=zh
DejaVuSans.ttf: "DejaVu Sans" "Book"

Deja Vu is English font(not contains Chinese glyphs) - maybe error fallback.

Please try to install other Chinese font such as "Google Noto CJK".

(I can't read Chinese)

you-can-change commented 3 years ago

I'm sorry for wasting your time, it was my fault:sweat_smile::sweat_smile::sweat_smile:. When I translated the default index.md(Repro steps->3. Rewrite index.md with Chinese), I treated the sentences beginning with the # sign as comments and deleted them. So there is no table of contents.

I added the missing directory structure, now it's work very well.Thank you for your support again.:heart::heart::heart: image