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

还可以生成图片:

相关推荐
liangshanbo121535 分钟前
虚拟列表深度面试题整理
java·开发语言·前端
gugucoding1 小时前
59. 【Java】Spring Boot 入门:第一个 Web 应用
java·开发语言·spring boot
点我头像干啥1 小时前
004:多个PDF文件合并
开发语言·pdf·php
10mAh2 小时前
【Java】HashMap 的 put 到底做了什么?——冲突、扩容与树化实测
java·开发语言·hash
我命由我123452 小时前
人脸识别 - 勒克斯(光线照射到物体表面上的明亮程度)
android·java·学习·java-ee·人脸识别·学习方法·android runtime
TechLee3 小时前
跨语言加解密总对不上?这个纯 Go 神库让 AES/RSA 与 PHP、Java 100% 互通
java·后端·算法
LINgZone23 小时前
系统架构与分支规范
java·数据库·架构
AC赳赳老秦3 小时前
企业级合规审计体系:用 OpenClaw 落地采集全链路留痕,自动生成合规审计报告
java·python·django·beautifulsoup·php·deepseek·openclaw
吃杠碰小鸡4 小时前
VsCode中开发Java项目
java·vscode