前言
在使用aspose导出pdf文件,会遇到windows环境导出pdf文档正常展示,部署到linux环境就出现导出乱码,其实就是linux服务器上的字体不支持的问题,我们只需要在linux服务器上配置对应字体就ok了。
解决问题
在linux上把字体库上传到linux服务上,一般放在/usr/share/fonts目录下面,配置权限
sudo chmod 755 /usr/share/fonts/*
使用fc-cache命令刷新缓存,然后导出pdf文档中,判断当前系统,如果是Linux系统,就读取/usr/share/fonts目录下指定的字体库,具体代码如下
public void exportPDF(String fileName, String resultHtml,
String path) throws Exception {
Document document = new Document(new ByteArrayInputStream(resultHtml.getBytes("utf-8")));
if (!"".equals(path)) {
File fileDir = new File(path);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
}
document.getViewOptions().setViewType(ViewType.PAGE_LAYOUT);
document.getViewOptions().setZoomPercent(100);
DocumentBuilder builder = new DocumentBuilder(document);
builder.getParagraphFormat().setLineSpacing(18);
OsInfo osInfo = SystemUtil.getOsInfo();
if (osInfo.isLinux()) {
FontSettings fontSettings = new FontSettings();
fontSettings.setFontsFolder(Config.FONTS_FOLDERS, true);
document.setFontSettings(fontSettings);
}
document.save(path + fileName + ".pdf");
}