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

还可以生成图片:

相关推荐
Le1Yu1 天前
2025-10-7学习笔记
java·笔记·学习
popoxf1 天前
spring容器启动流程(反射视角)
java·后端·spring
AAA修煤气灶刘哥1 天前
监控摄像头?不,我们管这个叫优雅的埋点艺术!
java·后端·spring cloud
寻星探路1 天前
Java EE初阶启程记09---多线程案例(2)
java·开发语言·java-ee
武子康1 天前
Java-141 深入浅出 MySQL Spring事务失效的常见场景与解决方案详解(3)
java·数据库·mysql·spring·性能优化·系统架构·事务
珹洺1 天前
Java-Spring入门指南(十五)SpringMVC注解开发
java·spring·microsoft
小满、1 天前
什么是Maven?关于 Maven 的坐标、依赖管理与 Web 项目构建
java·maven
半旧夜夏1 天前
【设计模式】核心设计模式实战
java·spring boot·设计模式
zhangfeng11331 天前
R 导出 PDF 时中文不显示 不依赖 showtext** 的最简方案(用 extrafont 把系统 TTF 真正灌进 PDF 内核)
开发语言·r语言·pdf·生物信息
半旧夜夏1 天前
【Spring】AOP的核心原理配方
java·spring