baskren / Forms9Patch

Simplify image management and text formatting in your Xamarin.Forms apps
http://Forms9Patch.com
Other
128 stars 33 forks source link

[Bug] await HTML to PDF "ToPdfAsync" doesn't return and lock the UI on iOS #105

Open XamMattia83 opened 2 years ago

XamMattia83 commented 2 years ago

Description

On Real iOS devices, the await html.ToPdfAsync call doens't return 100% of the time and freeze the UI main thread On Android it works.

Steps to Reproduce

  1. create an html string
  2. call the await.ToPdfAsync(htmlfile, filename, pagesize, margin) as in your demo code

Expected Behavior

the called Task must return

Actual Behavior

it doesn't return 100% of the time.

Basic Information

Screenshots

Reproduction Link - a link to a small demo project that reproduces this issue

                        try
                        {
                            if (Forms9Patch.ToPdfService.IsAvailable)
                            {
                                if (await htmlfile.ToPdfAsync(ReportFileName, PageSize.IsoA4) is ToFileResult pdfResult)
                                {
                                    if (pdfResult.IsError)
                                    {                                      
                                        await _userDialogs.AlertAsync(AppResources.StrReportGenerationError + pdfResult.Result);
                                    }
                                    else
                                    {   
                                        await Launcher.OpenAsync(new OpenFileRequest
                                        {
                                            Title = AppResources.OpenWithString,
                                            File = new ReadOnlyFile(pdfResult.Result)
                                        });
                                    }
                                }
                            }
                            else
                            {                             
                                await _userDialogs.AlertAsync(AppResources.StrPdfNonDisponibile);
                            }
                        }
                        catch (Exception err)
                        {                           
                            await _userDialogs.AlertAsync(AppResources.StrReportGenerationError + err);
                        }

Workaround

smalgin commented 1 year ago

I have very similar issue with the Android - nothing happens. Investigating.

smalgin commented 1 year ago

Okay, here's the fix that I made:

1) forked PdfService for easier editing. 2) enclosed the webview construction and initialization in the Device.InvokeOnMainThreadAsync() block.

That resolved the 'doing nothing' problem. Apparently, Webkit's WebView MUST be initialized on the main thread.

smalgin commented 1 year ago

FYI - the same problem was on both Android and iOS, so you will have to make changes in the both of ToPDF() implementations.