Exception Handling for Password-Protected PDFs in PdfThumbnail.generate
Summary:
When attempting to generate thumbnails for password-protected PDF files using PdfThumbnail.generate, the error is not caught by the catch blocks in JavaScript. This results in unhandled exceptions when the PDF is password-protected and the promise does not reject as expected.
Steps to Reproduce:
- Call
PdfThumbnail.generatewith a password-protected PDF file. - Observe that an error is thrown, but not caught by the JavaScript
catchblock.
Expected Behavior:
The promise should be rejected, and the error should be caught in the JavaScript catch block, allowing for proper error handling in the React Native environment.
Actual Behavior: The promise is not rejected when the PDF is password-protected, leading to a crash or unhandled exception error in the app.
Proposed Solution:
Modify the native PdfThumbnail.generate method to handle exceptions related to security (such as password protection) more explicitly. Here's the suggested change in the Kotlin code:
@ReactMethod
fun generate(filePath: String, page: Int, quality: Int, promise: Promise) {
// ...existing code...
try {
// ...existing code...
} catch (ex: SecurityException) {
promise.reject("SECURITY_EXCEPTION", "A security issue occurred, possibly due to an incorrect password.")
} catch (ex: IOException) {
// ...existing code...
}
// ...existing code...
}
Additional Context: The issue was discovered during development, and the proposed solution has been tested and confirmed to work as expected, allowing JavaScript to catch the error and handle it gracefully.
I am using this package in one of my project, how can I fix this problem in my code ?
where is this SecurityException comming from??
import java.io.SecurityException , is this right?
you need to edit a file in node_modules
the import is import java.lang.SecurityException
where is this SecurityException comming from??
import java.io.SecurityException , is this right?
you need to edit a file in node_modules
the import is import java.lang.SecurityException
where is this SecurityException comming from?? import java.io.SecurityException , is this right?
okay thanks, but for the production i need to create a patch,
can you tell me how to create it or can you check this one?
--- a/PdfThumbnailModule.kt +++ b/PdfThumbnailModule.kt @@ -1,5 +1,6 @@ package com.github.christophpickl.kpotpourri.common.logging
+import java.security.SecurityException import android.graphics.Bitmap import android.graphics.pdf.PdfRenderer import android.os.ParcelFileDescriptor @@ -54,7 +55,7 @@ class PdfThumbnailModule(reactContext: ReactApplicationContext) : ReactContextBa
@ReactMethod
fun generate(filePath: String, page: Int, quality: Int, promise: Promise) {
-
var parcelFileDescriptor: ParcelFileDescriptor? = null
-
var parcelFileDescriptor: ParcelFileDescriptor? = null var pdfRenderer: PdfRenderer? = null try { parcelFileDescriptor = getParcelFileDescriptor(filePath)
@@ -64,8 +65,14 @@ class PdfThumbnailModule(reactContext: ReactApplicationContext) : ReactContextBa promise.reject("FILE_NOT_FOUND", "File $filePath not found") return }
-
pdfRenderer = PdfRenderer(parcelFileDescriptor) -
if (page < 0 || page >= pdfRenderer.pageCount) { -
promise.reject("INVALID_PAGE", "Page number $page is invalid, file has ${pdfRenderer.pageCount} pages") -
return -
} -
val result = renderPage(pdfRenderer, page, filePath, quality) promise.resolve(result) -
} catch (ex: SecurityException) { -
} }promise.reject("SECURITY_EXCEPTION", "A security issue occurred, possibly due to an incorrect password.") } catch (ex: IOException) { promise.reject("INTERNAL_ERROR", ex) } finally { pdfRenderer?.close() parcelFileDescriptor?.close()
