本文參考來自 https://www.cnblogs.com/mituxiaogaoyang/p/15243103.html
支持windows和linux環(huán)境,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文檔轉(zhuǎn)換為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文檔轉(zhuǎn)換為PDF成功===========");
return true;
} catch (Exception e) {
e.printStackTrace();
log.error("WORD文檔轉(zhuǎn)換為PDF失敗===========");
} finally {
IOUtils.closeQuietly(fileOutputStream);
}
return false;
}
private static void setFontMapper(WordprocessingMLPackage mlPackage) throws Exception {
Mapper fontMapper = new IdentityPlusMapper();
// 加載字體文件(解決linux環(huán)境下無中文字體問題)
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目錄下執(zhí)行以下命令
字體擴展
mkfontscale
新增字體目錄
mkfontdir
刷新緩存
fc-cache -fv
font字體截圖

image.png