-
Notifications
You must be signed in to change notification settings - Fork 685
feat(loan): Implementation of Export PDF functionality. #2622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
techsavvy185
wants to merge
8
commits into
openMF:development
Choose a base branch
from
techsavvy185:loanPdfExport
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,218
−460
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d0d4192
Resolve conflicts.
techsavvy185 046f03d
Resolve conflicts.
techsavvy185 d26c663
Fix
techsavvy185 27afd0a
Resolve conflicts
techsavvy185 d8585cd
Synced with latest changes and made some adjustments.
techsavvy185 a5c2092
Implemented save as pdf functionality for android and desktop.
techsavvy185 6048c17
Minor changes.
techsavvy185 eaa3fa4
Minor changes.
techsavvy185 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
core/designsystem/src/commonMain/kotlin/com/mifos/core/designsystem/component/MifosTable.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright 2026 Mifos Initiative | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * See https://github.com/openMF/android-client/blob/master/LICENSE.md | ||
| */ | ||
| package com.mifos.core.designsystem.component | ||
|
|
||
| import androidx.compose.foundation.background | ||
| import androidx.compose.foundation.clickable | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.IntrinsicSize | ||
| import androidx.compose.foundation.layout.Row | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.height | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.width | ||
| import androidx.compose.material3.HorizontalDivider | ||
| import androidx.compose.material3.VerticalDivider | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.draw.clip | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.graphics.Shape | ||
| import androidx.compose.ui.unit.Dp | ||
| import androidx.compose.ui.unit.dp | ||
| import com.mifos.core.designsystem.theme.DesignToken | ||
| import template.core.base.designsystem.theme.KptTheme | ||
|
|
||
| @Composable | ||
| fun MifosTableRow( | ||
| cells: List<@Composable () -> Unit>, | ||
| widths: List<Dp>, | ||
| modifier: Modifier = Modifier, | ||
| edgeOffset: Dp = 0.dp, | ||
| backgroundColor: Color = KptTheme.colorScheme.surfaceVariant, | ||
| cornerShape: Shape = DesignToken.shapes.none, | ||
| showTopBorder: Boolean = false, | ||
| showBottomBorder: Boolean = true, | ||
| showSideBorders: Boolean = true, | ||
| borderColor: Color = KptTheme.colorScheme.outlineVariant, | ||
| onClick: () -> Unit = {}, | ||
| ) { | ||
| Column( | ||
| modifier = modifier.fillMaxWidth(), | ||
| ) { | ||
| if (showTopBorder) { | ||
| HorizontalDivider( | ||
| modifier = Modifier | ||
| .padding(horizontal = edgeOffset), | ||
| thickness = 0.5.dp, | ||
| color = borderColor, | ||
| ) | ||
| } | ||
| Row( | ||
| modifier = Modifier | ||
| .height(IntrinsicSize.Min) | ||
| .padding(horizontal = edgeOffset) | ||
| .clip(cornerShape) | ||
| .background(backgroundColor) | ||
| .clickable { onClick() }, | ||
| ) { | ||
| cells.forEachIndexed { index, content -> | ||
| val cellWidth = widths.getOrElse(index) { DesignToken.sizes.tableCellWidthMedium } | ||
| if (showSideBorders) { | ||
| VerticalDivider( | ||
| thickness = 0.5.dp, | ||
| color = borderColor, | ||
| ) | ||
| } | ||
| Box(modifier = Modifier.width(cellWidth)) { | ||
| content() | ||
| if (showBottomBorder) { | ||
| HorizontalDivider( | ||
| modifier = Modifier.align(Alignment.BottomStart), | ||
| thickness = 0.5.dp, | ||
| color = borderColor, | ||
| ) | ||
| } | ||
| } | ||
| if (showSideBorders && index == cells.lastIndex) { | ||
| VerticalDivider( | ||
| thickness = 0.5.dp, | ||
| color = borderColor, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
core/ui/src/androidMain/kotlin/com/mifos/core/ui/util/pdf/PdfGenerator.android.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * Copyright 2026 Mifos Initiative | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * See https://github.com/openMF/android-client/blob/master/LICENSE.md | ||
| */ | ||
| package com.mifos.core.ui.util.pdf | ||
|
|
||
| import android.content.Context | ||
| import android.print.PrintAttributes | ||
| import android.print.PrintManager | ||
| import android.webkit.WebView | ||
| import android.webkit.WebViewClient | ||
| import androidx.core.content.getSystemService | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.suspendCancellableCoroutine | ||
| import kotlinx.coroutines.withContext | ||
| import kotlin.coroutines.resume | ||
| import kotlin.coroutines.resumeWithException | ||
|
|
||
| /** | ||
| * Android implementation of PDF generator using WebView print adapter. | ||
| */ | ||
| actual class PdfGenerator { | ||
|
|
||
| private var context: Context? = null | ||
|
|
||
| fun setContext(context: Context) { | ||
| this.context = context | ||
| } | ||
|
|
||
| actual suspend fun generateAndSharePdf( | ||
| htmlContent: String, | ||
| fileName: String, | ||
| pageConfig: PageConfig, | ||
| ) { | ||
| withContext(Dispatchers.Main) { | ||
| val appContext = context ?: throw Exception("Context not initialized") | ||
|
|
||
| val finalHtml = run { | ||
| val orientation = if (pageConfig.orientation == Orientation.LANDSCAPE) { | ||
| "landscape" | ||
| } else { | ||
| "portrait" | ||
| } | ||
| val size = when (pageConfig.size) { | ||
| PageSize.A4 -> "A4" | ||
| PageSize.LETTER -> "letter" | ||
| } | ||
| val pageCss = | ||
| "@page { size: $size $orientation; margin: ${pageConfig.marginMm}mm; }" | ||
| htmlContent.replace("/* PAGE_CONFIG_PLACEHOLDER */", pageCss) | ||
| } | ||
|
|
||
| var webView: WebView? = WebView(appContext) | ||
|
|
||
| try { | ||
| suspendCancellableCoroutine { continuation -> | ||
| val currentWebView = webView ?: return@suspendCancellableCoroutine | ||
|
|
||
| continuation.invokeOnCancellation { | ||
| currentWebView.stopLoading() | ||
| currentWebView.destroy() | ||
| webView = null | ||
| } | ||
|
|
||
| currentWebView.settings.javaScriptEnabled = false | ||
| currentWebView.webViewClient = object : WebViewClient() { | ||
| override fun onPageFinished(view: WebView?, url: String?) { | ||
| try { | ||
| val printManager = appContext.getSystemService<PrintManager>() | ||
| ?: throw Exception("PrintManager not available") | ||
|
|
||
| val attributes = createPrintAttributes(pageConfig) | ||
| val nativeAdapter = view?.createPrintDocumentAdapter(fileName) | ||
| ?: throw Exception("PrintDocumentAdapter is null") | ||
|
|
||
| printManager.print(fileName, nativeAdapter, attributes) | ||
|
|
||
| if (continuation.isActive) continuation.resume(Unit) | ||
techsavvy185 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (e: Exception) { | ||
| if (continuation.isActive) continuation.resumeWithException(e) | ||
| } | ||
| } | ||
|
|
||
| override fun onReceivedError( | ||
| view: WebView?, | ||
| errorCode: Int, | ||
| description: String?, | ||
| failingUrl: String?, | ||
| ) { | ||
| if (continuation.isActive) { | ||
| continuation.resumeWithException(Exception("WebView Error: $description")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| currentWebView.loadDataWithBaseURL(null, finalHtml, "text/html", "UTF-8", null) | ||
| } | ||
| } finally { | ||
| // If webView wasn't nulled by cancellation, clean it up now | ||
| webView?.let { activeView -> | ||
| activeView.post { | ||
| activeView.stopLoading() | ||
| activeView.destroy() | ||
| } | ||
| webView = null | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun createPrintAttributes(pageConfig: PageConfig): PrintAttributes { | ||
| val isLandscape = pageConfig.orientation == Orientation.LANDSCAPE | ||
| val mediaSize = when (pageConfig.size) { | ||
| PageSize.A4 -> if (isLandscape) { | ||
| PrintAttributes.MediaSize.ISO_A4.asLandscape() | ||
| } else { | ||
| PrintAttributes.MediaSize.ISO_A4 | ||
| } | ||
|
|
||
| PageSize.LETTER -> if (isLandscape) { | ||
| PrintAttributes.MediaSize.NA_LETTER.asLandscape() | ||
| } else { | ||
| PrintAttributes.MediaSize.NA_LETTER | ||
| } | ||
| } | ||
| val marginMils = (pageConfig.marginMm * 39.3701).toInt() | ||
|
|
||
| return PrintAttributes.Builder() | ||
| .setMediaSize(mediaSize) | ||
| .setColorMode(PrintAttributes.COLOR_MODE_COLOR) | ||
| .setMinMargins( | ||
| PrintAttributes.Margins( | ||
| marginMils, | ||
| marginMils, | ||
| marginMils, | ||
| marginMils, | ||
| ), | ||
| ) | ||
| .build() | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
core/ui/src/androidMain/kotlin/com/mifos/core/ui/util/pdf/PdfGeneratorHelper.android.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright 2026 Mifos Initiative | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * See https://github.com/openMF/android-client/blob/master/LICENSE.md | ||
| */ | ||
| package com.mifos.core.ui.util.pdf | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.platform.LocalContext | ||
|
|
||
| /** | ||
| * Android-specific helper to create PdfGenerator with Context. | ||
| */ | ||
| @Composable | ||
| actual fun rememberPdfGenerator(): PdfGenerator { | ||
| val context = LocalContext.current | ||
| return remember(context) { | ||
| PdfGenerator().apply { | ||
| setContext(context) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.