freemark模板导出word-04-word转pdf

前几个章节写了基础导出word的一些问题以及注意的坑,本章节记录一下转化为pdf相关内容;
思路还是xml先转化为word,然后word转化为pdf。需要用到jar包(aspose-words-15.8.0-jdk16.jar)。

 

 

核心代码如下:getData05就是获取业务数据,自己按照项目实际情况来。

private static void test005() throws Exception{
        String word_xml_temp_01="D:\\data\\exportWord1\\tmp205.xml";
        String LICENSE_XML="D:\\data\\exportWord1\\license.xml";
        Map<String,Object> resultMap=getData05();
        String currentTime = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String filePath = new StringBuilder(currentTime).append(".doc").toString();
        FileUtil.mkdir(wordDir);
        String docPath=new StringBuilder(wordDir).append("/").append(filePath).toString();
        File outFile = new File(docPath);
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_20);
        configuration.setDefaultEncoding("utf-8");
        configuration.setDirectoryForTemplateLoading(new File(wordDir));

        InputStream inputStream = new FileInputStream(new File(word_xml_temp_01));
        String fileXmlPath = new StringBuilder(currentTime).append(".xml").toString();
        File xmlFile = new File(new StringBuilder(wordDir).append("/").append(fileXmlPath).toString());
        FileUtils.copyInputStreamToFile(inputStream, xmlFile);

        Template t = configuration.getTemplate(fileXmlPath, "utf-8");
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 102400);
        t.process(resultMap, out);
        out.flush();
        out.close();


        InputStream lis=new FileInputStream(new File(LICENSE_XML));
        License aposeLic = new License();
        aposeLic.setLicense(lis);

        String pdfPath=new StringBuilder(wordDir).append("/").append(currentTime).append(".pdf").toString();
        File pdfFile=new File(pdfPath);


        FileOutputStream pdfOs = new FileOutputStream(pdfFile);
        Document doc = new Document(docPath); // Address是将要被转化的word文档
        doc.save(pdfOs, SaveFormat.PDF);
        
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值