PDF Generation in Java: Overview of the Best Libraries
Generating PDF documents is one of the typical tasks in corporate Java development. Reports, invoices, contracts, certificates — all of these are convenient to generate programmatically. In this article, we will look at the three most popular libraries for working with PDF in Java: iText, Apache PDFBox, and OpenPDF. We will analyze their capabilities, features, and provide practical code examples.
1. iText — Powerful and Commercial Standard
iText is the de facto standard for creating PDFs in Java. The library has existed since 2000 and offers the widest range of functionality: from simple text to complex forms, watermarks, digital signatures, and working with PDF/A.
Licensing: iText is distributed under a dual license. For open-source projects (AGPL) it is free; for commercial use, a license purchase is required.
Example: Creating a Simple PDF with a Table
import com.itextpdf.kernel.pdf.PdfDocument;import com.itextpdf.kernel.pdf.PdfWriter;import com.itextpdf.layout.Document;import com.itextpdf.layout.element.Cell;import com.itextpdf.layout.element.Paragraph;import com.itextpdf.layout.element.Table;import com.itextpdf.layout.property.UnitValue;
public class ItextExample { public static void main(String[] args) throws Exception { String dest = "example_itext.pdf"; PdfWriter writer = new PdfWriter(dest); PdfDocument pdfDoc = new PdfDocument(writer); Document document = new Document(pdfDoc);
// Add a title document.add(new Paragraph("Sales Report"));
// Create a table Table table = new Table(UnitValue.createPercentArray(3)).useAllAvailableWidth(); table.addHeaderCell(new Cell().add(new Paragraph("Product"))); table.addHeaderCell(new Cell().add(new Paragraph("Quantity"))); table.addHeaderCell(new Cell().add(new Paragraph("Price")));
table.addCell(new Cell().add(new Paragraph("Laptop"))); table.addCell(new Cell().add(new Paragraph("10"))); table.addCell(new Cell().add(new Paragraph("$1200")));
table.addCell(new Cell().add(new Paragraph("Mouse"))); table.addCell(new Cell().add(new Paragraph("50"))); table.addCell(new Cell().add(new Paragraph("$25")));
document.add(table); document.close();
System.out.println("PDF created: " + dest); }}Pros of iText: huge community, excellent documentation, support for complex scenarios (signatures, forms, PDF/UA). Cons: high cost of the commercial license.
2. Apache PDFBox — Free and Open Source
Apache PDFBox is a completely free library with an Apache 2.0 license. It allows you not only to create PDFs from scratch but also to edit existing documents, extract text, work with forms and images.
Example: Creating a Document 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 org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import java.io.File;
public class PdfBoxExample { public static void main(String[] args) throws Exception { PDDocument document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Load a font (font file required) PDType0Font font = PDType0Font.load(document, new File("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf")); contentStream.setFont(font, 14); contentStream.beginText(); contentStream.newLineAtOffset(50, 700); conte