Forms9Patch
Forms9Patch copied to clipboard
[Bug] await HTML to PDF "ToPdfAsync" doesn't return and lock the UI on iOS
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
- create an html string
- 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
I have very similar issue with the Android - nothing happens. Investigating.
Okay, here's the fix that I made:
- forked PdfService for easier editing.
- 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.
FYI - the same problem was on both Android and iOS, so you will have to make changes in the both of ToPDF() implementations.