word转pdf

  1. 查阅各种资料,发现poi处理word,之后使用poi转为html,(不太容易,兼容doc和docx);然后再用itext将html转为pdf(中文不现实问题,要吐了)。看到有的博主用了这种思路,但是由于项目中原有的poi版本固定了,再操作word的时候,去兼容docx,旧版本的poi不支持等问题太多了,搁置。
  2. 另外,freemarker 也可以操作word,openoffice也是操作wps的工具,暂时未使用过。
  3. 简化思路,使用poi动态生成word,使用docx4j将word转化为pdf,发现使用docx4j将word转为pdf,操作简单,但耗时。

使用jar包:

         <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>6.0.0</version>
        </dependency>

代码:

 public static void main(String[] args) {
        convertDocx2Pdf("D:\WorkSpace\ftz_work\work-Director\" + "test01.docx");
    }

    /**
     * word(docx)转pdf
     * @param wordPath  docx文件路径
     * @return
     */
    public static String convertDocx2Pdf(String wordPath) {
        OutputStream os = null;
        InputStream is = null;
        try {
            is = new FileInputStream(new File(wordPath));
            WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(is);
            Mapper fontMapper = new IdentityPlusMapper();
            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("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"));

            mlPackage.setFontMapper(fontMapper);

            //输出pdf文件路径和名称
            String fileName = "pdfNoMark_" + System.currentTimeMillis() + ".pdf";
            //String pdfNoMarkPath = System.getProperty("java.io.tmpdir").replaceAll(separator + "$", "") + separator + fileName;
            String pdfNoMarkPath = "D:\WorkSpace\ftz_work\work-Director\PDFTest\" + fileName;
            os = new java.io.FileOutputStream(pdfNoMarkPath);

            //docx4j  docx转pdf
            FOSettings foSettings = Docx4J.createFOSettings();
            foSettings.setWmlPackage(mlPackage);
            Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

            is.close();//关闭输入流
            os.close();//关闭输出流

        } catch (Exception e) {
            e.printStackTrace();
            try {
                if(is != null){
                    is.close();
                }
                if(os != null){
                    os.close();
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        return "";
    }

注意事项:

1.fontMapper 中放入的字体,需要在对应的字体库中存在,才可以使用。如果word中字体为宋体,那么fontMapper中需要放入宋体,并且系统的字体库中必须存在。
2.windows下字体库在C盘下,linux系统下可能没有这些字体,需要找到docx4j读取字体库的位置(日志查看),然后手动放入。
原文地址:https://www.cnblogs.com/xuebuhui/p/11950942.html