Skip to content

Commit

Permalink
Merge pull request #6 from Lilytreasure/pdfgendev
Browse files Browse the repository at this point in the history
signature embedding  on android
  • Loading branch information
Lilytreasure authored Dec 3, 2024
2 parents c868a2b + b57566d commit 4914f0b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 11 deletions.
2 changes: 2 additions & 0 deletions composeApp/src/androidMain/kotlin/PdfPreview.android.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
58 changes: 51 additions & 7 deletions composeApp/src/androidMain/kotlin/PdfUtil.android.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
}
}

Expand Down
1 change: 1 addition & 0 deletions composeApp/src/commonMain/kotlin/PdfUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ expect object PdfUtil {
email: String,
fileName: String,
context: PlatformContext,
signature: ByteArray?,
fileSavedStatus:(url: String)->Unit
): String
}
3 changes: 2 additions & 1 deletion composeApp/src/commonMain/kotlin/home/HomeContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ fun HomeContent(
firstname = firstName,
lastname = lastName,
email,
fileName
fileName,
signature = null
)
)
if (loc.isNotBlank()){
Expand Down
4 changes: 3 additions & 1 deletion composeApp/src/commonMain/kotlin/receipt/ReceitContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import composables.EntriesText
import composables.SignAction
import kotlinx.coroutines.launch
import savePdfDoc
import toByteArray
import utils.CustomSnackBar

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -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()) {
Expand Down
32 changes: 30 additions & 2 deletions composeApp/src/commonMain/kotlin/savePdfDoc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions composeApp/src/iosMain/kotlin/PdfUtil.ios.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ actual object PdfUtil {
email: String,
fileName: String,
context: PlatformContext,
signature: ByteArray?,
fileSavedStatus: (url: String) -> Unit
): String {

Expand Down

0 comments on commit 4914f0b

Please sign in to comment.