Convert HTML to PDF for download in Spring Boot

  • Last updated Apr 25, 2024

Converting HTML web content to a PDF file is a common requirement in many web applications, particularly when generating reports, invoices, or other printable documents. In this tutorial, we will share various examples of Spring Boot code for converting HTML to PDF using different Java libraries. Each example includes a REST API for downloading a PDF file after converting it from HTML.

Convert Using Flying Saucer

Flying Saucer and JSoup are both open-source Java libraries. Flying Saucer provides as an XHTML and CSS 2.1 renderer, commonly used for converting HTML and CSS-styled content into PDF documents. On the other hand, JSoup offers a convenient API for parsing HTML, extracting and manipulating data from HTML documents, and traversing the HTML Document Object Model (DOM). It is commonly used for web scraping and HTML parsing tasks in Java applications.

Add Jsoup and Flying Saucer libraries to your project.

If you're using Maven, include the following dependencies to your pom.xml:

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

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.17.2</version>
</dependency>

If you're using Gradle, include the following dependencies to your build.gradle:

implementation group: 'org.xhtmlrenderer', name: 'flying-saucer-pdf', version: '9.5.1'
implementation group: 'org.jsoup', name: 'jsoup', version: '1.17.2'

In your application's web controller, create a REST method that enables the download of a PDF file after converting from HTML, as demonstrated in the example below:

import java.io.ByteArrayOutputStream;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.xhtmlrenderer.pdf.ITextRenderer;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@RestController
public class OpenPdfController {

  @CrossOrigin(allowedHeaders = "*", origins = "*")
  @GetMapping(path = "download-pdf/{htmlDocumentId}")
  public void generatePdf(@PathVariable(name = "htmlDocumentId") String htmlDocumentId,
      HttpServletRequest request, HttpServletResponse response) {

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
      // Get HTML document here
      String htmlContent =
          "<html lang=\"en\"><head><title>Example Report</title></head><body><h1 class=\"\">Report</h1><p>This is paragraph 1.</p><p>This is paragraph 2.</p></body></html>";

      Document document = Jsoup.parse(htmlContent, "UTF-8");
      document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);


      ITextRenderer renderer = new ITextRenderer();
      renderer.setDocumentFromString(document.html());

      // Call layout before creating PDF
      renderer.layout();

      // Set up response headers
      String filename = "document.pdf";
      response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
      response.setContentType(MediaType.APPLICATION_PDF_VALUE);
      response.setHeader("Access-Control-Expose-Headers", "*");

      // Create PDF and write to response output stream
      renderer.createPDF(baos);
      baos.writeTo(response.getOutputStream());
      response.getOutputStream().flush();

    } catch (Exception ex) {
      System.out.println("Error caught while generating pdf from html: " + ex);
    }
  }

}
Convert Using pdfHTML

pdfHTML is an iText add-on for Java, a popular Java library for creating and manipulating PDF documents. The pdfHTML provides functionality to convert HTML content, including its associated CSS styles, into PDF format. You may need to purchase license for commercial use.

Add pdfHTML library to your project.

If you're using Maven, include the following dependency to your pom.xml:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>html2pdf</artifactId>
    <version>5.0.2</version>
</dependency>

If you're using Gradle, include the following dependency to your build.gradle:

implementation group: 'com.itextpdf', name: 'html2pdf', version: '5.0.2'

In your application's web controller, create a REST method that allows the download of a PDF file:

import java.io.ByteArrayOutputStream;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.itextpdf.html2pdf.HtmlConverter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class ReportController {

  @CrossOrigin(allowedHeaders = "*", origins = "*")
  @GetMapping(path = "download-pdf/{htmlDocumentId}")
  public void generatePdf(@PathVariable(name = "htmlDocumentId") String htmlDocumentId,
      HttpServletRequest request, HttpServletResponse response) {

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
      // Get HTML document here
      String htmlContent =
          "<html lang=\"en\"><head><title>Example Report</title></head><body><h1 class=\"\">Report</h1><p>This is paragraph 1.</p><p>This is paragraph 2.</p></body></html>";

      HtmlConverter.convertToPdf(htmlContent, baos);

      String filename = "document.pdf";
      response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
      response.setContentType(MediaType.APPLICATION_PDF_VALUE);
      response.setHeader("Access-Control-Expose-Headers", "*");

      baos.writeTo(response.getOutputStream());
      response.getOutputStream().flush();

    } catch (Exception ex) {
      System.out.println("Error caught while generating pdf from html: " + ex);
    }
  }

}