Forms9Patch icon indicating copy to clipboard operation
Forms9Patch copied to clipboard

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

Open XamMattia83 opened this issue 3 years ago • 3 comments

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

  • Version with issue: guess all
  • Last known good version: none
  • IDE:
  • Platform Target Frameworks:
    • iOS: 11
    • Android: 11
    • UWP:
  • Android Support Library Version:
  • Nuget Packages: Forms9Patch 2.4.9
  • Affected Devices: all real iOS devices

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

XamMattia83 avatar Mar 16 '22 09:03 XamMattia83

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

smalgin avatar Dec 03 '22 21:12 smalgin

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 avatar Dec 08 '22 05:12 smalgin

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

smalgin avatar Dec 11 '22 22:12 smalgin