Generating PDF Reports in Java: Overview of Libraries and Tools
Creating PDF documents is one of the most common tasks in corporate Java development. Reports, invoices, waybills, analytical dashboards — all of this requires flexible and performant tooling. In this article, we will review four key libraries for generating PDF in Java: Apache PDFBox, iText, JasperReports, and OpenPDF. We will examine their strengths, code examples, and use cases.
1. Apache PDFBox — Low-Level Creation and Editing
Apache PDFBox is an open-source library (Apache 2.0 license) that allows you to create PDF documents from scratch, as well as edit, extract text, and fill forms. The main advantage is full control over the content: you work directly with the document objects.
Example of creating a simple PDF:
import org.apache.pdfbox.pdmodel.*;import org.apache.pdfbox.pdmodel.font.PDType1Font;import java.io.IOException;
public class PdfBoxExample { public static void main(String[] args) throws IOException { try (PDDocument document = new PDDocument()) { PDPage page = new PDPage(); document.addPage(page);
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) { contentStream.setFont(PDType1Font.HELVETICA_BOLD, 14); contentStream.beginText(); contentStream.newLineAtOffset(100, 700); contentStream.showText("Hello, PDFBox World!"); contentStream.endText(); }
document.save("output.pdf"); System.out.println("PDF created!"); } }}When to use: if you need fine-grained control over every element (for example, for generating complex charts or filling forms). Not suitable for large volumes of text without pre-layout.
2. iText — Commercial Power for Complex Reports
iText is the de facto standard in the industry. It exists in two versions: iText 7 Community (AGPL) and iText 7 Commercial (commercial license). The library supports HTML-to-PDF, creating interactive forms, digital signatures, encryption, and much more.
Example of generating a PDF with a table:
import com.itextpdf.kernel.pdf.*;import com.itextpdf.layout.*;import com.itextpdf.layout.element.*;
public class ITextExample { public static void main(String[] args) throws Exception { PdfWriter writer = new PdfWriter("report.pdf"); PdfDocument pdfDoc = new PdfDocument(writer); Document document = new Document(pdfDoc);
document.add(new Paragraph("Monthly Report")); Table table = new Table(2); table.addCell("Indicator"); table.addCell("Value"); table.addCell("Revenue"); table.addCell("1,250,000 RUB"); table.addCell("Expenses"); table.addCell("890,000 RUB"); document.add(table);
document.close(); System.out.println("Report ready!"); }}When to use: for commercial projects where high performance, support for complex layouts, and legally significant documents (signatures, encryption) are required. Note: the AGPL version requires opening the code upon distribution.
3. JasperReports — Template-Based Generation with JRXML
JasperReports is not just a library, but a whole ecosystem for creating reports. You describe the template in XML format (or via the visual editor Jaspersoft Studio), and the library fills it with data from databases, collections, or JSON. The result can be exported to PDF, Excel, HTML, CSV, and other formats.
Example of programmatically filling a template:
import net.sf.jasperre Also in library
Working with Dates in Java: Overview of java.time, Joda-Time, and Apache Commons Libraries
Logging for Swift (iOS/macOS): Best Solutions
Internationalization (i18n) in TypeScript: i18next Library