Printing¶
A library for using Compose to generated printed documents, using Android's printing services.
Gradle¶
dependencies {
implementation "com.halilibo.compose-richtext:printing:${richtext_version}"
}
Usage¶
There are multiple entry points into this library. See their kdoc for usage and parameter documentation, and take a look at the samples for example code.
Printable
¶
This is the simplest entry point. It's a composable function that displays its children on screen,
but can also print itself. Printing is triggered by the PrintableController
passed to Printable
. PrintableController
is a hoisted state type, just like ScrollState
,
created by calling rememberPrintableController
.
val printController = rememberPrintableController()
Printable(printController) {
ScrollableColumn {
Card { … }
Card { … }
Button(onClick = { printController.print("sales report") }) { … }
}
}
ComposePrintAdapter
¶
This is a PrintDocumentAdapter
that can be used directly with Android's printing APIs to print any composable function. It takes,
at minimum, the ComponentActivity
that owns the print adapter (as required by Android's printing
framework), a string name for the document, and the composable function that defines the content to
print. See the linked API documentation for more details.
Paged
¶
This is another composable, but doesn't actually have anything to do with printing.
Conceptually it's similar to a ScrollableColumn
– it lays its contents out at full height, then
can display them at various vertical offsets. However, it also tries to ensure that no composables
are clipped at the bottom, by measuring where all the leaf composables (those without any
children) are located clipping the content before them. It is used by the printing APIs to try to
ensure that composable content looks decent when split into printer pages.
See the PagedSample
for more information.
Demo¶
The DocumentSample
tries to match the style of one of the Google Docs templates. It looks great
on small phone screens, but also prints: