Linux 下安装Windows字体
项目部署到Linux平台之后经常会遇缺少字体的问题,例如在Ubuntu Server上预览或者打印jasper报表时异常:
net.sf.jasperreports.engine.util.JRFontNotFoundException: Font 'Arial' is not available to the JVM. See the Javadoc for more details.
at net.sf.jasperreports.engine.util.JRFontUtil.checkAwtFont(JRFontUtil.java:321)
at net.sf.jasperreports.engine.util.JRStyledText.getAwtAttributedString(JRStyledText.java:226)
而开发人员在本地测试缺没有问题,这是因为这些字体在windows安装的时候就有了(mac嘛我没用过,嘿嘿,不知道是否默认安装了windows的字体)
解决办法(以Ubuntu 为例)
-
使用包管理器安装
sudo apt update && sudo apt install ttf-mscorefonts-installer
安装过程中会弹出提示,一路默认选项同意即可
安装完成记得刷新字体缓存
sudo fc-cache -f -v
-
将Windows下的字体拷贝到Linux下
将Windows字体(选中所有字体压缩)压缩成Fonts.zip包,并上传到Linux中
在Linux中新建目录
sudo mkdir -p /usr/share/fonts/WindowsFonts
将Windows字体压缩包中的内容解压到刚刚创建的目录中
sudo unzip ~/Fonts.zip -d /usr/share/fonts/WindowsFonts
然后修改一下权限
sudo chmod 755 /usr/share/fonts/WindowsFonts/*
最后刷新一下字体
sudo fc-cache
再次预览报表,就可以正常显示了~