-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create PdfGenerator class to launch WebView (#3258)
* Create PdfGenerator class to launch WebView * Update documentation in PdfGenerator class * Move webView to constructor for testing purposes * Test PdfGenerator * spotlessApply * Add notes to CHANGELOG.md
- Loading branch information
1 parent
3a2d8a6
commit 05d9903
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
60 changes: 60 additions & 0 deletions
60
android/engine/src/main/java/org/smartregister/fhircore/engine/pdf/PdfGenerator.kt
This file contains 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,60 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.pdf | ||
|
||
import android.content.Context | ||
import android.print.PrintAttributes | ||
import android.print.PrintManager | ||
import android.webkit.WebView | ||
|
||
/** | ||
* PdfGenerator creates PDF files from HTML content using Android's WebView and PrintManager. Must | ||
* be initialized on the Main thread. | ||
* | ||
* @param context Application context for initializing WebView and PrintManager. | ||
* @param webView Optional WebView for testing purposes. | ||
*/ | ||
class PdfGenerator(context: Context, private val webView: WebView = WebView(context)) { | ||
|
||
private val printManager = context.getSystemService(Context.PRINT_SERVICE) as PrintManager | ||
|
||
/** | ||
* Generates a PDF file from the provided HTML content. | ||
* | ||
* This method loads the given HTML content into a WebView, creates a print adapter from the | ||
* WebView, and uses the PrintManager to generate a PDF document with the specified title. | ||
* | ||
* Example usage: | ||
* ``` | ||
* val pdfGenerator = PdfGenerator(context) | ||
* val htmlContent = "<html><body><h1>Hello, World!</h1></body></html>" | ||
* pdfGenerator.generatePdfWithHtml(htmlContent, "SamplePDF") | ||
* ``` | ||
* | ||
* @param html The HTML content to be converted into a PDF. | ||
* @param pdfTitle The title of the PDF document. | ||
*/ | ||
fun generatePdfWithHtml(html: String, pdfTitle: String) { | ||
webView.loadDataWithBaseURL(null, html, "text/HTML", "UTF-8", null) | ||
val printAdapter = webView.createPrintDocumentAdapter(pdfTitle) | ||
printManager.print( | ||
pdfTitle, | ||
printAdapter, | ||
PrintAttributes.Builder().build(), | ||
) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
android/engine/src/test/java/org/smartregister/fhircore/engine/pdf/PdfGeneratorTest.kt
This file contains 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,66 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.pdf | ||
|
||
import android.content.Context | ||
import android.print.PrintDocumentAdapter | ||
import android.print.PrintManager | ||
import android.webkit.WebView | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.ArgumentMatchers.eq | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.verify | ||
import org.mockito.Mockito.`when` | ||
import org.mockito.MockitoAnnotations | ||
import org.mockito.junit.MockitoJUnitRunner | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class PdfGeneratorTest { | ||
|
||
@Mock private lateinit var context: Context | ||
|
||
@Mock private lateinit var printManager: PrintManager | ||
|
||
@Mock private lateinit var webView: WebView | ||
|
||
@Mock private lateinit var printDocumentAdapter: PrintDocumentAdapter | ||
|
||
private lateinit var pdfGenerator: PdfGenerator | ||
|
||
@Before | ||
fun setUp() { | ||
MockitoAnnotations.initMocks(this) | ||
`when`(context.getSystemService(Context.PRINT_SERVICE)).thenReturn(printManager) | ||
pdfGenerator = PdfGenerator(context, webView) // Inject the mock webView | ||
} | ||
|
||
@Test | ||
fun testGeneratePdfWithHtml() { | ||
val htmlContent = "<html><body><h1>Hello, World!</h1></body></html>" | ||
val pdfTitle = "SamplePDF" | ||
|
||
`when`(webView.createPrintDocumentAdapter(pdfTitle)).thenReturn(printDocumentAdapter) | ||
|
||
pdfGenerator.generatePdfWithHtml(htmlContent, pdfTitle) | ||
|
||
verify(webView).loadDataWithBaseURL(null, htmlContent, "text/HTML", "UTF-8", null) | ||
verify(webView).createPrintDocumentAdapter(pdfTitle) | ||
verify(printManager).print(eq(pdfTitle), eq(printDocumentAdapter), eq(null)) | ||
} | ||
} |