本文参考来自 https://www.cnblogs.com/mituxiaogaoyang/p/15243103.html
支持windows和linux环境,linux需要安装中文避免乱码问题
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-Internal</artifactId>
<version>8.3.1</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.3.1</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.3.1</version>
</dependency>
package com.xxx.utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.docx4j.Docx4J;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFont;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.org.apache.poi.util.IOUtils;
import org.docx4j.fonts.PhysicalFonts;
import java.io.File;
import java.io.FileOutputStream;
public class Word2PdfUtil {
private static final Log log = LogFactory.getLog(Word2PdfUtil.class);
public static void main(String[] args) throws Exception {
String inputWord = "D:\\var\\aa.docx";
String outputFile = "D:\\var\\bb.pdf";
boolean isSuccess = Word2PdfUtil.convertDoc2Pdf(inputWord, outputFile);
System.out.println("convertDoc2Pdf:============" + isSuccess);
}
/**
* docx文档转换为PDF
*
* @param docPath WORD文档存储路径
* @param pdfPath PDF文档存储路径
* @throws Exception 可能为Docx4JException, FileNotFoundException, IOException等
*/
public static boolean convertDoc2Pdf(String docPath, String pdfPath) throws Exception {
FileOutputStream fileOutputStream = null;
try {
File file = new File(docPath);
fileOutputStream = new FileOutputStream(new File(pdfPath));
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(file);
setFontMapper(mlPackage);
Docx4J.toPDF(mlPackage, new FileOutputStream(new File(pdfPath)));
log.info("WORD文档转换为PDF成功===========");
return true;
} catch (Exception e) {
e.printStackTrace();
log.error("WORD文档转换为PDF失败===========");
} finally {
IOUtils.closeQuietly(fileOutputStream);
}
return false;
}
private static void setFontMapper(WordprocessingMLPackage mlPackage) throws Exception {
Mapper fontMapper = new IdentityPlusMapper();
// 加载字体文件(解决linux环境下无中文字体问题)
if (PhysicalFonts.get("SimSun") == null) {
System.out.println("加载本地SimSun字体库");
// PhysicalFonts.addPhysicalFonts("SimSun", WordUtils.class.getResource("/fonts/SIMSUN.TTC"));
}
fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
fontMapper.put("等线", PhysicalFonts.get("SimSun"));
fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
fontMapper.put("新細明體", PhysicalFonts.get("SimSun"));
// 解决宋体(正文)和宋体(标题)的乱码问题
PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));
PhysicalFonts.put("新細明體", PhysicalFonts.get("SimSun"));
// 宋体&新宋体
PhysicalFont simsunFont = PhysicalFonts.get("SimSun");
fontMapper.put("SimSun", simsunFont);
// 设置字体
mlPackage.setFontMapper(fontMapper);
}
}
安装中文字体
从windows的C:\Windows\Fonts拷贝字体到linux服务器fonts目录下,新建目录chinese
cd ../usr/share/fonts
在fonts目录下执行以下命令
字体扩展
mkfontscale
新增字体目录
mkfontdir
刷新缓存
fc-cache -fv