Generating PDF Files Using Thymeleaf – 使用Thymeleaf生成PDF文件

最后修改: 2020年 5月 12日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll learn how to generate PDFs using Thymeleaf as a template engine through a quick and practical example.

在本教程中,我们将通过一个快速而实用的示例,学习如何使用Thymeleaf作为模板引擎来生成PDF。

2. Maven Dependencies

2.Maven的依赖性

First, let’s add our Thymeleaf dependency:

首先,让我们添加我们的Thymeleaf依赖项。

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>

Thymeleaf by itself is just a template engine, and it can’t generate PDFs on its own. For this purpose, we’re going to add flying-saucer-pdf to our pom.xml:

Thymeleaf本身只是一个模板引擎,它不能独立生成PDF。为此,我们要在我们的flying-saucer-pdf中添加pom.xml

<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-pdf</artifactId>
    <version>9.1.20</version>
</dependency>

3. Generating PDFs

3.生成PDF文件

Next, let’s create a simple Thymeleaf HTML template – thymeleaf_template.html:

接下来,让我们创建一个简单的Thymeleaf HTML模板 – thymeleaf_template.html

<html xmlns:th="http://www.thymeleaf.org">
  <body>
    <h3 style="text-align: center; color: green">
      <span th:text="'Welcome to ' + ${to} + '!'"></span>
    </h3>
  </body>
</html>

And then, we’ll create a simple function – parseThymeleafTemplate – that’ll parse our template and return an HTML String:

然后,我们将创建一个简单的函数–parseThymeleafTemplate–它将解析我们的模板并返回一个HTMLString

private String parseThymeleafTemplate() {
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateMode.HTML);

    TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);

    Context context = new Context();
    context.setVariable("to", "Baeldung");

    return templateEngine.process("thymeleaf_template", context);
}

Finally, let’s implement a simple function that receives the previously generated HTML as input and write a PDF to our home folder:

最后,让我们实现一个简单的函数,接收先前生成的HTML作为输入,并将一个PDF写到我们的主文件夹中:

public void generatePdfFromHtml(String html) {
    String outputFolder = System.getProperty("user.home") + File.separator + "thymeleaf.pdf";
    OutputStream outputStream = new FileOutputStream(outputFolder);

    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocumentFromString(html);
    renderer.layout();
    renderer.createPDF(outputStream);

    outputStream.close();
}

After running our code, we’ll notice a file named thymeleaf.pdf, in our user’s home directory, that looks like this:

运行我们的代码后,我们会注意到一个名为thymeleaf.pdf的文件,在我们用户的主目录下,它看起来像这样:

As we can see, the text is green and aligned to the center as defined in our inline CSS. This is an extremely powerful tool for customizing our PDFs.

正如我们所看到的,文本是绿色的,并按照我们的内联CSS的定义向中心对齐。这是一个非常强大的工具,可以定制我们的PDF。

We should keep in mind that Thymeleaf is completely decoupled from Flying Saucer, meaning that we can use any other template engine for creating PDFs like Apache FreeMarker.

我们应该记住,Thymeleaf与Flying Saucer完全脱钩,这意味着我们可以使用任何其他模板引擎来创建PDF,如Apache FreeMarker

4. Conclusion

4.总结

In this quick tutorial, we’ve learned how to easily generate PDFs using Thymeleaf as a template engine.

在这个快速教程中,我们已经学会了如何使用Thymeleaf作为模板引擎来轻松生成PDF。

As always, the code is available over on GitHub.

像往常一样,代码可在GitHub上获得