Spring Boot integrates itext to realize the function of generating PDF from html

HBLOG
4 min readMar 28, 2024

--

1.itext introduction

iText is a project of the famous open source site sourceforge and is a java class library used to generate PDF documents. iText can not only generate PDF or rtf documents, but also convert XML and Html files into PDF files.

Features of iText

  • Interactive − iText provides you with classes (APIs) to generate interactive PDF documents. Using these you can create maps and books.
  • Adding bookmarks, page numbers, etc − Using iText, you can add bookmarks, page numbers, and watermarks.
  • Split & Merge − Using iText, you can split an existing PDF into multiple PDFs and add/join additional pages to it.
  • Fill Forms − Using iText you can fill out interactive forms in PDF documents。
  • Save as Image − Using iText, you can save a PDF as an image file, such as PNG or JPEG.
  • Canvas − iText library provides you with a Canvas class that you can use to draw various geometric shapes on PDF documents, such as circles, lines, etc.
  • Create PDFs − Using iText, you can create new PDF files from a Java program. You can also include images and fonts.

2.Code Project

Experimental goal: Generate thymeleaf views into PDF

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>itextpdf</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.1.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

application.yaml

server:
port: 8088
spring:
thymeleaf:
cache: false

DemoApplication

package com.et.itextpdf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

controller

converterProperties.setBaseUri is important. Otherwise, static resources like /main.css will not be found

package com.et.itextpdf.controller;
import com.et.itextpdf.pojo.Order;
import com.et.itextpdf.util.OrderHelper;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@Controller
@RequestMapping("/orders")
public class PDFController {

@Autowired
ServletContext servletContext;
private final TemplateEngine templateEngine;
public PDFController(TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}
@RequestMapping(path = "/")
public String getOrderPage(Model model) {
Order order = OrderHelper.getOrder();
model.addAttribute("orderEntry", order);
return "order";
}
@RequestMapping(path = "/pdf")
public ResponseEntity<?> getPDF(HttpServletRequest request, HttpServletResponse response) throws IOException {
/* Do Business Logic*/
Order order = OrderHelper.getOrder();
/* Create HTML using Thymeleaf template Engine */
WebContext context = new WebContext(request, response, servletContext);
context.setVariable("orderEntry", order);
String orderHtml = templateEngine.process("order", context);
/* Setup Source and target I/O streams */
ByteArrayOutputStream target = new ByteArrayOutputStream();
ConverterProperties converterProperties = new ConverterProperties();
converterProperties.setBaseUri("http://localhost:8088");
/* Call convert method */
HtmlConverter.convertToPdf(orderHtml, target, converterProperties);
/* extract output as bytes */
byte[] bytes = target.toByteArray();

/* Send the response as downloadable PDF */
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=order.pdf")
.contentType(MediaType.APPLICATION_PDF)
.body(bytes);
}
}

view

Spring MVC comes with a template engine that can serve dynamic HTML content. We can easily convert these responses to PDF format using the following method. In this example, I imported spring-boot-starter-web and spring-boot-starter-thymeleaf to provide MVC and thymeleaf support for my spring boot project. You can use the template engine of your choice. Take a look at the contents of the thymeleaf template below. Mainly displays order details. In addition, some virtual order content is generated through the auxiliary methods of OrderHelper.

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
name="viewport">
<meta content="ie=edge" http-equiv="X-UA-Compatible">
<title>Spring Boot - Thymeleaf</title>
<link th:href="@{/main.css}" rel="stylesheet"/>
</head>
<body class="flex items-center justify-center h-screen">
<div class="rounded-lg border shadow-lg p-10 w-3/5">
<div class="flex flex-row justify-between pb-4">
<div>
<h2 class="text-xl font-bold">Order #<span class="text-green-600" th:text="${orderEntry.orderId}"></span>
</h2>
</div>
<div>
<div class="text-xl font-bold" th:text="${orderEntry.date}"></div>
</div>
</div>
<div class="flex flex-col pb-8">
<div class="pb-2">
<h2 class="text-xl font-bold">Delivery Address</h2>
</div>
<div th:text="${orderEntry.account.address.street}"></div>
<div th:text="${orderEntry.account.address.city}"></div>
<div th:text="${orderEntry.account.address.state}"></div>
<div th:text="${orderEntry.account.address.zipCode}"></div>
</div>
<table class="table-fixed w-full text-right border rounded">
<thead class="bg-gray-100">
<tr>
<th class="text-left pl-4">Product</th>
<th>Qty</th>
<th>Price</th>
<th class="pr-4">Total</th>
</tr>
</thead>
<tbody>
<tr th:each="item : ${orderEntry.items}">
<td class="pl-4 text-left" th:text="${item.name}"></td>
<td th:text="${item.quantity}"></td>
<td th:text="${item.price}"></td>
<td class="pr-4" th:text="${item.price * item.quantity}"></td>
</tr>
</tbody>
</table>
<div class="flex flex-row-reverse p-5">
<h2 class="font-medium bg-gray-200 p-2 rounded">
Grand Total: <span class="text-green-600" th:text="${orderEntry.payment.amount}"></span>
</h2>
</div>
<h2 class="text-xl font-bold">Payment Details</h2>
<table class="table-fixed text-left w-2/6 border">
<tr>
<th class="text-green-600">Card Number</th>
<td th:text="${orderEntry.payment.cardNumber}"></td>
</tr>
<tr>
<th class="text-green-600">CVV</th>
<td th:text="${orderEntry.payment.cvv}"></td>
</tr>
<tr>
<th class="text-green-600">Expires (MM/YYYY)</th>
<td th:text="${orderEntry.payment.month +'/'+ orderEntry.payment.year}"></td>
</tr>
</table>
</div>
</body>
</html>

POJO

package com.et.itextpdf.pojo;
import lombok.Data;
@Data
public class Account {
private String name;
private String phoneNumber;
private String email;
private Address address;

}
package com.et.itextpdf.pojo;
import lombok.Data;
@Data
public class Address {
private String street;
private String city;
private String state;
private String zipCode;
}
package com.et.itextpdf.pojo;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class Item {
private String sku;
private String name;
private Integer quantity;
private BigDecimal price;
}
package com.et.itextpdf.pojo;
import lombok.Data;
import java.util.List;
@Data
public class Order {
private Integer orderId;
private String date;
private Account account;
private Payment payment;
private List<Item> items;
}
package com.et.itextpdf.pojo;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class Payment {
private BigDecimal amount;
private String cardNumber;
private String cvv;
private String month;
private String year;
}

The above are just some key codes. For all codes, please see the code repository below.

Code Repository

3.Test

  • start spring boot application
  • open the browser and input http://127.0.0.1:8088/orders/

4.Reference

--

--

HBLOG
HBLOG

Written by HBLOG

talk is cheap ,show me your code

No responses yet