christopherdro / react-native-html-to-pdf

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

Fixed the issue with pdf generation when error is returned from pdf module this library is using #226

Closed shubhamdeol closed 3 years ago

shubhamdeol commented 3 years ago

What?

This pull request fixes the issue when error is returned from pdf module.

Why?

when user tries generated pdf mIsCurrentlyConverting variable becomes true and error is thrown from library, it never becomes false again, It is because this library is not handling error returned from module correctly.

so due to

        if (mIsCurrentlyConverting)
            return;

Library never tried to generate pdf again and promise stays in pending state.

How?

After looking at PdfDocumentAdapter. I found out that Library has two methods onWriteFailed and onWriteCancelled. I started using these and returning error when pdf generation fails and call destroy() method to unset the flag mIsCurrentlyConverting to false.

     @Override
                        public void onWriteFailed(CharSequence error) {
                            String errorResult = "Please retry, Error occurred generating the pdf";
                            if (error != null) {
                                errorResult = error.toString();
                            }
                            mPromise.reject(errorResult);
                            destroy();
                        }

                        @Override
                        public void onWriteCancelled() {
                            destroy();
                        }

Anything Else?

This fixes the issue with promise staying in pending state, and returns the error gracefully.

Request from library maintainer Please fix the issue of .apk size getting increased by 5mb, otherwise this works nicely. There is one fork of this Library by Onibenjp which fixes the size issue. But I think there is more scope to this. Please merge the changes from this library. There is no point of having many libraries solving same problem.

liamjones commented 2 years ago

Should the promise not be rejected/resolved in onWriteCancelled too?

Pranav2992 commented 1 year ago

Still i have [Error: Please retry, Error occurred generating the pdf]

liamjones commented 1 year ago

@Pranav2992 This doesn't fix errors while generating the PDF (nor was it trying to). It just fixed the fact that one failure would lock up the module so no more PDFs could be generated.