Arjun Bhat
Arjun Bhat

Reputation: 151

Is there any way to convert Composables into Pdf file?

I want to allow users to download the ticket from the app in PDF format and store it locally. I want to have exactly similar view in PDF format, that's why I am searching for way to convert composables to PDF. It will be cool to directly convert compose view into Pdf format. Is this possible to achieve?

1.) KotOrderTicket.kt

@Composable
fun KotOrderTicket(modifier: Modifier = Modifier) {
    var totalQuantity: Int = 0

    val rowModifier = Modifier
        .fillMaxWidth()

    val textCenterModifier = Modifier
        .fillMaxWidth()
        .wrapContentWidth(align = Alignment.CenterHorizontally)

    LazyColumn(
        modifier = modifier
            .fillMaxWidth(),
    ){
        item {
            KotDashedDivider()
            Text("Kitchen Order Ticket - Dinning", modifier = textCenterModifier)
            KotDashedDivider()
            Text("Running", modifier = textCenterModifier)
            KotDashedDivider()

            Row(
                modifier = rowModifier,
                horizontalArrangement = Arrangement.SpaceBetween,
            ){
                Text("Kot# 22")
                Text("Table T7")
            }
            Text("Date 2022-04-3 10: 23: 22")
            Text("Ready Till 2022-04-3 10: 23: 22")
            KotDashedDivider()

            Row(
                modifier = rowModifier,
                horizontalArrangement = Arrangement.SpaceBetween,
            ){
                Text("SN")
                Text("Item's Name")
                Text("Qty")
            }
            KotDashedDivider()
        }

        itemsIndexed(summaryItems) { index, item ->
            totalQuantity += (item.qty?.toInt() ?: 0)
            KotItemSummary(sn = index.toString(), name = item.productId.toString(), qty = item.qty.toString(), comment = "\"Nun kam rakhnu hai\"")
        }

        item {
            KotDashedDivider()
            Row(
                modifier = rowModifier,
                horizontalArrangement = Arrangement.SpaceBetween,
            ){
                Text("")
                Text("Total Qty: ")
                Text(totalQuantity.toString())
            }
            KotSpacer()
            Text("Remarks: ")
            Text("\"Please cook fast, i am so hungry\"")
        }
    }
}

2.) This view will look like this.

How to convert composeables into pdf format and save it.

Upvotes: 11

Views: 2675

Answers (1)

blacktiago
blacktiago

Reputation: 393

You could turn your composable into a bitmap following part of this tutorial then place that bitmap in a pdf using android utility class PdfDocument

Upvotes: 2

Related Questions