Generating PDF Reports in Kotlin: Library Overview and Practical Examples
Kotlin, as a modern language for the JVM, provides developers with powerful tools for creating PDF documents. Whether you are creating financial reports, invoices, analytical dashboards, or simply exporting data, Kotlin has several mature libraries that allow you to accomplish this task. In this article, we will review the main libraries for PDF generation in Kotlin, compare their capabilities, and provide practical code examples.
1. iText 7 — A Powerful and Flexible Tool
iText is one of the most popular libraries for working with PDF in the Java and Kotlin world. It provides full control over the document content: text, tables, images, annotations, bookmarks, and much more. iText 7 is a redesigned version with an improved architecture.
Installation (Gradle)
implementation("com.itextpdf:itext7-core:8.0.2")Example: Creating a Simple PDF Report
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 java.io.FileOutputStream
fun generateReport(fileName: String, data: List<Pair<String, Int>>) { val writer = PdfWriter(FileOutputStream(fileName)) val pdf = PdfDocument(writer) val document = Document(pdf)
// Title document.add(Paragraph("Sales Report").setFontSize(20).setBold())
// Table val table = Table(2) table.addCell("Product") table.addCell("Quantity") data.forEach { (product, quantity) -> table.addCell(product) table.addCell(quantity.toString()) } document.add(table)
document.close() println("PDF created: $fileName")}
// Usagefun main() { val salesData = listOf( "Laptops" to 150, "Monitors" to 230, "Keyboards" to 340 ) generateReport("report_itext.pdf", salesData)}iText provides a rich API for formatting: you can set fonts, colors, margins, alignment, add images, and even embed fonts (e.g., Cyrillic). However, iText has licensing restrictions: commercial use requires purchasing a license (AGPL for open-source projects).
2. Apache PDFBox — A Free Alternative
Apache PDFBox is an open-source library (Apache 2.0 license) that has no restrictions on commercial use. PDFBox allows you to create PDF from scratch, fill forms, extract text, and even work with digital signatures.
Installation (Gradle)
implementation("org.apache.pdfbox:pdfbox:3.0.1")Example: Creating a PDF with a Table
import org.apache.pdfbox.pdmodel.PDDocumentimport org.apache.pdfbox.pdmodel.PDPageimport org.apache.pdfbox.pdmodel.PDPageContentStreamimport org.apache.pdfbox.pdmodel.font.PDType1Fontimport java.awt.Color
fun createPdfBoxReport(fileName: String, data: List<Pair<String, Int>>) { val document = PDDocument() val page = PDPage() document.addPage(page)
val contentStream = PDPageContentStream(document, page) contentStream.beginText() contentStream.setFont(PDType1Font.HELVETICA_BOLD, 18f) contentStream.newLineAtOffset(50f, 750f) contentStream.showText("Sales Report (PDFBox)") contentStream.endText()
// Draw the table manually var y = 700f contentStream.setFont(PDType1Font.HELVETICA, 12f) contentStream.setNonStrokingColor(Color.BLACK) data.forEach { (product, quantity) -> contentStream.beginText() contentStream.newLineAtOffset(50f, y) contentStream.showText("$product — $quantity pcs.") contentStream.endText()