本篇文章为你整理了使用docx4j实现docx转pdf(解决linux环境下中文乱码问题)(linux word转pdf中文乱码)的详细内容,包含有word转pdf java linux linux word转pdf中文乱码 linux doc转unix linux aspose word转pdf乱码 使用docx4j实现docx转pdf(解决linux环境下中文乱码问题),希望能帮助你了解 使用docx4j实现docx转pdf(解决linux环境下中文乱码问题)。
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.org.apache.poi.util.IOUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;
@Slf4j
public class WordUtils {
public static void main(String[] args) throws Exception {
WordUtils.convertDocxToPdf("D:/test/test.docx","D:/test/test.pdf");
* docx文档转换为PDF
* @param body 文档
* @param response 响应给前端
* @return pdf 输出流
* @throws Exception 可能为Docx4JException, FileNotFoundException, IOException等
public static void convertDocxToPdf(byte[] body , HttpServletResponse response) throws Exception {
response.setContentType("application/pdf");
File docxFile = FileUtil.byteToFile(body, UUID.randomUUID().toString() + ".docx");
try {
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(docxFile);
setFontMapper(mlPackage);
Docx4J.toPDF(mlPackage, response.getOutputStream());
}catch (Exception e){
e.printStackTrace();
log.error("docx文档转换为PDF失败");
FileUtil.deleteTempFile(docxFile);
* @param pdfPath PDF文档存储路径
* @throws Exception 可能为Docx4JException, FileNotFoundException, IOException等
public static void convertDocxToPdf(String docxPath, String pdfPath) throws Exception {
FileOutputStream fileOutputStream = null;
try {
File file = new File(docxPath);
fileOutputStream = new FileOutputStream(new File(pdfPath));
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(file);
setFontMapper(mlPackage);
Docx4J.toPDF(mlPackage, new FileOutputStream(new File(pdfPath)));
}catch (Exception e){
e.printStackTrace();
log.error("docx文档转换为PDF失败");
}finally {
IOUtils.closeQuietly(fileOutputStream);
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);
3.在linux环境中安装windows字体
(1)在linux(CentOS7)环境中新建文件夹/usr/share/fonts/win_font
(2)将C:\Windows\Fonts(win10)中的字体文件拷贝到/usr/share/fonts/win_font文件夹中
(3)加载字体文件
mkfontscale //字体扩展
mkfontdir //新增字体目录
fc-cache -fv //刷新缓存
(4)查看字体安装情况
fc-list :lang=zh
4.打包部署并启动程序即可查看最终效果,亲测通过,希望对你有所帮助。
如有疑问,可留言给我,看到后会回复。
以上就是使用docx4j实现docx转pdf(解决linux环境下中文乱码问题)(linux word转pdf中文乱码)的详细内容,想要了解更多 使用docx4j实现docx转pdf(解决linux环境下中文乱码问题)的内容,请持续关注盛行IT软件开发工作室。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。