Skip to content

A Compose Multiplatform library for exporting UI content to PDF across desktop and mobile platforms.

License

Notifications You must be signed in to change notification settings

ch4rl3x/compose-multiplatform-pdf-export

Repository files navigation

Compose Multiplatform PDF Export

Build Maven Central

A Compose Multiplatform library for exporting UI content to vector PDFs across Android, iOS, and JVM/desktop.

Features

  • Render any @Composable to a multi‑page PDF (vector output).
  • Automatic pagination based on measured height.
  • Optional item‑based pagination to keep blocks together.
  • Custom page formats (A4, Letter, Custom, etc.) and orientation.
  • Consistent output with Compose layouts and styling.

Supported Targets

  • Android (minSdk 26)
  • iOS (iosX64, iosArm64, iosSimulatorArm64)
  • JVM (desktop)

Installation

Gradle (Kotlin DSL)

kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation("de.charlex.compose:multiplatform-pdf-export:<version>")
        }
    }
}

Use the version shown in the Maven Central badge above.

Usage

1) Render a single composable (auto page breaks)

val pdfBytes = renderComposeToPdf(
    context = PdfContext(),
    format = PdfFormat.A4,
    orientation = PdfOrientation.Portrait
) {
    Column {
        Text("Hello PDF")
        // ... long content
    }
}

2) Item‑based pagination (keep blocks together)

val pdfBytes = renderComposeToPdf(
    context = PdfContext(),
    format = PdfFormat.A4,
    orientation = PdfOrientation.Portrait
) {
    item { Header() }
    items(data) { row -> RowItem(row) }
    item { Footer() }
}

Platform‑specific PdfContext

// Android
val pdfBytes = renderComposeToPdf(
    context = PdfContext(activity),
    content = { /* ... */ }
)

// iOS / JVM
val pdfBytes = renderComposeToPdf(
    context = PdfContext(),
    content = { /* ... */ }
)

How It Works (High Level)

  • Android: renders directly to android.graphics.pdf.PdfDocument.
  • iOS: renders to SVG and draws into CoreGraphics PDF context.
  • JVM: renders to SVG and converts to PDF (Batik/FOP), then merges pages (PDFBox).

Notes on Fonts

PDF output uses the Compose rendering stack. If you need identical typography across platforms, consider providing explicit fonts in your Compose theme (instead of relying on system defaults).

Sample Apps

See sample/ for Android, iOS and JVM examples.

License

MIT — see LICENSE.

About

A Compose Multiplatform library for exporting UI content to PDF across desktop and mobile platforms.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages