Java PDF Report Generation: Libraries and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Java PDF Report Generation: Library Overview and Code Examples

Creating PDF documents is a common task in enterprise Java applications: from generating invoices and acts to building complex analytical reports with charts. In this article, we will look at three main libraries for PDF generation in Java — iText, Apache PDFBox, and JasperReports — compare their capabilities and provide working code examples.



1. iText — Powerful and Flexible Library

iText (currently iText 8.x) is one of the most popular libraries for working with PDF in Java. It supports creating documents from scratch, editing existing ones, adding images, tables, bookmarks, watermarks, as well as working with forms and digital signatures.

Licensing: iText is distributed under a dual license — AGPL (free for open-source projects) and commercial (for proprietary software).



Example: Creating a Simple PDF Document with Text and a Table

import com.itextpdf.kernel.pdf.PdfDocument;import com.itextpdf.kernel.pdf.PdfWriter;import com.itextpdf.layout.Document;import com.itextpdf.layout.element.Paragraph;import com.itextpdf.layout.element.Table;import com.itextpdf.layout.element.Cell;

public class ItextPdfExample { public static void main(String[] args) throws Exception { String dest = "report.pdf"; PdfWriter writer = new PdfWriter(dest); PdfDocument pdf = new PdfDocument(writer); Document document = new Document(pdf);

// Title document.add(new Paragraph("Sales Report").setBold().setFontSize(18));

// Table Table table = new Table(3); table.addCell(new Cell().add(new Paragraph("Product"))); table.addCell(new Cell().add(new Paragraph("Quantity"))); table.addCell(new Cell().add(new Paragraph("Amount"))); table.addCell("Laptop"); table.addCell("5"); table.addCell("450 000 RUB"); table.addCell("Mouse"); table.addCell("12"); table.addCell("6 000 RUB");

document.add(table); document.close(); System.out.println("PDF created!"); }}

This code creates a PDF with the title "Sales Report" and a table with three columns. iText automatically handles line breaks and page breaks.



2. Apache PDFBox — Lightweight and Free Alternative

Apache PDFBox is an open-source library (Apache 2.0 license) that allows you to create, edit, and extract text from PDF files. PDFBox requires no third-party dependencies and is well-suited for simple tasks: generating text documents, filling forms, extracting data.

Disadvantage: working with tables and complex layouts requires more manual code than in iText.



Example: Creating a PDF with Text and an Image

import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;import org.apache.pdfbox.pdmodel.PDPageContentStream;import org.apache.pdfbox.pdmodel.font.PDType0Font;import java.io.File;

public class PdfBoxExample { public static void main(String[] args) throws Exception { try (PDDocument document = new PDDocument()) { PDPage page = new PDPage(); document.addPage(page);

PDPageContentStream contentStream = new PDPageContentStream(document, page); // Loading a font (mandatory!) PDType0Font font = PDType0Font.load(document, new File("LiberationSans-Regular.ttf")); contentStream.setFont(font, 14); contentStream.beginText(); contentStream.newLineAtOffset(50, 700); contentStream.showText("Hello, PDFBox World!"); contentStream.endText(); contentStream.close();

Recommendations