我们用的java 版本 aspose,用于将word 转换为pdf,windows 下中文正常,linux 中文显示为方块,是因为linux 下没有中文字体或者 docker 容器中没有中文字体导致的
乱码解决方法:
1. linux 配置中文字体(适用于宿主机直接跑java 程序的方式)
#CentOS字体所在目录为/usr/share/fonts,在该目录下新建文件夹chinese, su root mkdir /usr/share/fonts/chinese cd /usr/share/fonts/chinese #安装字体 yum install mkfontscale yum install fontconfig mkfontscale mkfontdir fc-cache -fv #重启系统或者source /etc/profile。
2. docker 容器映射中文字体(前提是主机存在中文字体)
# 直接将字体映射到容器 docker run -d --restart=always -v /etc/localtime:/etc/localtime -v /usr/share/fonts:/usr/share/fonts --name test --net=host test
表格被压缩问题处理
首先需要用aspose 读取出word 文档的内容,哪个表格出问题就解决哪个表格的适应就行
public static void doc2pdf(String wordPath, String pdfPath) throws Exception {
/*
* // 验证License 若不验证则转化出的pdf文档会有水印产生 if
*/
AsposeLicenseHelper.getWordsLicense();
// 转换开始前时间
long old = System.currentTimeMillis();
// 新建的PDF文件路径
File file = new File(pdfPath);
FileOutputStream os = new FileOutputStream(file);
// 要转换的word文档的路径
Document doc = new Document(wordPath);
// 设置表格自适应,根据内容自适应,否则表格多了转pdf 会挤压在一起 ,简单示例,取word 中的第3个表格
Node node=doc.getChild(NodeType.TABLE,2,true);
Table table=(Table) node;
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);// 设置表格根据内容自适应
doc.updateFields();
// 相互转换
doc.save(os, com.aspose.words.SaveFormat.PDF);
// 转换结束后时间
long now = System.currentTimeMillis();
os.close();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
}