Skip to content

Commit

Permalink
Merge pull request #7 from Lilytreasure/pdfgendev
Browse files Browse the repository at this point in the history
iOS  signature embedding
  • Loading branch information
Lilytreasure authored Dec 3, 2024
2 parents 4914f0b + b78d3dc commit 8f3aff6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions composeApp/src/iosMain/kotlin/PdfPreview.ios.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ actual fun savePdfDoc(fileLocation: (url: String) -> Unit): Launcher {
pdfretrived?.lastname?: "",
pdfretrived?.email?: "",
pdfretrived?.fileName?: "",
signature = pdfretrived?.signature,
context = IOSPlatformContext,
fileSavedStatus = { location ->
fileLocation(location)
Expand Down
10 changes: 6 additions & 4 deletions composeApp/src/iosMain/kotlin/PdfUtil.ios.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ actual object PdfUtil {
signature: ByteArray?,
fileSavedStatus: (url: String) -> Unit
): String {

val pdf2 = PDFHelper()
val people = listOf(
mapOf("firstname" to firstname, "lastname" to lastname, "email" to email)
)
val uiImageData = pdf2.byteArrayToUIImageWithSignature(signature?.map { it.toUByte() })
val pdfData =
pdf2.generatePDFFrom(people) // Ensure this matches the function in your Swift code
pdf2.generatePDFFrom(
people,
uiImageData
) // Ensure this matches the function in your Swift code
if (pdfData != null) {
// Save the PDF
val savedURL =
Expand All @@ -34,7 +37,7 @@ actual object PdfUtil {
return "Error: Failed to generate PDF."
}

//Sample to show different ways to handle cinterop in Local directory
//Sample to show different ways to handle cinterop in Local directory
// val pdfHelper = PdfController() // Ensure PdfController is correctly implemented and accessible
// // Create a list of people
// val people = listOf(
Expand All @@ -55,6 +58,5 @@ actual object PdfUtil {
}



@Composable
actual fun getPlatformContext(): PlatformContext = IOSPlatformContext
49 changes: 43 additions & 6 deletions iosApp/iosApp/pdf/PdfHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import PDFKit
import SwiftUI


@objc public class PDFHelper: NSObject {
@objc public override init() {
super.init()
}
@objc public func generatePDF(from people: [[String: String]]) -> Data? {
super.init()
}

@objc public func generatePDF(from people: [[String: String]], signature: UIImage?) -> Data? {
let pageWidth: CGFloat = 612
let pageHeight: CGFloat = 792
let margin: CGFloat = 50
Expand All @@ -24,6 +24,7 @@ import SwiftUI
let pdfRenderer = UIGraphicsPDFRenderer(bounds: CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight))
let rowHeight: CGFloat = 30
let columnWidths: [CGFloat] = [contentWidth * 0.3, contentWidth * 0.3, contentWidth * 0.4]
let spacingAboveSignature: CGFloat = 40

let data = pdfRenderer.pdfData { context in
context.beginPage()
Expand Down Expand Up @@ -79,18 +80,44 @@ import SwiftUI
currentY += rowHeight
}
}

// Add space above the signature
currentY += spacingAboveSignature

// Add "Signature" label and image
let signatureLabel = "Signature"
let signatureAttributes: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 14, weight: .medium)
]
let labelWidth = (signatureLabel as NSString).size(withAttributes: signatureAttributes).width
let gap: CGFloat = 20

if let signatureImage = signature {
let scaleFactor: CGFloat = 0.3
let scaledWidth = signatureImage.size.width * scaleFactor
let scaledHeight = signatureImage.size.height * scaleFactor
let totalWidth = labelWidth + gap + scaledWidth

// Center align label and signature
let startX = (pageWidth - totalWidth) / 2
(signatureLabel as NSString).draw(at: CGPoint(x: startX, y: currentY), withAttributes: signatureAttributes)

let signatureX = startX + labelWidth + gap
let signatureY = currentY - scaledHeight / 2
signatureImage.draw(in: CGRect(x: signatureX, y: signatureY, width: scaledWidth, height: scaledHeight))
}
}

return data
}

@objc public func savePDF(data: Data, fileName: String) -> URL? {
let fileManager = FileManager.default
guard let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
return nil
}
let fileURL = documentDirectory.appendingPathComponent("\(fileName).pdf")

do {
try data.write(to: fileURL)
return fileURL
Expand All @@ -99,6 +126,16 @@ import SwiftUI
return nil
}
}

@objc public func byteArrayToUIImage(signature: [UInt8]?) -> UIImage? {
guard let signatureData = signature else { return nil }

// Convert ByteArray to NSData
let nsData = NSData(bytes: signatureData, length: signatureData.count)

// Create UIImage from NSData
return UIImage(data: nsData as Data)
}
}


Expand Down

0 comments on commit 8f3aff6

Please sign in to comment.