GeneratePDFUseCase.kt 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Álvaro Brey
  5. * Copyright (C) 2023 Álvaro Brey
  6. * Copyright (C) 2023 Nextcloud GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.nextcloud.client.documentscan
  23. import android.graphics.BitmapFactory
  24. import android.graphics.pdf.PdfDocument
  25. import com.nextcloud.client.logger.Logger
  26. import java.io.FileOutputStream
  27. import java.io.IOException
  28. import javax.inject.Inject
  29. /**
  30. * This class takes a list of images and generates a PDF file.
  31. */
  32. class GeneratePDFUseCase @Inject constructor(private val logger: Logger) {
  33. /**
  34. * @param imagePaths list of image paths
  35. * @return `true` if the PDF was generated successfully, `false` otherwise
  36. */
  37. fun execute(imagePaths: List<String>, filePath: String): Boolean {
  38. return if (imagePaths.isEmpty() || filePath.isBlank()) {
  39. logger.w(TAG, "Invalid parameters: imagePaths: $imagePaths, filePath: $filePath")
  40. false
  41. } else {
  42. val document = PdfDocument()
  43. fillDocumentPages(document, imagePaths)
  44. writePdfToFile(filePath, document)
  45. }
  46. }
  47. /**
  48. * @return `true` if the PDF was generated successfully, `false` otherwise
  49. */
  50. private fun writePdfToFile(
  51. filePath: String,
  52. document: PdfDocument
  53. ): Boolean {
  54. return try {
  55. val fileOutputStream = FileOutputStream(filePath)
  56. document.writeTo(fileOutputStream)
  57. fileOutputStream.close()
  58. document.close()
  59. true
  60. } catch (ex: IOException) {
  61. logger.e(TAG, "Error generating PDF", ex)
  62. false
  63. }
  64. }
  65. private fun fillDocumentPages(
  66. document: PdfDocument,
  67. imagePaths: List<String>
  68. ) {
  69. imagePaths.forEach { path ->
  70. val bitmap = BitmapFactory.decodeFile(path)
  71. val pageInfo = PdfDocument.PageInfo.Builder(bitmap.width, bitmap.height, 1).create()
  72. val page = document.startPage(pageInfo)
  73. page.canvas.drawBitmap(bitmap, 0f, 0f, null)
  74. document.finishPage(page)
  75. }
  76. }
  77. companion object {
  78. private const val TAG = "GeneratePDFUseCase"
  79. }
  80. }