PdfDriver

interface PdfDriver

Per-document handle returned by a PdfDriverFactory.

A driver is stateful and single-use: produce pages one at a time with beginPage / endPage, then call finish exactly once to obtain the encoded PDF bytes. Calling any method after finish is undefined.

The expected lifecycle is:

val driver = factory.create(metadata, customFonts)
val canvas1 = driver.beginPage(PageSize.A4)
// ... draw on canvas1 ...
driver.endPage()
val canvas2 = driver.beginPage(PageSize.A4)
// ... draw on canvas2 ...
driver.endPage()
val bytes = driver.finish()

Drivers are not thread-safe and must be used from a single coroutine / thread.

Properties

Link copied to clipboard
abstract val fontMetrics: FontMetrics

Font measurement service backed by the same renderer the canvas uses.

Functions

Link copied to clipboard
abstract fun beginPage(size: PageSize): PdfCanvas

Starts a new page and returns a fresh PdfCanvas for it. The previous canvas (if any) becomes invalid; do not retain a reference to it.

Link copied to clipboard
open fun close()

Releases any underlying resources without producing output. Called by the renderer when rendering fails partway through (so finish is never reached) to avoid leaking native / file handles. Must be idempotent and safe to call after finish. The default is a no-op — backends whose resources are already freed by finish (or that hold none, like the Android PdfDocument / iOS CGContext backends) need not override it; the JVM PdfBox backend overrides it to close its PDDocument.

Link copied to clipboard
abstract fun endPage()

Closes the page previously opened with beginPage. Must be called before either another beginPage or finish.

Link copied to clipboard
abstract fun finish(): ByteArray

Finalises the document and returns its encoded bytes.