PDF Generation in Kotlin: Overview of Libraries and Practical Examples
Generating PDF documents is a common task in modern applications: from creating reports and invoices to data export. Kotlin, being a modern JVM language, provides developers with access to powerful Java libraries for working with PDF. In this article, we will review the main tools for PDF generation in Kotlin, their features, and usage examples.
Popular Libraries for Working with PDF in Kotlin
Since Kotlin is fully compatible with Java, you can use any Java library for PDF. The most popular options are:
- iText (version 7/8) — a commercial library with open source code (AGPL). Provides extensive capabilities: creating documents from scratch, working with forms, watermarks, encryption. A license purchase is required for commercial use.
- Apache PDFBox — a free library (Apache 2.0). Allows you to create PDFs, extract text, and fill forms. It works at a lower level than iText but gives full control over the content.
- OpenPDF — a fork of iText 4 (LGPL/MPL). A lightweight library for basic tasks: creating text, tables, images. Suitable for simple projects without complex layout.
- Jetpack Compose + PDF — a modern approach: you describe the UI in Compose and then convert it to PDF using Android libraries (e.g.,
android.graphics.pdf.PdfDocument). Suitable for Android applications.
Project Setup and Adding Dependencies
For demonstration, we will use iText 7 and Apache PDFBox. Add the dependencies to build.gradle.kts:
// For iText 7implementation("com.itextpdf:itext7-core:8.0.4")
// For Apache PDFBoximplementation("org.apache.pdfbox:pdfbox:3.0.2")Example 1: Creating a PDF with Text (iText 7)
Let's create a simple document with a title and a paragraph:
import com.itextpdf.kernel.pdf.PdfDocumentimport com.itextpdf.kernel.pdf.PdfWriterimport com.itextpdf.layout.Documentimport com.itextpdf.layout.element.Paragraph
fun createSimplePdf() { val writer = PdfWriter("output.pdf") val pdf = PdfDocument(writer) val document = Document(pdf)
document.add(Paragraph("Hello, world!")) document.add(Paragraph("This is a PDF created using iText in Kotlin."))
document.close()}Example 2: Working with Tables and Cyrillic (iText 7)
For correct display of Cyrillic characters, you need to load a font that supports Unicode. Use the standard FreeSans font or any TTF font:
import com.itextpdf.kernel.font.PdfFontFactoryimport com.itextpdf.kernel.pdf.PdfDocumentimport com.itextpdf.kernel.pdf.PdfWriterimport com.itextpdf.layout.Documentimport com.itextpdf.layout.element.Cellimport com.itextpdf.layout.element.Paragraphimport com.itextpdf.layout.element.Table
fun createTableWithCyrillic() { val writer = PdfWriter("table.pdf") val pdf = PdfDocument(writer) val document = Document(pdf)
// Loading a font with Cyrillic support val font = PdfFontFactory.createFont("fonts/FreeSans.ttf", "Identity-H") document.setFont(font)
val table = Table(2) // two columns table.addCell(Cell().add(Paragraph("Name"))) table.addCell(Cell().add(Paragraph("Age"))) table.addCell(Cell().add(Paragraph("Anna"))) table.addCell(Cell().add(Paragraph("25"))) table.addCell(Cell().add(Paragraph("Ivan"))) table.addCell(Cell().add(Paragraph("30")))
document.add(table) document.close()}Example 3: Creating a PDF with an Image (Apache PDFBox)
PDFBox requires more detailed work with coordinates but offers flexibility:
Also in library
Recommendations