aspose 使用ftl模板生成word和pdf

1 先找到word模板,用${},替换变量,保存,然后另存为xml,最后把xml后缀改成ftl。

如下图:

word 模板文件

ftl模板文件如下:

2 代码生成

下面函数将ftl填充数据,并生成word和pdf

java 复制代码
    /**
     * 
     * @param dataMap  模板需要填充的数据
     * @param templateName  模板名称
     * @param format 生成的文件格式
     * @return 生成的文件路径
     */
    public static String ftl2word2pdf(Map<String, Object> dataMap, String templateName, int format) {
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        FileOutputStream os = null;
        try {

            /**
             * 模板以及生成word pdf 存放路径
             */
            String path="C:\\Users\\Administrator\\Desktop\\asposeTest\\";
            /**
             * 先根据ftl模板生成word
             */
            File directory = new File(path);
            String absolutePath = directory.getAbsolutePath();

            Configuration configuration = new Configuration(new Version("2.3.0"));
            configuration.setDefaultEncoding("utf-8");
            configuration.setDirectoryForTemplateLoading(new File(absolutePath));
            //.ftl配置文件所在路径
            Template template = configuration.getTemplate(templateName, "utf-8");
            /**
             * 生成临时word 文件
             */
            //输出文档路径及名称
            //临时文件的文件名
            String tempFileName= UUID.randomUUID().toString();
            String tempWordName=path+File.separator+ tempFileName+".docx";
            File outFile = new File(tempWordName);
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),
                    "utf-8"), 10240);
            template.process(dataMap, out);
            out.close();
            System.out.println("Execute Word:"+tempWordName);
            String tempPdfName=path+File.separator+tempFileName+".pdf";
            File file = new File(tempPdfName); // 新建一个空白pdf文档
            os = new FileOutputStream(file);
            Document doc = new Document(tempWordName);
            doc.save(os,format);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            // EPUB, XPS, SWF 相互转换
            System.out.println("Execute Pdf:"+tempPdfName);
            //Files.deleteIfExists(Paths.get(tempWordName));//删除临时文件
            return tempPdfName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

3 测试主程序

java 复制代码
    public static void main(String[] args) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("name", "斯蒂芬·库里");
        dataMap.put("age", "33");
        dataMap.put("team", "金州勇士队");
        dataMap.put("city", "奥克兰");
        dataMap.put("location", "控球后卫");
        dataMap.put("no", "30");
        dataMap.put("info", "投篮准、出手速度快、善于抢断与投三分球");
        ftl2word2pdf(dataMap,"playerInfo.ftl",SaveFormat.PDF);
        System.out.println("success......");
    }

4 结果:

pdf文件

word文件

还可以生成图片:

相关推荐
好家伙VCC6 分钟前
**基于RISC-V架构的嵌入式系统开发:从零开始构建高效低功耗应用**在当前物联网(IoT)和边缘计
java·python·物联网·架构·risc-v
wyu7296116 分钟前
Spring框架学习笔记:从IoC到声明式事务
java
qqacj28 分钟前
Spring Security 官网文档学习
java·学习·spring
Rsun0455139 分钟前
10、Java 桥接模式从入门到实战
java·开发语言·桥接模式
金銀銅鐵41 分钟前
[Java] 从 class 文件看 cglib 对 MethodInterceptor 的处理 (下)
java·后端
lee_curry43 分钟前
Java中关于“锁”的那些事
java·线程·并发·juc
pearlthriving1 小时前
c++当中的泛型思想以及c++11部分新特性
java·开发语言·c++
梦魇星虹1 小时前
idea Cannot find declaration to go to
java·ide·intellij-idea
小雅痞1 小时前
[Java][Leetcode hard] 42. 接雨水
java·开发语言·leetcode
xfcoding1 小时前
关于代码注释的思考
java