Spring Boot buat PDF dengan Thymeleaf

 

Halo guys kali ini saya akan sharing bagaimana generate pdf dengan thymeleaf. thymeleaf tidak hanya menangani HTML namun banyak sekali seperti XML, XHTML, Javascript bahkan text. 

disini kita akan menggunakan Flaying Soucer yang mana akan mengconvert HTML atau XHTML ke bentuk PDF.


Requirement:

- maven

- jdk 8

- intelliJ 


tambahkan dulu dependency untuk generate pdfnya

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gpch</groupId>
    <artifactId>pdfgenerator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>pdfgenerator</name>
    <description>Pdf Generator</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf -->
        <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>flying-saucer-pdf</artifactId>
            <version>9.1.20</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

lalu buat modelnya dengan buat java class baru Student.java

package com.gpch.pdfgenerator.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Student {
    private Integer id;
    private String name;
    private String lastName;
    private LocalDate birthday;
    private String nationality;
    private String university;
    private Boolean active;
}

lalu buat fake service data StudentService.java

package com.gpch.pdfgenerator.service;

import com.gpch.pdfgenerator.model.Student;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
public class StudentService {

    public List<Student> getStudents(){
        final List<Student> students = IntStream.range(1, 10)
                .mapToObj(v -> Student.builder()
                        .id(v)
                        .name("Name " + v)
                        .lastName("Last Name " + v)
                        .active(v%2==0?true:false)
                        .birthday(LocalDate.now())
                        .nationality("Nationality " + v)
                        .university("University " + v)
                        .build())
                .collect(Collectors.toList());
        return students;
    }
}

lalu buat PdfService.java

package com.gpch.pdfgenerator.service;

import com.lowagie.text.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

@Service
public class PdfService {

    private static final String PDF_RESOURCES = "/pdf-resources/";
    private StudentService studentService;
    private SpringTemplateEngine templateEngine;

    @Autowired
    public PdfService(StudentService studentService, SpringTemplateEngine templateEngine) {
        this.studentService = studentService;
        this.templateEngine = templateEngine;
    }

    public File generatePdf() throws IOException, DocumentException {
        Context context = getContext();
        String html = loadAndFillTemplate(context);
        return renderPdf(html);
    }


    private File renderPdf(String html) throws IOException, DocumentException {
        File file = File.createTempFile("students", ".pdf");
        OutputStream outputStream = new FileOutputStream(file);
        ITextRenderer renderer = new ITextRenderer(20f * 4f / 3f, 20);
        renderer.setDocumentFromString(html, new ClassPathResource(PDF_RESOURCES).getURL().toExternalForm());
        renderer.layout();
        renderer.createPDF(outputStream);
        outputStream.close();
        file.deleteOnExit();
        return file;
    }

    private Context getContext() {
        Context context = new Context();
        context.setVariable("students", studentService.getStudents());
        return context;
    }

    private String loadAndFillTemplate(Context context) {
        return templateEngine.process("pdf_students", context);
    }


}

lihat di variable PDF_RESOURCES diatas disana ada /pdf-resources dimana difolder sana kita akan menaruh resource yang kita butuhkan seperti image, css, dan javascript. 


lalu buat pdf_student untuk template PDF dan student.html untuk template webnya. 

pdf_student.html 

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Students</title>
    <bookmarks>
        <bookmark name="Students" href="#students"></bookmark>
    </bookmarks>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>

<body>
<img class="logo" src="images/logo.png"/>
<span class="titles-style" th:text="|Students / Count: ${#lists.size(students)}|"></span>

<!-- Students Table -->
<div id="students">
    <div th:if="${not #lists.isEmpty(students)}">
        <table class="students_table paginate-table">
            <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Last Name</th>
                <th>Nationality</th>
                <th>University</th>
                <th>Active</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="student : ${students}">
                <td th:text="${student?.id}"></td>
                <td th:text="${student?.name}"></td>
                <td th:text="${student?.lastName}"></td>
                <td th:text="${student?.nationality}"></td>
                <td th:text="${student?.university}"></td>
                <td th:text="${student?.active}"></td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

</body>
</html>

students.html

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Students View</title>
    <!-- Bootstrap -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">

    <h1 th:text="'Students / Count:' + ${#lists.size(students)}" ></h1>

    <div class="table-responsive">
        <table class="table">
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Last Name</th>
                <th>University</th>
                <th>Nationality</th>
                <th>Birthday</th>
                <th>Active</th>
            </tr>
            <tr th:each="student : ${students}">
                <td th:text="${student.id}"></td>
                <td th:text="${student.name}"></td>
                <td th:text="${student.lastName}"></td>
                <td th:text="${student.university}"></td>
                <td th:text="${student.nationality}"></td>
                <td th:text="${#temporals.format(student.birthday, 'dd/MM/yyyy')}"></td>
                <td th:text="${student.active}"></td>
            </tr>
        </table>
    </div>

    <a th:href="@{|/download-pdf|}">
        <img th:src="@{/images/pdf_icon.png}" height="35" width="35"/>
        <span>Download PDF Document</span>
    </a>
</div>

</body>
</html>

setelah buat templatenya kita buat Controller nya

StudentController.java

package com.gpch.pdfgenerator.controller;

import com.gpch.pdfgenerator.service.PdfService;
import com.gpch.pdfgenerator.service.StudentService;
import com.lowagie.text.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class StudentController {

    private final StudentService studentService;
    private final PdfService pdfService;

    @Autowired
    public StudentController(StudentService studentService, PdfService pdfService) {
        this.studentService = studentService;
        this.pdfService = pdfService;
    }

    @GetMapping("/students")
    public ModelAndView studentsView(ModelAndView modelAndView) {
        modelAndView.addObject("students", studentService.getStudents());
        modelAndView.setViewName("students");
        return modelAndView;
    }

    @GetMapping("/download-pdf")
    public void downloadPDFResource(HttpServletResponse response) {
        try {
            Path file = Paths.get(pdfService.generatePdf().getAbsolutePath());
            if (Files.exists(file)) {
                response.setContentType("application/pdf");
                response.addHeader("Content-Disposition",
                        "attachment; filename=" + file.getFileName());
                Files.copy(file, response.getOutputStream());
                response.getOutputStream().flush();
            }
        } catch (DocumentException | IOException ex) {
            ex.printStackTrace();
        }
    }
}


source: https://medium.com/@gustavo.ponce.ch/generating-pdf-documents-using-java-a29f90fbbd52


Komentar

Postingan populer dari blog ini

whois

Membuat export dan import Excel di spring boot

Spring Boot CRUD Thymeleaf-Pagination + Bootstrap Dynamic Modals