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

还可以生成图片:

相关推荐
BlockChain888几秒前
MPC 钱包实战(三):Rust MPC Node + Java 调度层 + ETH 实际转账(可运行)
java·开发语言·rust
Remember_9932 分钟前
【数据结构】Java集合核心:线性表、List接口、ArrayList与LinkedList深度解析
java·开发语言·数据结构·算法·leetcode·list
小旭95272 分钟前
【Java 面试高频考点】finally 与 return 执行顺序 解析
java·开发语言·jvm·面试·intellij-idea
陌路2010 分钟前
RPC分布式通信(2)---四种典型式线程池(1)
java·开发语言·c++
微露清风11 分钟前
系统性学习C++-第二十四讲-智能指针的使用及其原理
java·c++·学习
wenjianhai18 分钟前
若依(RuoYi-Vue-Plus)框架使用WebSocket
java·若依
Coder_Boy_19 分钟前
基于SpringAI的在线考试系统-核心模块的数据模型交互关系
java·数据库·人工智能·spring boot·交互
yaoxin52112320 分钟前
295. Java Stream API - 选择适用于并行计算的 BinaryOperator
java·开发语言
We....20 分钟前
SpringBoot 微服务拦截器与负载均衡实践
java·spring boot·微服务·负载均衡
冬至喵喵21 分钟前
RoaringBitmap与传统Bitmap
java·开发语言