Skip to content

Commit

Permalink
Create PdfGenerator class to launch WebView (#3258)
Browse files Browse the repository at this point in the history
* 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
FikriMilano authored May 20, 2024
1 parent 3a2d8a6 commit 05d9903
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.1] - 2024-05-20

### Added
- Added a new class (PdfGenerator) for generating PDF documents from HTML content using Android's WebView and PrintManager

## [1.1.0] - 2024-02-15

### Changed
Expand Down
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(),
)
}
}
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))
}
}

0 comments on commit 05d9903

Please sign in to comment.