Creating a PDF Export API with iText PDF in Java Spring Boot

Rijul Dahiya
3 min readApr 3, 2023

--

Photo by GoodNotes on Unsplash

If you’ve ever worked on a project that required exporting query results to a file, you know how tedious it can be to manually copy and paste data into a CSV or Excel file. To save time and effort, you can create a PDF export POST API using iText PDF.

iText PDF is a Java library that allows you to create, manipulate, and extract data from PDF documents. It’s an open-source library and has a lot of features that make it a popular choice for PDF manipulation.

In this blog post, we’ll go over how to create a PDF export POST API using iText PDF in a Java Spring Boot application.

Setup

First, we need to add the iText PDF dependency to our project. 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.13.2</version>
</dependency>

Creating the PDF

Next, we need to create a utility class that will generate the PDF file. We’ll call this class PdfUtils. Here's an example implementation:

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
public class PdfUtils {
public static ByteArrayOutputStream generatePdfStream(List<Map<String, Object>> queryResults) throws DocumentException {
Document document = new Document();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfWriter.getInstance(document, outputStream);
document.open();
// Write column names
Map<String, Object> firstRow = queryResults.get(0);
for (String column : firstRow.keySet()) {
Font boldFont = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);
Paragraph paragraph = new Paragraph(column, boldFont);
document.add(paragraph);
}
document.add(new Paragraph("\n"));
// Write data rows
for (Map<String, Object> row : queryResults) {
for (Object value : row.values()) {
Paragraph paragraph = new Paragraph(value.toString());
document.add(paragraph);
}
document.add(new Paragraph("\n"));
}
document.close();
return outputStream;
}
}

This utility class creates a new Document and PdfWriter, and writes the column names and data rows to the PDF document. The generatePdfStream method returns a ByteArrayOutputStream containing the PDF file.

Controller

Now that we have our PDF generation logic in place, we need to create a controller that will generate the PDF and return it as a downloadable file. Here’s an example implementation:

import com.itextpdf.text.DocumentException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
public class MyController {
@PostMapping("/pdf")
public ResponseEntity<byte[]> exportPdf(@RequestBody QueryRequest request) throws IOException, DocumentException {
List<Map<String, Object>> queryResults = myService.executeQuery(request);
ByteArrayOutputStream pdfStream = PdfUtils.generatePdfStream(queryResults); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=query_results.pdf");
headers.setContentLength(pdfStream.size());
return new ResponseEntity<>(pdfStream.toByteArray(), headers, HttpStatus.OK);
}
}

In this controller, we take in a `QueryRequest` object as the request body, which contains the query to be executed. We then call a `myService.executeQuery` method to retrieve the results of the query.

We then pass the query results to the `PdfUtils.generatePdfStream` method, which returns a `ByteArrayOutputStream` containing the PDF file.

Finally, we set the appropriate response headers and return a `ResponseEntity` containing the PDF file as a byte array.

Conclusion

In this blog post, we went over how to create a PDF export POST API using iText PDF in a Java Spring Boot application. By using iText PDF, we were able to generate a PDF file containing query results and make it available for download.

While this implementation is relatively simple, it can be extended to include more features like custom styling, pagination, and more. With iText PDF, the possibilities are endless.

--

--

Rijul Dahiya

Founder @ TenderEarth | SDE @ Groupon | Follow me to learn more on the cultural dimensions of technology