songsterq / react-native-pdf-thumbnail

react native module for generating thumbnail for PDF files
MIT License
45 stars 19 forks source link

Exception Handling for Password-Protected PDFs in PdfThumbnail.generate #73

Open Metecko opened 10 months ago

Metecko commented 10 months ago

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:

  1. Call PdfThumbnail.generate with a password-protected PDF file.
  2. Observe that an error is thrown, but not caught by the JavaScript catch block.

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.


Nirbhay897 commented 8 months ago

I am using this package in one of my project, how can I fix this problem in my code ?

Nirbhay897 commented 8 months ago

where is this SecurityException comming from??

import java.io.SecurityException , is this right?

Metecko commented 8 months ago

you need to edit a file in node_modules

image

the import is import java.lang.SecurityException

where is this SecurityException comming from??

import java.io.SecurityException , is this right?

Nirbhay897 commented 8 months ago

you need to edit a file in node_modules

image

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) {