From b57566ded2b248f21145996ea8f6a18609c7d8bb Mon Sep 17 00:00:00 2001 From: SirDennis Date: Tue, 3 Dec 2024 16:55:39 +0300 Subject: [PATCH] signature embeding android --- .../androidMain/kotlin/PdfPreview.android.kt | 2 + .../src/androidMain/kotlin/PdfUtil.android.kt | 58 ++++++++++++++++--- composeApp/src/commonMain/kotlin/PdfUtil.kt | 1 + .../src/commonMain/kotlin/home/HomeContent.kt | 3 +- .../kotlin/receipt/ReceitContent.kt | 4 +- .../src/commonMain/kotlin/savePdfDoc.kt | 32 +++++++++- composeApp/src/iosMain/kotlin/PdfUtil.ios.kt | 1 + 7 files changed, 90 insertions(+), 11 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/PdfPreview.android.kt b/composeApp/src/androidMain/kotlin/PdfPreview.android.kt index 4c4f2cd..4191c7e 100644 --- a/composeApp/src/androidMain/kotlin/PdfPreview.android.kt +++ b/composeApp/src/androidMain/kotlin/PdfPreview.android.kt @@ -68,6 +68,7 @@ actual fun savePdfDoc(fileLocation: (url: String) -> Unit): Launcher { pdfretrived?.lastname?: "", pdfretrived?.email?: "", pdfretrived?.fileName?: "", + signature = pdfretrived?.signature, context = AndroidPlatformContext(context), fileSavedStatus = { location -> fileLocation(location) @@ -90,6 +91,7 @@ actual fun savePdfDoc(fileLocation: (url: String) -> Unit): Launcher { pdfretrived?.lastname?: "", pdfretrived?.email?: "", pdfretrived?.fileName?: "", + signature = pdfretrived?.signature, context = AndroidPlatformContext(context), fileSavedStatus = { location -> fileLocation(location) diff --git a/composeApp/src/androidMain/kotlin/PdfUtil.android.kt b/composeApp/src/androidMain/kotlin/PdfUtil.android.kt index 7289b48..d39ae21 100644 --- a/composeApp/src/androidMain/kotlin/PdfUtil.android.kt +++ b/composeApp/src/androidMain/kotlin/PdfUtil.android.kt @@ -4,6 +4,8 @@ import android.app.PendingIntent import android.content.ContentValues import android.content.Context import android.content.Intent +import android.graphics.Bitmap +import android.graphics.BitmapFactory import android.graphics.Canvas import android.graphics.Paint import android.graphics.pdf.PdfDocument @@ -30,22 +32,24 @@ actual object PdfUtil { email: String, fileName: String, context: PlatformContext, + signature: ByteArray?, fileSavedStatus: (url: String) -> Unit ): String { val androidContext = (context as AndroidPlatformContext).context val currentActivity: AppCompatActivity = (androidContext as AppCompatActivity) + // Create the PDF document val pdfDocument = PdfDocument() - val pageInfo = PdfDocument.PageInfo.Builder(595, 842, 1).create() + val pageInfo = PdfDocument.PageInfo.Builder(595, 842, 1).create() // A4 size (595x842) val page = pdfDocument.startPage(pageInfo) val canvas: Canvas = page.canvas val paint = Paint().apply { isAntiAlias = true } - // Define text dimensions and positions + // Canvas will give you the freedom to play with constraints to design the pdf as you wish + //For more Versatile form designs you could use itext val startX = 50f val startY = 50f val columnWidth = 150f val rowHeight = 50f - // Add header row paint.textSize = 16f paint.textAlign = Paint.Align.CENTER @@ -62,16 +66,56 @@ actual object PdfUtil { val textY = startY + rowHeight + rowHeight / 2 - (paint.descent() + paint.ascent()) / 2 canvas.drawText(data, textX, textY, paint) } + // Add space above the signature section + val signatureMargin = 80f // Space between user data and signature section + val signatureStartY = startY + rowHeight * 2 + signatureMargin + + // Center-align signature text and signature on the same row + val pageWidth = pageInfo.pageWidth.toFloat() + val title = "Signature" + paint.textSize = 14f + paint.textAlign = Paint.Align.LEFT + val titleWidth = paint.measureText(title) + + // Add signature next to the title + signature?.let { + // Decode the signature ByteArray to Bitmap + val signatureBitmap = BitmapFactory.decodeByteArray(it, 0, it.size) + + // Check if the bitmap is valid + signatureBitmap?.let { bitmap -> + // Scale the signature bitmap + val scaleFactor = 0.3f + val scaledWidth = (bitmap.width * scaleFactor).toInt() + val scaledHeight = (bitmap.height * scaleFactor).toInt() + val scaledBitmap = + Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true) + + // Calculate total width (text + gap + signature) + val gap = 20f // Space between the text and the signature + val totalWidth = titleWidth + gap + scaledWidth + + // Calculate the starting X position to center-align + val startX2 = (pageWidth - totalWidth) / 2 + + // Center-align the text + canvas.drawText(title, startX2, signatureStartY, paint) + + // Center-align the signature next to the text + val signatureStartX = startX2 + titleWidth + gap + val signatureBitmapStartY = + signatureStartY - scaledHeight / 2 // Adjust to align vertically with text + canvas.drawBitmap(scaledBitmap, signatureStartX, signatureBitmapStartY, paint) + } + } + // Finish the page and document pdfDocument.finishPage(page) - - // Saving the PDF using Scoped Storage val fileUri: Uri? = savePdfToStorage(currentActivity, pdfDocument, fileName) - // Notify the result fileSavedStatus(fileUri.toString()) // Return the URI of the saved file pdfDocument.close() - return fileUri.toString() // Return the URI as the file path + return fileUri.toString() } } diff --git a/composeApp/src/commonMain/kotlin/PdfUtil.kt b/composeApp/src/commonMain/kotlin/PdfUtil.kt index 5257337..f1029e2 100644 --- a/composeApp/src/commonMain/kotlin/PdfUtil.kt +++ b/composeApp/src/commonMain/kotlin/PdfUtil.kt @@ -11,6 +11,7 @@ expect object PdfUtil { email: String, fileName: String, context: PlatformContext, + signature: ByteArray?, fileSavedStatus:(url: String)->Unit ): String } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/home/HomeContent.kt b/composeApp/src/commonMain/kotlin/home/HomeContent.kt index 8c41777..810ed81 100644 --- a/composeApp/src/commonMain/kotlin/home/HomeContent.kt +++ b/composeApp/src/commonMain/kotlin/home/HomeContent.kt @@ -167,7 +167,8 @@ fun HomeContent( firstname = firstName, lastname = lastName, email, - fileName + fileName, + signature = null ) ) if (loc.isNotBlank()){ diff --git a/composeApp/src/commonMain/kotlin/receipt/ReceitContent.kt b/composeApp/src/commonMain/kotlin/receipt/ReceitContent.kt index 18f8597..a98179b 100644 --- a/composeApp/src/commonMain/kotlin/receipt/ReceitContent.kt +++ b/composeApp/src/commonMain/kotlin/receipt/ReceitContent.kt @@ -52,6 +52,7 @@ import composables.EntriesText import composables.SignAction import kotlinx.coroutines.launch import savePdfDoc +import toByteArray import utils.CustomSnackBar @OptIn(ExperimentalMaterial3Api::class) @@ -239,7 +240,8 @@ fun NotificationContent(component: ReceiptComponent, modifier: Modifier = Modifi firstname = firstName, lastname = lastName, email, - fileName + fileName, + signature = signature?.toByteArray(ImageFormat.PNG) ) ) if (loc.isNotBlank()) { diff --git a/composeApp/src/commonMain/kotlin/savePdfDoc.kt b/composeApp/src/commonMain/kotlin/savePdfDoc.kt index 40f34ac..911adc9 100644 --- a/composeApp/src/commonMain/kotlin/savePdfDoc.kt +++ b/composeApp/src/commonMain/kotlin/savePdfDoc.kt @@ -6,8 +6,36 @@ data class PdfDocData( var firstname: String, var lastname: String, var email: String, - var fileName: String -) + var fileName: String, + var signature: ByteArray?, +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other == null || this::class != other::class) return false + + other as PdfDocData + + if (firstname != other.firstname) return false + if (lastname != other.lastname) return false + if (email != other.email) return false + if (fileName != other.fileName) return false + if (signature != null) { + if (other.signature == null) return false + if (!signature.contentEquals(other.signature)) return false + } else if (other.signature != null) return false + + return true + } + + override fun hashCode(): Int { + var result = firstname.hashCode() + result = 31 * result + lastname.hashCode() + result = 31 * result + email.hashCode() + result = 31 * result + fileName.hashCode() + result = 31 * result + (signature?.contentHashCode() ?: 0) + return result + } +} @Composable expect fun savePdfDoc(fileLocation: (url: String) -> Unit): Launcher diff --git a/composeApp/src/iosMain/kotlin/PdfUtil.ios.kt b/composeApp/src/iosMain/kotlin/PdfUtil.ios.kt index daeae56..364fd5f 100644 --- a/composeApp/src/iosMain/kotlin/PdfUtil.ios.kt +++ b/composeApp/src/iosMain/kotlin/PdfUtil.ios.kt @@ -12,6 +12,7 @@ actual object PdfUtil { email: String, fileName: String, context: PlatformContext, + signature: ByteArray?, fileSavedStatus: (url: String) -> Unit ): String {