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文件

还可以生成图片:

相关推荐
SimonKing2 小时前
OpenCode AI辅助编程,不一样的编程思路,不写一行代码
java·后端·程序员
FastBean2 小时前
Jackson View Extension Spring Boot Starter
java·后端
Seven973 小时前
剑指offer-79、最⻓不含重复字符的⼦字符串
java
皮皮林55112 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河13 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程16 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅17 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者18 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺18 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端