登录
    Technology changes quickly but people's minds change slowly.

Aspose word 转pdf 中文乱码以及表格挤在一块的解决方法

技术宅 破玉 3696次浏览 0个评论

我们用的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) + "秒");
	}

华裳绕指柔, 版权所有丨如未注明 , 均为原创|转载请注明Aspose word 转pdf 中文乱码以及表格挤在一块的解决方法
喜欢 (10)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址