Centos7 下 Java 生成图片水印时中文乱码,原因是没有安装中文字体。
以下是基于 Centos7 手动安装中文字体的详细步骤。当测试或者生产环境服务器比较多的时候,建议使用自动化运维工具。
1# 安装字体库
2$ yum -y install fontconfig
3
4# 查看是否有中文字体
5$ fc-list :lang=zh
6
7# 创建中文字体目录
8$ mkdir /usr/share/fonts/chinese
9
10# 在 windows 的 C:\Windows\Fonts 目录下找到相应的字体 copy 到 chinese 目录下,这里以 宋体 为例
11$ scp simsun.ttc simsunb.ttf root@xxxxx:/usr/share/fonts/chinese
12
13# 查看是否有中文字体
14$ fc-list :lang=zh
15/usr/share/fonts/chinese/simsun.ttc: SimSun,宋体:style=Regular,常规
16/usr/share/fonts/chinese/simsun.ttc: NSimSun,新宋体:style=Regular,常规
通常测试或者生产环境服务器比较多,下面记录如何使用 Ansbile 来批量安装中文字体。
1# ansbile playbook 执行
2$ ansible-playbook fonts.yml
3
4# 验证所有服务器是否生效
5$ ansible all -m shell -a "fc-list :lang=zh"
6sever01 | SUCCESS | rc=0 >>
7/usr/share/fonts/chinese/simsun.ttc: SimSun,宋体:style=Regular,常规
8/usr/share/fonts/chinese/simsun.ttc: NSimSun,新宋体:style=Regular,常规
9sever02 | SUCCESS | rc=0 >>
10/usr/share/fonts/chinese/simsun.ttc: SimSun,宋体:style=Regular,常规
11/usr/share/fonts/chinese/simsun.ttc: NSimSun,新宋体:style=Regular,常规
12......
fonts.yml 内容:
1---
2- name: Install Chinese Fonts.
3 hosts: all
4 remote_user: root
5 become: yes
6 become_method: sudo
7 become_user: root
8 roles:
9 - fonts
ansible playbook 目录结构(删除了无用目录):
1$ tree roles/fonts
2roles/fonts
3├── files
4│ ├── simsun.ttc
5│ └── simsunb.ttf
6└── tasks
7 └── main.yml
8
92 directories, 3 files
task/main.yml 内容:
1---
2# tasks file for fonts
3
4- name: install fontconfig.
5 yum:
6 name: "{{ item }}"
7 state: installed
8 with_items:
9 - fontconfig
10 ignore_errors: true
11
12- name: mkdir /usr/share/fonts/chinese.
13 file:
14 path: /usr/share/fonts/chinese
15 state: directory
16 mode: 0755
17
18- name: Copy fonts to agent.
19 copy:
20 src: "{{ item }}"
21 dest: /usr/share/fonts/chinese
22 with_items:
23 - simsun.ttc
24 - simsunb.ttf