PDF Generation in Kotlin: Library Overview and Practical Examples
Kotlin, as a modern language for the JVM, provides developers with powerful tools for working with PDF documents. Thanks to full compatibility with the Java ecosystem, you can use time-tested libraries such as iText and Apache PDFBox, as well as modern Kotlin frameworks. In this article, we will review the main approaches to PDF generation in Kotlin, provide code examples, and compare popular solutions.
Why Kotlin for Working with PDF?
Kotlin offers several advantages over Java when working with PDF:
- Concise syntax — less boilerplate code for creating documents
- Null-safety — reduces the risk of errors when working with nullable fields
- Extension functions — allow extending library functionality
- DSL builders — ideal for describing PDF structure
- Full compatibility — all Java libraries work without changes
Main Libraries for PDF Generation
1. iText (iText 7 / iText 8)
iText is the most popular commercial library for working with PDF. It is accessible in Kotlin via Java interop. Includes modules:
kernel— basic PDF operationslayout— high-level document constructionhtml2pdf— HTML to PDF conversionpdfa— PDF/A support
Example of creating a simple PDF with iText 7 in Kotlin:
import com.itextpdf.kernel.pdf.PdfDocumentimport com.itextpdf.kernel.pdf.PdfWriterimport com.itextpdf.layout.Documentimport com.itextpdf.layout.element.Paragraphimport java.io.FileOutputStream
fun createSimplePdf() { val writer = PdfWriter(FileOutputStream("hello_kotlin.pdf")) val pdfDoc = PdfDocument(writer) val document = Document(pdfDoc) document.add(Paragraph("Hello, Kotlin!")) document.add(Paragraph("This is a PDF created with iText 7")) document.close() println("PDF successfully created")}2. Apache PDFBox
Apache PDFBox is a free, open-source library. It is well-suited for basic tasks: creating, editing, and extracting text. In Kotlin, it is used via the Java API.
Example with PDFBox:
import org.apache.pdfbox.pdmodel.PDDocumentimport org.apache.pdfbox.pdmodel.PDPageimport org.apache.pdfbox.pdmodel.PDPageContentStreamimport org.apache.pdfbox.pdmodel.font.PDType1Fontimport java.io.File
fun createPdfWithBox() { val document = PDDocument() val page = PDPage() document.addPage(page) val contentStream = PDPageContentStream(document, page) contentStream.beginText() contentStream.setFont(PDType1Font.HELVETICA_BOLD, 14f) contentStream.newLineAtOffset(50f, 700f) contentStream.showText("PDFBox + Kotlin = ❤") contentStream.endText() contentStream.close() document.save(File("pdfbox_example.pdf")) document.close() println("Document saved")}3. Kotlin DSL for PDF (KPDF)
KPDF is a native Kotlin library using DSL builders. It allows you to describe a document in a declarative style:
// Example with a hypothetical DSL (adapted for real libraries)fun generateInvoice() { pdfDocument { page { paragraph("Invoice #123") { fontSize = 24 bold() } table { row { cell("Product"); cell("Price") } row { cell("Kotlin consultation"); cell("5000 RUB") } } } }.save("invoice.pdf")}