Generating PDF Reports in Kotlin: iText, Apache PDFBox, OpenPDF

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

Generating PDF Reports in Kotlin: Library Overview and Practical Examples



Kotlin, being a modern language for the JVM, is well-suited for creating server applications, including generating complex PDF reports. Unlike Python, where ReportLab or FPDF are popular, the Kotlin/Java ecosystem uses time-tested libraries: iText, Apache PDFBox, and OpenPDF. In this article, we will break down each of them, show code examples in Kotlin, and provide recommendations for choosing one.



1. iText (versions 7 and 8)

iText is the de facto standard for PDF generation on the JVM. The library provides a powerful API for creating documents from scratch, working with forms, watermarks, and digital signatures. However, starting from version 7, iText switched to the AGPL license (or commercial), which is important to consider for commercial use.



Example of creating a simple PDF with a table in Kotlin:

import com.itextpdf.kernel.pdf.PdfDocumentimport com.itextpdf.kernel.pdf.PdfWriterimport com.itextpdf.layout.Documentimport com.itextpdf.layout.element.Paragraphimport com.itextpdf.layout.element.Tableimport com.itextpdf.layout.property.UnitValue

fun generateReportItext(outputPath: String) { val writer = PdfWriter(outputPath) val pdf = PdfDocument(writer) val document = Document(pdf)

// Title document.add(Paragraph("Sales Report").setFontSize(20))

// Table val table = Table(UnitValue.createPercentArray(3)).useAllAvailableWidth() table.addCell("Product") table.addCell("Quantity") table.addCell("Price") table.addCell("Laptop") table.addCell("10") table.addCell("75000") table.addCell("Monitor") table.addCell("5") table.addCell("25000")

document.add(table) document.close() println("PDF created: $outputPath")}


2. Apache PDFBox

PDFBox is a free library (Apache 2.0 license) that allows not only creating PDFs but also extracting text, filling forms, and rendering pages to images. The API is slightly more low-level than iText, but gives full control over the content.



Example with an image and text formatting:

import org.apache.pdfbox.pdmodel.PDDocumentimport org.apache.pdfbox.pdmodel.PDPageimport org.apache.pdfbox.pdmodel.PDPageContentStreamimport org.apache.pdfbox.pdmodel.font.PDType0Fontimport org.apache.pdfbox.pdmodel.graphics.image.PDImageXObjectimport java.io.File

fun generateReportPdfBox(outputPath: String) { val document = PDDocument() val page = PDPage() document.addPage(page)

val contentStream = PDPageContentStream(document, page) // Load font (TrueType) val font = PDType0Font.load(document, File("fonts/DejaVuSans.ttf")) contentStream.setFont(font, 14f)

// Text contentStream.beginText() contentStream.newLineAtOffset(50f, 750f) contentStream.showText("Report for January 2025") contentStream.endText()

// Image (logo) val image = PDImageXObject.createFromFile("logo.png", document) contentStream.drawImage(image, 50f, 650f, 100f, 50f)

contentStream.close() document.save(outputPath) document.close() println("PDFBox report ready: $outputPath")}


3. OpenPDF

OpenPDF is a fork of an older version of iText (LGPL) that remains completely free. It is ideal for simple reports: text, tables, basic graphic primitives. The API resembles iText 2.x, but this is not a problem in modern Kotlin code.



Example of creating a PDF with paragraphs and a list:

import com.lowagie.text.Documentimport com.lowagie.text.Paragraphimport com.lowagie.text.pdf.PdfWriterimport java.io.FileOutputStream

fun generateReportOpenPdf(outputPath: String) {

Recommendations