java使用html模板导出pdf(使用freemarker进行渲染)

  • 依赖
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- ALWAYS required. -->
<dependency>
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-core</artifactId>
    <version>1.0.0</version>
</dependency>
<!-- Required for PDF output. -->
<dependency>
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-pdfbox</artifactId>
    <version>1.0.0</version>
</dependency>
  • 工具类
package com.shell.common.utils.pdf;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import org.springframework.util.ResourceUtils;
import java.io.OutputStream;
public class PdfUtil {
    /**
     *
     * @param os
     * @param htmlContent
     * @throws Exception
     */
    public static void buildPdf(OutputStream os, String htmlContent) throws Exception {
        PdfRendererBuilder builder = new PdfRendererBuilder();
        builder.useFont(ResourceUtils.getFile("classpath:templates/fonts/simsun.ttf"), "simsun");
        builder.useFastMode();
        builder.withHtmlContent(htmlContent, ResourceUtils.getURL("classpath:templates/img/").toString());
        builder.toStream(os);
        builder.run();
    }
}
  • 测试代码
package com.step.pdf.demo.controller;
import com.step.pdf.demo.util.PdfUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.DateUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@RestController
public class DemoController {
    @GetMapping("pdf2")
    public void test2(HttpServletResponse response, HttpServletRequest request) throws Exception {
        OutputStream out = response.getOutputStream();
        Writer w =null;
        ByteArrayOutputStream outStream = null;
        Map<String, Object> variables = new HashMap<>();
        variables.put("staffName", "staffName");
        variables.put("functions", "functions");
        variables.put("assessmentDate", "assessmentDate");
        variables.put("roles", "roles");
        variables.put("aggregateSource", "aggregateSource");
        variables.put("questionList", null);
        variables.put("comments", null);
        variables.put("score", "score");
        variables.put("signature", "signature");
        variables.put("paperName","paperName");
        variables.put("assessmentTypeName","info.getAssessmentTypeName()");
        variables.put("pdfFileName","test");
        try {
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
            // 模板路径
            String templatePath = "src/main/resources";
            // step2 获取模版路径
            cfg.setDirectoryForTemplateLoading(new File(templatePath));
            Template freemarkerTemplate = cfg.getTemplate("/template/exam-paper.ftlh");
            outStream = new ByteArrayOutputStream();
            w = new OutputStreamWriter(outStream, "utf-8");
            //不要写成下面: 否则无法关闭fos流,打zip包时存取被拒抛异常
            //w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
            freemarkerTemplate.process(variables, w);
            String htmlContent = outStream.toString("utf-8");
            PdfUtil.buildPdf(out,htmlContent);
        }catch (Exception e) {
           e.printStackTrace();
        }
        finally {
            try {
                outStream.close();
                out.close();
                w.close();
            } catch (Exception e) {
               e.printStackTrace();
            }
        }
    }
}
  • 模板代码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8" />
    <title>${pdfFileName}</title>
    <style>
        @page {
            size: A4;
            @top-center {
                content: element(header);
            }
            @bottom-center {
                font-family: 'simsun', serif;
            }
        }
        html,
        body {
            font-family: 'simsun', serif;
            font-size: 14px;
        }
        table {
            -fs-table-paginate: paginate;
            border-collapse: collapse;
            width: 100%;
        }
        .headingBox {
            font-size: 18px;
            text-align: center;
            font-style: italic;
            font-weight: bolder;
        }
        table,
        td,
        th {
            border: 0px solid #555;
            padding: 3px 5px;
        }
        .box1,
        .box2,
        .box3 {
            padding: 10px 20px;
            border-top: 1px solid #999;
        }
        .trBox {
            display: flex;
        }
        .tdBox {
            width: 50%;
            display: flex;
        }
        .titleBox {
            display: inline-block;
            width: 32%;
            font-weight: bold;
        }
        .contentBox {
            width: 68%;
            color: cornflowerblue;
        }
        .titleOverviewBox {
            display: inline-block;
            width: 64%;
            font-weight: bold;
        }
        .contentOverViewBox {
            width: 36%;
            color: cornflowerblue;
        }
        .functionTitleBox,
        .commentsTitleBox {
            font-style: italic;
            font-weight: bolder;
            margin: 0 0 0 2%;
        }
    </style>
    <style>
        #app {
            border: 1px solid #999;
            width: 550px;
            box-sizing: border-box;
            margin: 0px auto;
        }
        #comm {
            height: 100px;
        }
        #content {
            white-space: pre-line;
        }
    </style>
</head>
<body>
<div id="app">
    <h3 class="headingBox">Competence Assessment_${assessmentTypeName}</h3>
    <div class="box1">
        <table border="0px;" class="baseInfoBox">
            <tr class="trBox">
                <td class="tdBox"><span class="titleBox">Name:</span><span class="contentBox"
                    >${staffName}</span></td>
                <td class="tdBox"></td>
            </tr>
            <tr class="trBox">
                <td class="tdBox"><span class="titleBox">Function:</span><span class="contentBox"
                    >${functions}</span></td>
                <td class="tdBox"><span class="titleOverviewBox">Date:</span><span class="contentOverViewBox"
                    >${assessmentDate}</span></td>
            </tr>
            <tr class="trBox">
                <td class="tdBox"><span class="titleBox">Role:</span><span class="contentBox"
                    >${roles}</span></td>
                <td class="tdBox"><span class="titleOverviewBox">Aggregate Source:</span><span
                            class="contentOverViewBox">${aggregateSource}</span></td>
            </tr>
        </table>
    </div>
    <#--<#list questionList as question>
        <div class="box1">
            <h4 class="functionTitleBox">${question.functionName}:</h4>
            <div class="w">
                <tbody border="0">
                <tr>
                    <td id="content">${question.questionContent}</td>
                </tr>
                </tbody>
            </div>
        </div>
        <div class="box1">
            <h4 class="commentsTitleBox">Comments:</h4>
            <div class="c" height="500px" id="comm">
            </div>
            <div style="display: flex;">
                <div style="padding-left: 18em"><span>Score:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span>
                    <span>Signature:</span></div>
            </div>
        </div>
    </#list>-->
</div>
</body>
</html>
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用Freemarker和iText可以方便地将HTML换为PDF。下面是一个基本的示例: 1. 首先,您需要引入iText和freemarker的依赖项。如果您使用的是Maven,可以添加以下依赖项到pom.xml文件中: ``` <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> ``` 2. 创建Freemarker模板,例如: ``` <html> <head> <title>${title}</title> </head> <body> <h1>${heading}</h1> <p>${content}</p> </body> </html> ``` 3. 创建Java类来生成PDF文件。例如: ``` import java.io.*; import java.util.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; import freemarker.template.*; public class HtmlToPdf { public static void main(String[] args) throws Exception { // 创建数据模型 Map<String, Object> data = new HashMap<String, Object>(); data.put("title", "My PDF Document"); data.put("heading", "Hello World"); data.put("content", "This is my first PDF document."); // 创建Freemarker模板 Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); cfg.setClassForTemplateLoading(HtmlToPdf.class, "/"); Template template = cfg.getTemplate("template.ftl"); // 将Freemarker模板渲染HTML StringWriter writer = new StringWriter(); template.process(data, writer); String html = writer.toString(); // 创建PDF文档 Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); // 将HTML换为PDF InputStream is = new ByteArrayInputStream(html.getBytes()); XMLWorkerHelper.getInstance().parseXHtml(writer, document, is); // 关闭PDF文档 document.close(); } } ``` 这个示例将生成一个名为“output.pdf”的PDF文件,其中包含一个标题为“My PDF Document”的页面,以及一个标题为“Hello World”和内容为“This is my first PDF document.”的段落。 请注意,此示例仅是一个基本示例。您可以根据需要修改模板Java代码以生成更复杂的PDF文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值