当前位置: 首页 > 工具软件 > cache4j > 使用案例 >

使用docx4j 实现word转pdf(linux乱码处理)

凌征
2023-12-01

使用docx4j 实现word转pdf

1.引入maven依赖
		<dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j</artifactId>
            <version>6.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-export-fo</artifactId>
            <version>8.1.1</version>
        </dependency>
2.代码实现
import lombok.extern.slf4j.Slf4j;
import org.docx4j.Docx4J;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @description: word文档操作utils
 * @author: wzg
 * @create: 2021-12-16
 **/
@Slf4j
public class WordToPdf {


    public static void main(String[] args) throws Exception {
        WordToPdf.convertDocxToPdf("/data/doc/2021-12-17/word1639704593928.doc","/data/pdf/2021-12-17/pdf1639704593928.pdf");
    }


    /**
     * doc文档转换为PDF
     *
     * @param docxPath word文档路径
     * @param pdfPath PDF文档存储路径
     * @throws Exception 
     */
    public static void convertDocxToPdf(String docxPath, String pdfPath) throws Exception {

        FileOutputStream fileOutputStream = null;
        try {
            File docFile = new File(docxPath);
            File pdfFile = new File(pdfPath);
            WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(docFile);
            setFontMapper(mlPackage);
            fileOutputStream = new FileOutputStream(pdfFile);
            Docx4J.toPDF(mlPackage, fileOutputStream);
        }catch (Exception e){
            e.printStackTrace();
            log.error("doc文档转换为PDF失败");
        }finally {
            fileOutputStream.flush();
            fileOutputStream.close();
        }
    }


    /**
     * 字体设置
     * @param mlPackage
     * @throws Exception
     */
    private static void setFontMapper(WordprocessingMLPackage mlPackage) throws Exception {
        Mapper fontMapper = new IdentityPlusMapper();
        //fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
        fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
        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("SimSun"));
        fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
        fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
        //fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
        //fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
        //fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
        fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
        //fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
        //fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
        //fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
        //fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
        //解决宋体(正文)和宋体(标题)等的乱码问题
        PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));
        PhysicalFonts.put("新細明體", PhysicalFonts.get("SimSun"));


        mlPackage.setFontMapper(fontMapper);
    }


}
3.docx4j部署linux乱码处理

​ 3.1 复制windows的C:\Windows\Fonts\下的所有字体库,放在桌面的fonts文件夹里。

​ 3.2 使用xshell 将桌面上的fonts文件夹里的字体库,全部放入 /usr/share/fonts/chinese 文件夹下,如果文件夹不存在,手动创建。

​ 3.3 给文件夹下的文件授权

chmod 775 /usr/share/fonts/chinese

3.4 建立字体缓存库

cd /usr/share/fonts/chinese
 
sudo mkfontscale
 
sudo mkfontdir
 
sudo fc-cache -fv
 类似资料: