Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

An easier way to generate PDFs from HTML templates

March 11, 2022
Muhammad Edwin
Related topics:
JavaLinuxOpen sourceSpring Boot
Related products:
Red Hat Enterprise Linux

Share:

    Applications frequently are required to generate invoices, reports, ID cards, and much more in PDF format. There are Java libraries and tools that developers can use to generate PDFs, including the popular JasperReports. While sophisticated, using these programs can be complicated because they support a wide range of documents.

    This article introduces a simpler tool, the open source wkhtmltopdf utility. I will show you how to use wkhtmltopdf to solve a common scenario: You have an HTML form, parameterized to accept input data, and you need to produce a PDF from the data in that form. You will learn how to set up your data and make a call to the wkhtmltopdf utility from a Spring Boot web application. We'll use Red Hat's Universal Base Image (UBI) 8 as a base image to simplify the application build, then deploy the final image into Red Hat Openshift 4.

    Note: You can find the code for the example in my GitHub repository.

    Configuration using Maven

    Let's start with a Maven file. I'm using a Snowdrop bill of materials (BOM) on my Maven project object model (POM) file instead of a community version of Spring Boot:

    <?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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.edw</groupId>
        <artifactId>SpringBootAndPdf</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <repositories>
            <repository>
                <id>redhat-early-access</id>
                <name>Red Hat Early Access Repository</name>
                <url>https://maven.repository.redhat.com/earlyaccess/all/</url>
            </repository>
            <repository>
                <id>redhat-ga</id>
                <name>Red Hat GA Repository</name>
                <url>https://maven.repository.redhat.com/ga/</url>
            </repository>
        </repositories>
    
    
        <pluginRepositories>
            <pluginRepository>
                <id>redhat-early-access</id>
                <name>Red Hat Early Access Repository</name>
                <url>https://maven.repository.redhat.com/earlyaccess/all/</url>
            </pluginRepository>
            <pluginRepository>
                <id>redhat-ga</id>
                <name>Red Hat GA Repository</name>
                <url>https://maven.repository.redhat.com/ga/</url>
            </pluginRepository>
        </pluginRepositories>
    
        <properties>
            <snowdrop-bom.version>2.4.9.Final-redhat-00001</snowdrop-bom.version>
            <spring-boot.version>2.1.4.RELEASE-redhat-00001</spring-boot.version>
            <maven.compiler.source>11</maven.compiler.source>
            <maven.compiler.target>11</maven.compiler.target>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>dev.snowdrop</groupId>
                    <artifactId>snowdrop-dependencies</artifactId>
                    <version>${snowdrop-bom.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
    
        <build>
            <finalName>app</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                    <configuration>
                        <mainClass>com.edw.Main</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    </project>

    The Spring Boot program

    Our main class in Java is:

    package com.edw;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Main {
        public static void main(String[] args) {
            SpringApplication.run(Main.class, args);
        }
    }

    The following Controller class displays a generated PDF. One of the class's most important tasks is to display the PDF properly in a browser; this task is achieved by setting up a proper MediaType that produces an application/pdf content-type header. Another important task is to call an external process that runs wkhtmltopdf to trigger the conversion from HTML to PDF:

    package com.edw.controllers;
    
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Scanner;
    import java.util.UUID;
    
    @Controller
    public class ReportController {
    
        @GetMapping(
                value = "/generate-report",
                produces = MediaType.APPLICATION_PDF_VALUE
        )
        public @ResponseBody byte[] generateReport(@RequestParam(value = "name") String name,
                                                   @RequestParam(value = "address") String address) throws Exception {
            String uuid = UUID.randomUUID().toString();
            Path pathHtml = Paths.get("/tmp/" + uuid + ".html");
            Path pathPdf = Paths.get("/tmp/" + uuid + ".pdf");
    
            try {
                // read the template and fill the data
                String htmlContent = new Scanner(getClass().getClassLoader().getResourceAsStream("template.html"), "UTF-8")
                                        .useDelimiter("\\A")
                                        .next();
                htmlContent = htmlContent.replace("$name", name)
                                            .replace("$address", address);
    
                // write to html
                Files.write(pathHtml, htmlContent.getBytes());
    
                // convert html to pdf
                Process generateToPdf = Runtime.getRuntime().exec("wkhtmltopdf " + pathHtml.toString() + " " + pathPdf.toString() );
                generateToPdf.waitFor();
    
                // deliver pdf
                return Files.readAllBytes(pathPdf);
    
            } finally {
                // delete temp files
                Files.delete(pathHtml);
                Files.delete(pathPdf);
            }
        }
    }

    Formatting the HTML data

    The following HTML template generates a report with input data consisting of names (the $name parameter) and addresses (the $address parameter):

    <html>
    <head>
        <style>
    		<!-- put your css here -->
        </style>
    </head>
    <body>
        <div class="container" style="padding-top: 100px;">
            <div class="row justify-content-center">
                <div class="col-md-6">
                    <table class="table table-bordered">
                        <thead class="thead-light">
                            <tr>
                                <th>Name</th>
                                <th>Address</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>$name</td>
                                <td>$address</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </body>
    </html>

    Installing the wkhtmltopdf program

    This next part is where the magic happens. First, you need to download wkhtmltopdf from GitHub and extract the program. After that, you can create a wkhtml folder within your Java project and copy wkhtmltopdf to the application's folder from the bin folder:

    $ wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
    
    $ tar -xf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
    
    $ mkdir /code/wkhtml
    
    $ cp wkhtmltox/bin/wkhtmltopdf /code/wkhtml/

    The resulting directory structure looks like:

    +--- .gitignore
    +--- Dockerfile
    +--- pom.xml
    +--- src
    |   +--- main
    |   |   +--- java
    |   |   |   +--- com
    |   |   |   |   +--- edw
    |   |   |   |   |   +--- controllers
    |   |   |   |   |   |   +--- HelloWorldController.java
    |   |   |   |   |   |   +--- ReportController.java
    |   |   |   |   |   +--- Main.java
    |   |   +--- resources
    |   |   |   +--- application.properties
    |   |   |   +--- template.html
    |   +--- test
    |   |   +--- java
    +--- wkhtml
    |   +--- wkhtmltopdf

    Building the image

    The following Dockerfile uses UBI 8 as the base image:

    FROM registry.access.redhat.com/ubi8/openjdk-11-runtime:1.10
    
    USER root
    
    ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' TZ='Asia/Jakarta'
    
    RUN microdnf update && \
        microdnf install tzdata libXrender libXext fontconfig  && \
        ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && \
        microdnf clean all
    
    COPY wkhtml/wkhtmltopdf /usr/local/bin/
    
    EXPOSE 8080
    USER 185
    
    COPY target/app.jar /deployments/app.jar
    ENTRYPOINT [ "java", "-jar", "/deployments/app.jar" ]

    Now, build your image and run it:

    $ docker build -t springboot-and-pdf . 
    
    $ docker run -p 8080:8080  springboot-and-pdf

    In your browser, enter the generate-report URL with a name and address as parameters, and you get a PDF ready to download and print, as shown in Figure 1.

    The application displays a PDF reflecting the application's HTML template.
    Figure 1. The Spring Boot application generates a PDF report from the data in an HTML template.

    Conclusion

    The streamlined process shown in this example is useful in a variety of situations where you want to generate a report with tabular data or another structured PDF based on input data. Feel free to leave comments on this article to discuss where you might use this approach and any questions you may have.

    Last updated: July 31, 2023

    Related Posts

    • Node.js - Harnessing the power of Java (for PDF generation and more)

    • Eclipse MicroProfile for Spring Boot developers

    • Migrating Spring Boot tests to Quarkus

    Recent Posts

    • GuideLLM: Evaluate LLM deployments for real-world inference

    • Unleashing multimodal magic with RamaLama

    • Integrate Red Hat AI Inference Server & LangChain in agentic workflows

    • Streamline multi-cloud operations with Ansible and ServiceNow

    • Automate dynamic application security testing with RapiDAST

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue