java 使用freemarker生成html

java 使用freemarker生成html

技术方案

java 1.8 + Freemarker

maven依赖

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.31</version> <!-- 请使用最新版本 -->
</dependency>

创建模板文件,保存至src/main/resources/templates/,文件名为template.html

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
    <title>${name}的简历</title>
    <style>
        @page {
            size: A4;
            margin: 5mm 10mm; /* 上下和左右两个方向的边距分别为 10mm 和 20mm */
        }
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f9;
        }
        .container {
            width: 80%;
            margin: 0 auto;
            padding: 20px;
            background-color: #ffffff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .section-title {
            color: #4CAF50;
            margin-top: 20px;
        }
        .section-content {
            margin: 10px 0;
        }
        .contact-info, .skills, .experience, .education {
            margin-bottom: 20px;
        }
        .experience, .education {
            margin-top: 10px;
        }
        ul {
            list-style-type: none;
            padding-left: 0;
        }
        li {
            margin-bottom: 10px;
        }
        .job, .degree {
            font-weight: bold;
        }
    </style>
</head>



<body>

<div class="container">
    <h1>${name}的简历</h1>

    <!-- 联系信息 -->
    <div class="contact-info">
        <h2 class="section-title">联系信息</h2>
        <p>邮箱: ${email}</p>
        <p>电话: ${phone}</p>
    </div>

    <!-- 工作经历 -->
    <div class="experience">
        <h2 class="section-title">工作经历</h2>
        <#list experience as job>
        <div class="job">
            <p><strong>${job.position}</strong> 在 ${job.company}(${job.startYear} - ${job.endYear})</p>
            <p>${job.description}</p>
        </div>
        </#list>
    </div>

    <!-- 教育背景 -->
    <div class="education">
        <h2 class="section-title">教育背景</h2>
        <#list education as degree>
            <div class="degree">
                <p><strong>${degree.degree}</strong> ${degree.school}(${degree.year}年)</p>
            </div>
        </#list>
    </div>
</div>

</body>
</html>

实现代码

package com.ruoyi.system.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.util.Map;

/**
 * @Program: RuoYi-master
 * @ClassName: GenerateHtmlUtil
 * @author: zhouzihao
 * @date: 2025年2月20日, 0020 下午 03:33
 * @version: 1.0.0
 * @Description:
 * @Time: 2025-02-20 15:33
 */
public class GenerateHtmlUtil {

    // 模板路径
    private static final String templatesPath = "/templates";
    // 模板文件
    private static final String templatesFile = "template.html";

    public static void generateHtml(Map<String, Object> data, String filePName) {
        // 配置 Freemarker
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_30);
        cfg.setClassForTemplateLoading(GenerateHtmlUtil.class, templatesPath);

        // 获取 HTML 模板
        try {
            Template template = cfg.getTemplate(templatesFile);

            // 5. 创建输出流
            try (Writer writer = new FileWriter(new File(filePName))) {
                // 6. 合并模板和数据模型,生成 HTML 文件
                template.process(data, writer);
                System.out.println("HTML file generated successfully!");
            } catch (IOException | TemplateException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
posted @   ghostmen  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示