Java Springboot打印PDF教程

1. 整体流程

下面是实现Java Springboot打印PDF的整体流程表格:

步骤 描述
步骤一 创建Springboot项目
步骤二 添加相关依赖
步骤三 创建PDF模板
步骤四 填充PDF模板数据
步骤五 生成PDF文件
步骤六 提供下载链接

接下来,我们将逐步介绍每一个步骤需要做什么,以及具体的代码实现。

2. 步骤一:创建Springboot项目

首先,我们需要创建一个Springboot项目,这里我以IntelliJ IDEA为例进行演示。

在IDEA中,点击"File" -> "New" -> "Project",选择"Spring Initializr",填写项目的基本信息,然后点击"Next"。

在"Dependencies"中,选择"Web"和"Thymeleaf",然后点击"Next",最后点击"Finish"完成项目创建。

3. 步骤二:添加相关依赖

在创建好的Springboot项目中,打开pom.xml文件,添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>

上述依赖包含了处理模板的Thymeleaf和生成PDF文件的iText。

4. 步骤三:创建PDF模板

src/main/resources/templates目录下,创建一个名为template.html的HTML模板文件。

在模板中,可以使用Thymeleaf的语法来动态生成模板内容。下面是一个简单的示例:

<!DOCTYPE html>
<html xmlns:th="
<body>
    
    <p th:text="${content}"></p>
</body>
</html>

在上述示例中,我们使用了Thymeleaf的变量表达式${}来填充模板数据。

5. 步骤四:填充PDF模板数据

在Springboot的Controller中,我们需要定义一个用于处理打印PDF请求的接口。

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class PdfController {

    @GetMapping("/print-pdf")
    public String printPdf(Model model) {
        model.addAttribute("title", "Hello, PDF");
        model.addAttribute("content", "This is a PDF generated by Java Springboot.");
        return "pdfTemplate";
    }
}

在上述代码中,我们使用@GetMapping注解定义了一个/print-pdf的接口,用于处理打印PDF的请求。我们通过Model对象将需要填充的数据传递给模板。

6. 步骤五:生成PDF文件

为了生成PDF文件,我们需要创建一个PDF生成工具类。

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class PdfGenerator {

    public static void generatePdf(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws DocumentException, IOException {
        Document document = new Document();
        OutputStream outputStream = response.getOutputStream();
        PdfWriter.getInstance(document, outputStream);

        document.open();
        ClassPathResource resource = new ClassPathResource("templates/template.html");
        byte[] templateBytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
        String htmlContent = new String(templateBytes, StandardCharsets.UTF_8);

        String processedHtml = processHtmlTemplate(htmlContent, modelMap);
        InputStream inputStream = new ByteArrayInputStream(processedHtml.getBytes(StandardCharsets.UTF_8));

        HtmlConverter.convertToPdf(inputStream, document);
        document.close();

        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=\"output.pdf\"");
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

    private static String processHtmlTemplate(String htmlTemplate, ModelMap modelMap) {
        Context context = new Context();
        context.setVariables(modelMap);