Whether you’re building a server-side application, desktop tool, multiplatform project, or an Android app, working with PDF documents is extremely common. Developers often need a reliable Kotlin PDF library to create, convert, read, render, and save PDF files.
Because Kotlin runs on the JVM, Android, and Kotlin Multiplatform, there isn’t a single “official” PDF API. Instead, developers rely on mature and battle-tested Java PDF libraries, which work perfectly with Kotlin thanks to excellent interoperability. These libraries support PDF generation, text extraction, image handling, page rendering, and more.
This guide covers four of the best Kotlin-friendly PDF libraries—including one Android-specific renderer—along with code examples, supported features, dependencies, and ideal use cases.
1. Apache PDFBox – Best Free Open-Source Kotlin PDF Library
Apache PDFBox is one of the most popular free PDF libraries available for JVM languages. It works seamlessly with Kotlin and provides powerful APIs for generating, reading, and editing PDF documents.
Why Developers Choose PDFBox
- 100% free under Apache License 2.0
- Great for reading and writing PDF files
- Active community with frequent releases (mar, jun, etc.)
- Easy to add as a Gradle/Maven dependency
- Supports images (PNG, JPG), embedded fonts, landscape pages, and multi-page PDFs
- Handles text selection, URLs, input/output streams
- Converts PDF pages into images (PNG/JPG)
Gradle Dependency
dependencies {
// Current stable version is 3.0.x which requires Java 8
implementation("org.apache.pdfbox:pdfbox:3.0.6")
}
Basic Kotlin Example – Create a PDF
val document = PDDocument()
val page = PDPage()
document.addPage(page)
val content = PDPageContentStream(document, page)
content.beginText()
content.setFont(PDType1Font.HELVETICA_BOLD, 16f)
content.newLineAtOffset(50f, 700f)
content.showText("Hello from Kotlin using Apache PDFBox!")
content.endText()
content.close()
document.save("output.pdf")
document.close()
Use Cases
- Server-side PDF generation
- Reading and processing existing PDF content
- Extracting text or images from PDFs
- Rendering PDF pages into PNG format
2. OpenPDF – Lightweight & Open-Source Kotlin PDF Library
OpenPDFis a highly reliable open-source library for generating and editing PDF documents. It’s a community-driven fork of the original iText 2.x and is widely used due to its simplicity and permissive LGPL/MPL licensing.
Why Choose OpenPDF
- Very easy to use for simple PDF generation
- LGPL/MPL license—safe for commercial use
- Stable API reference with helpful documentation
- Includes support for tables, fonts, page layout, and styled content
- Ideal for templated documents and invoice systems
Gradle Dependency
dependencies {
implementation("com.github.librepdf:openpdf:1.3.40")
}
Kotlin Example – Generate a PDF Document
val doc = Document()
PdfWriter.getInstance(doc, FileOutputStream("openpdf_example.pdf"))
doc.open()
doc.add(Paragraph("PDF generated in Kotlin using OpenPDF"))
doc.close()
Best For
- Lightweight server applications
- Simple PDF builders
- Multi-page documents, receipts, reports
- PDF generation without complex requirements
3. iText Core – Enterprise-Grade Java/Kotlin PDF Generation (Paid / AGPL)
When you need enterprise-level PDF functionality, iText is one of the most advanced and feature-rich options available. It supports a wide range of professional PDF workflows, analytics, and compliance features.
Key Features
- High-quality PDF generation
- Advanced table and layout engine
- Supports HTML-to-PDF conversion
- Digital signatures, encryption, stamping
- Barcodes, forms (AcroForms), and PDF/A
- Strong documentation and API reference
Gradle Dependency
dependencies {
implementation("com.itextpdf:itext7-core:8.0.3")
}
Kotlin Example
val writer = PdfWriter("itext_output.pdf")
val pdfDoc = PdfDocument(writer)
val document = Document(pdfDoc)
document.add(Paragraph("Kotlin PDF library example using iText."))
document.close()
Ideal For
- Enterprise document generation
- Secure and compliant PDF documents
- Digital signatures & encryption workflows
- Projects needing HTML/CSS rendering into PDF
4. PdfRenderer (Android Only) – Native Android PDF Viewer/Renderer
For developers building Android applications, PdfRenderer is the best built-in solution for rendering and displaying PDF pages on the device. It converts PDF pages into bitmaps, making it ideal for in-app PDF viewers.
Features
- Native PDF viewer designed for Android
- Simple API: open file → open page → render to bitmap
- Works offline, no third-party dependencies
- Supports portrait and landscape
- Perfect for apps requiring embedded PDF previews
Kotlin Example – Render a PDF Page as Bitmap
val file = File(context.cacheDir, "sample.pdf")
val renderer = PdfRenderer(
ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
)
val page = renderer.openPage(0)
val bitmap = Bitmap.createBitmap(
page.width,
page.height,
Bitmap.Config.ARGB_8888
)
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
page.close()
renderer.close()
Perfect For
- Android apps with PDF viewers
- Offline PDF reading
- Rendering PDF pages into images (PNG, JPG)
Comparison: Which Kotlin PDF Library Should You Use?
| Feature / Need | Best Library | Platform | License |
|---|---|---|---|
| Free & Open-Source PDF generation/editing | Apache PDFBox | JVM, Android (via JVM) | Apache 2.0 |
| Simple document creation & styling | OpenPDF | JVM, Android (via JVM) | LGPL/MPL |
| Enterprise-grade security/signatures | iText | JVM, Android (via JVM) | AGPLv3 / Commercial |
| Android PDF viewer or renderer | PdfRenderer | Android Native | Android SDK |
| HTML → PDF conversion | iText (pdfHTML) or OpenPDF (openpdf-html) | JVM, Android (via JVM) | AGPL/Commercial or LGPL/MPL |
| Extracting text/images | Apache PDFBox | JVM, Android (via JVM) | Apache 2.0 |
Conclusion
Choosing the right Kotlin PDF library depends on your platform—JVM, server, multiplatform, or Android—and the complexity of the PDF documents you need to generate or process. Thanks to Kotlin’s strong interoperability with Java, you can implement robust PDF creation, rendering, and file manipulation using proven libraries such as Apache PDFBox, OpenPDF, and iText, while PdfRenderer covers Android-specific PDF display needs.




Top comments (0)