Generate a PDF in Kotlin for Android
Shows how to create a document programmatically using an Android canvas. Get additional resources by visiting our Android PDF generation library.
/* * Copyright © 2020-2025 PSPDFKit GmbH. All rights reserved. * * The PSPDFKit Sample applications are licensed with a modified BSD license. * Please see License for details. This notice may not be removed from this file. */
package com.pspdfkit.catalog.examples.kotlin
import android.content.Contextimport android.graphics.Canvasimport android.graphics.Paintimport android.graphics.Pathimport android.net.Uriimport android.util.Logimport com.pspdfkit.catalog.Rimport com.pspdfkit.catalog.SdkExampleimport com.pspdfkit.catalog.SdkExample.Companion.TAGimport com.pspdfkit.configuration.activity.PdfActivityConfigurationimport com.pspdfkit.document.processor.NewPageimport com.pspdfkit.document.processor.PdfProcessorimport com.pspdfkit.document.processor.PdfProcessorTaskimport com.pspdfkit.ui.PdfActivityIntentBuilderimport io.reactivex.rxjava3.android.schedulers.AndroidSchedulersimport io.reactivex.rxjava3.disposables.Disposableimport io.reactivex.rxjava3.schedulers.Schedulersimport java.io.Fileimport java.io.IOException
/** * This example shows how to create a document from [Canvas] drawing. */class DocumentFromCanvasExample(context: Context) : SdkExample(context, R.string.documentFromCanvasExampleTitle, R.string.documentFromCanvasExampleDescription) {
private var documentProcessingDisposable: Disposable? = null
override fun launchExample(context: Context, configuration: PdfActivityConfiguration.Builder) { // Turn off saving, so we have the clean original document every time the example is launched. configuration.autosaveEnabled(false)
// Create a canvas based on a A4 page. val pageCanvas = NewPage.fromCanvas(NewPage.PAGE_SIZE_A4) { canvas -> val paint = Paint().apply { style = Paint.Style.STROKE }
val path = Path().apply { cubicTo(0f, 0f, 100f, 300f, 400f, 300f) }
canvas.drawPath(path, paint) }.build()
val task = PdfProcessorTask.newPage(pageCanvas) val outputFile = try { File(getCatalogCacheDirectory(context), "Canvas.pdf").canonicalFile } catch (exception: IOException) { throw IllegalStateException("Couldn't create Canvas.pdf file.", exception) }
documentProcessingDisposable = PdfProcessor.processDocumentAsync(task, outputFile) // Ignore PdfProcessor progress. .ignoreElements() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { val intent = PdfActivityIntentBuilder.fromUri(context, Uri.fromFile(outputFile)) .configuration(configuration.build()) .build() context.startActivity(intent) }, { throwable -> Log.e(TAG, "Error while trying to create PDF document.", throwable) } ) }
override fun onDestroy() { super.onDestroy() documentProcessingDisposable?.dispose() documentProcessingDisposable = null }
companion object { private const val PSPDFKIT_DIRECTORY_NAME = "catalog-pspdfkit"
@Throws(IOException::class) private fun getCatalogCacheDirectory(ctx: Context): File { val dir = File(ctx.cacheDir, PSPDFKIT_DIRECTORY_NAME) if (!dir.exists()) { if (!dir.mkdirs()) { throw IOException("Failed to create Catalog cache directory.") } } return dir } }}
This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.