Converting HTML to PDF in Java: A Simple Guide

Daniel Angel
4 min readMar 27, 2023

--

In today’s digital world, there is a high demand for the ability to convert HTML to PDF format. This is because PDF files are easy to share and can be viewed across all platforms without worrying about compatibility issues. In this article, we will explore how to convert HTML to PDF format in Java, using a few simple steps.

We will be covering the following steps:

  1. Setting up the project
  2. Creating a PDF generator utility class
  3. Creating a service class and REST controller
  4. Testing our API in Postman

Step 1: Setting up the project

To get started, we need to create a new Spring Boot project. You can use your favorite IDE or build tool to do this.

We will also need to add the iText library as a dependency. We can do this by adding the following dependency to our pom.xml file:

  
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.10</version>
</dependency>
  • com.itextpdf:itextpdf:5.5.10 is a dependency of iText, a Java library for working with PDF files. It provides an API for creating, reading, and modifying PDF files, and can generate PDF documents from scratch or from predefined templates.
  • com.itextpdf.tool:xmlworker:5.5.10 is a dependency of iText that provides a tool for converting HTML and CSS to PDF. It uses the Flying Saucer HTML rendering library to create a DOM from the HTML and CSS, and then uses iText to convert the DOM to a PDF file.

These are necessary to be able to convert the html to pdf correctly since when using itext sometimes you can not process the html or images correctly

Step 2: Creating a PDF generator utility class

Next, we will create a utility class called PdfGenerator that will handle the conversion of HTML to PDF. This class will have a static method called generatePDFFromHTML that takes two parameters: the inputStream and outputStream.

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import java.io.InputStream;
import java.io.OutputStream;

public class PdfGenerator {

public static void generatePDFFromHTML(InputStream inputStream, OutputStream outputStream)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document, inputStream);
document.close();
}
}

In this example, we have defined a static method generatePDFFromHTML that takes two parameters: the HTML content as a string, and the base path for images as a string. This method creates a new Document object with the page size set to PageSize.LETTER, and a new ByteArrayOutputStream to hold the generated PDF. We then create a new instance of PdfWriter and open the document. We use the HTMLWorker class to parse the HTML content and add it to the document. Finally, we close the document and return the generated PDF as a byte array.

Step 3: Creating a service class and REST controller

Now that we have our PDF generator utility class, we can create a service class and REST controller to expose it as a RESTful web service.

First, we will create a service class called PdfService that will have a single method called generatePDFFromHTML, which takes two parameters: the HTML content as a string, and the base path for images as a string. This method will simply call the generatePDFFromHTML method of our PdfGenerator class and return the generated PDF as a byte array.

import org.springframework.stereotype.Service;
import java.io.IOException;

@Service
public class PdfService {

public byte[] generatePDFFromHTML(String html) throws DocumentException, IOException {
//generating bytes array
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfGenerator.generatePDFFromHTML(new ByteArrayInputStream(html.getBytes()), outputStream);
return outputStream.toByteArray();
}
}

Next, we will create a REST controller called PdfController that will expose the generatePDFFromHTML method of our PdfService as a RESTful web service.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;

@RestController
public class PdfController {

@Autowired
private PdfService pdfService;

@PostMapping(value = "/convert", consumes = MediaType.TEXT_HTML_VALUE, produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> convertToPDF(@RequestBody String html) throws IOException, DocumentException {
byte[] bytes = pdfService.generatePDFFromHTML(html);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
headers.setContentDispositionFormData("filename", "output.pdf");
headers.setContentLength(bytes.length);
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
}
}

In this example, we have defined a REST controller called PdfController that is annotated with @RestController. We have also defined a single endpoint called /convert, which accepts HTML content as a request body and the base path for images as a request parameter. The endpoint returns the generated PDF as a byte array with a media type of application/pdf.

Step 4: Testing our API in Postman

Mark as HTML text input in our Raw Body

Response:

Conclusion

In this tutorial, we have learned how to convert HTML to PDF using Java and the iText library. We have created a PDF generator utility class that can handle HTML content and images, and we have exposed it as a RESTful web service using a service class and controller. This example provides a good starting point for anyone looking to convert HTML to PDF using Java.

If you liked leave a round of applause or subscribe for more interesting topics and from an easy and fast perspective

--

--

Daniel Angel
Daniel Angel

Written by Daniel Angel

Backend developer | Java, Spring boot, Cloud| Laravel, Php

No responses yet