PDF Generation in Kotlin: Library Overview and Practical Examples
Generating PDF documents is a common task in modern applications: from creating reports and invoices to generating certificates and e-books. Kotlin, thanks to its conciseness and compatibility with Java, provides developers with access to powerful Java libraries for working with PDF. In this article, we will look at three main libraries: iText, Apache PDFBox, and OpenPDF, and provide code examples in Kotlin.
1. iText — Powerful and Flexible Library
iText is one of the most popular libraries for creating and manipulating PDF documents on the JVM. It supports complex formatting, tables, images, watermarks, encryption, and much more. For Kotlin, the official iText7 wrapper with Kotlin extensions is available.
Setup (Gradle)
dependencies { implementation("com.itextpdf:itext7-core:7.2.5") implementation("com.itextpdf:itext7-kotlin-extensions:1.0.0")}Example: Creating a Simple PDF with Text
import com.itextpdf.kernel.pdf.PdfDocumentimport com.itextpdf.kernel.pdf.PdfWriterimport com.itextpdf.layout.Documentimport com.itextpdf.layout.element.Paragraph
fun createSimplePdf(fileName: String) { val writer = PdfWriter(fileName) val pdf = PdfDocument(writer) val document = Document(pdf)
document.add(Paragraph("Hello, PDF world in Kotlin!")) document.add(Paragraph("This is an example of using iText7."))
document.close() println("PDF created: $fileName")}
fun main() { createSimplePdf("example_itext.pdf")}Example: Table with Data
import com.itextpdf.layout.element.Tableimport com.itextpdf.layout.element.Cell
fun createTablePdf(fileName: String) { val writer = PdfWriter(fileName) val pdf = PdfDocument(writer) val document = Document(pdf)
val table = Table(3) // 3 columns table.addHeaderCell("Name") table.addHeaderCell("Age") table.addHeaderCell("City")
table.addCell("Anna") table.addCell("28") table.addCell("Moscow")
table.addCell("Ivan") table.addCell("35") table.addCell("Saint Petersburg")
document.add(table) document.close() println("PDF with table created: $fileName")}2. Apache PDFBox — Lightweight and Free Alternative
Apache PDFBox is an open-source library (Apache 2.0 license). It is ideal for basic tasks: creating documents, extracting text, filling forms. Unlike iText, PDFBox does not require license fees for commercial use.
Setup (Gradle)
dependencies { implementation("org.apache.pdfbox:pdfbox:2.0.27")}Example: Creating a PDF with Text
import org.apache.pdfbox.pdmodel.PDDocumentimport org.apache.pdfbox.pdmodel.PDPageimport org.apache.pdfbox.pdmodel.PDPageContentStreamimport org.apache.pdfbox.pdmodel.font.PDType1Font
fun createPdfWithPdfBox(fileName: String) { val document = PDDocument() val page = PDPage() document.addPage(page)
val contentStream = PDPageContentStream(document, page) contentStream.beginText() contentStream.setFont(PDType1Font.HELVETICA_BOLD, 14f) contentStream.newLineAtOffset(100f, 700f) contentStream.showText("PDFBox + Kotlin = great!") contentStream.endText() contentStream.close()
document.save(fileName) document.close() println("PDF created: $fileName")}Example: Adding an Image
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject
fun createPdfWithImage(fileName: String, imagePath: String) { val document = PDDocument() val Also in library
RestSharp for C# (.NET): Complete Guide with Examples
ORM for Kotlin: Exposed vs Ktor vs Ktorm — Comparison and Examples
OAuth client for Kotlin: integration with providers via ktor-client and kotlinx-serialization
Swift Actors Guide: Complete Tutorial with Examples