christopherdro / react-native-html-to-pdf

Convert html strings to PDF documents using React Native
MIT License
434 stars 264 forks source link

Getting crash on Android 11 #251

Closed rohitatcoffee closed 2 years ago

rohitatcoffee commented 2 years ago

java.lang.IllegalArgumentException: fd cannot be null at Ua.onWrite(Unknown Source:187) at android.print.PdfConverter$1.onPageFinished(PdfConverter.java:71) at bC0.b(Unknown Source:10) at V9.handleMessage(Unknown Source:280) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:257) at android.app.ActivityThread.main(ActivityThread.java:8192) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1015)

2021-11-28 19:54:13.478 7756-7756/com.test.desk D/PdfConverter: Failed to open ParcelFileDescriptor java.io.FileNotFoundException: open failed: EACCES (Permission denied) at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:344) at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:231) at android.print.PdfConverter.getOutputFileDescriptor(PdfConverter.java:154) at android.print.PdfConverter.access$100(PdfConverter.java:33) at android.print.PdfConverter$1.onPageFinished(PdfConverter.java:71) at bC0.b(Unknown Source:10) at V9.handleMessage(Unknown Source:280) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:257) at android.app.ActivityThread.main(ActivityThread.java:8192) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1015) 2021-11-28 19:54:13.479 7756-7756/com.test.desk W/om.test.des: Accessing hidden method Landroid/print/PrintDocumentAdapter$WriteResultCallback;-><init>()V (greylist, linking, allowed) 2021-11-28 19:54:13.486 7756-7756/com.test.desk D/AndroidRuntime: Shutting down VM

Getting crash whole generating pdf file.

sundardsTechMind commented 2 years ago

The same issue I'm facing Now Playstore is required Target API Level 30 if I upgrade to API Level 30,31 it's failing to generate and Open PDF anyone have a solution for this?

Need Support

rohitatcoffee commented 2 years ago

I fixed the issue by implementing new permission in Android and called function using RNBridge

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

Android:


    @ReactMethod
    public void checkStoragePermission(final Promise promise) {
        boolean isStoragePermission = false;
        if (SDK_INT >= Build.VERSION_CODES.R) {
            isStoragePermission = Environment.isExternalStorageManager();
        } else {
            int result = ContextCompat.checkSelfPermission(reactContext, READ_EXTERNAL_STORAGE);
            int result1 = ContextCompat.checkSelfPermission(reactContext, WRITE_EXTERNAL_STORAGE);
            isStoragePermission = (result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED);
        }

        if(!isStoragePermission) {
            requestStoragePermission();
        }
        promise.resolve(isStoragePermission);
    }
    private void requestStoragePermission() {
        if (SDK_INT >= Build.VERSION_CODES.R) {
            try {
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setData(Uri.parse(String.format("package:%s",reactContext.getPackageName())));
                Objects.requireNonNull(reactContext.getCurrentActivity()).startActivityForResult(intent, 2296);
            } catch (Exception e) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                Objects.requireNonNull(reactContext.getCurrentActivity()).startActivityForResult(intent, 2296);
            }
        } else {
            //below android 11
            ActivityCompat.requestPermissions(Objects.requireNonNull(getCurrentActivity()), new String[]{WRITE_EXTERNAL_STORAGE}, 2296);
        }
    }

I hope this helps you..

sundardsTechMind commented 2 years ago

Thanks for you're response really appreciate you're valuable time can you help me where can add this permission? Is there any specific file?

Thanks in advance

rohitatcoffee commented 2 years ago

JFYI My app got rejected by Google. They emailed me that, I need to remove MANAGE_EXTERNAL_STORAGE permission. They have provided me following link: https://developer.android.com/training/data-storage/shared/media

rohitatcoffee commented 2 years ago

I fixed the issue on my fork using this https://github.com/christopherdro/react-native-html-to-pdf/pull/252/commits. Seems it's working.

https://github.com/rohitatcoffee/react-native-html-to-pdf.git

rohitatcoffee commented 2 years ago

Thanks for you're response really appreciate you're valuable time can you help me where can add this permission? Is there any specific file?

Thanks in advance

Please check, issue is fixed

manawaraquevix commented 2 years ago

Go to android/src/main/java/com/christopherdro/htmltopdf/RNHTMLtoPDFModule.java line 68 and replace new File(Environment.getExternalStorageDirectory(), options.getString(DIRECTORY)) :

with new File(Build.VERSION.SDK_INT >= 30 ? mReactContext.getExternalFilesDir(null):Environment.getExternalStorageDirectory(), options.getString(DIRECTORY)) :

Akifcan commented 9 months ago

I saw this issue on google play crash logs which sdk version has 8.1(27) Galaxy J7 Prime

fully message

java.lang.IllegalArgumentException
Exception java.lang.IllegalArgumentException: fd cannot be null
  at yz.onWrite (chromium-Monochrome.aab-stable-604516420:192)
  at android.print.PdfConverter$1.onPageFinished (PdfConverter.java:71)
  at m65.b (chromium-Monochrome.aab-stable-604516420:12)
  at Ay.handleMessage (chromium-Monochrome.aab-stable-604516420:277)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:7000)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:441)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1408)

Note: only affected 1 user 2 times