Skip to content

Windows support with WebView2? #1

Description

@NXTwoThou

Have you considered Windows support by using WebView2? Here's a quicky example..

private const double A4_WIDTH_INCHES = 8.27; private const double A4_HEIGHT_INCHES = 11.69; #if WINDOWS public async Task<HtmlToPdfResult> ConvertValWebView(string html, string filePath) { var tcs = new TaskCompletionSource<HtmlToPdfResult>(); try { Microsoft.UI.Xaml.Controls.WebView2 nativeWebView = new Microsoft.UI.Xaml.Controls.WebView2(); await nativeWebView.EnsureCoreWebView2Async(); CoreWebView2 coreWebView2 = nativeWebView.CoreWebView2; coreWebView2.NavigationCompleted += async (sender, e) => { if (e.IsSuccess) { CoreWebView2PrintSettings printSettings = coreWebView2.Environment.CreatePrintSettings(); printSettings.PageWidth = A4_WIDTH_INCHES; printSettings.PageHeight = A4_HEIGHT_INCHES; await nativeWebView.CoreWebView2.PrintToPdfAsync(filePath, printSettings); tcs.TrySetResult(HtmlToPdfResult.Success(filePath)); } else { System.Diagnostics.Debug.WriteLine($"PDF Generation Error: {e.WebErrorStatus}"); tcs.TrySetResult(HtmlToPdfResult.Failure(e.WebErrorStatus.ToString())); } }; coreWebView2.NavigateToString(html); await Task.Delay(800); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"PDF Generation Error: {ex.Message}"); tcs.TrySetResult(HtmlToPdfResult.Failure(ex.Message)); } return await tcs.Task; } #endif

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions