WinUI 3.0 - UWP: Error on WebView2 Print functionality
Asked Answered
S

0

1

In my WinUI 3.0 - UWP project, I have a WebView2 control that displays a simple html as follows. But when I call the following javascript code using ExecuteScriptAsync (as shown below), I get the following error:

Error:

Check your printer or select another printer. The printer you chose isn't available or isn't installed correctly.

Screenshot of Error:

enter image description here

Remarks: There is nothing wrong on my Windows 10's default Print to PDF printer as I can print the exact same html page from a browser (MS Edge or Google).

Question: What I may be doing wrong here, and can we resolve the issue? I think a cause of the issue may have something to do with how I'm using the JavaScript in my html and/or how I'm passing a parameter to the ExecuteScriptAsync(...) method. Note: wvTest, as you may have guessed, is the name of WebView2 control.

Code:

//Correctly displays the html page with a simple text: `Test paragraph`
private async void myButton_Click(object sender, RoutedEventArgs e)
{
    string sHTML = "<!DOCTYPE html>" +
        "<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">" +
        "<head>" +
            "<meta charset=\"utf-8\" />" +
            "<title>Test Title</title>" +
        "</head>" +
        "<body>" +
            "<p>Test paragraph</p>" +
            "<script> window.print();</script>" +
        "</body>" +
        "</html>";
    //System.Diagnostics.Debug.WriteLine(sHTML);
    wvTest.NavigateToString(sHTML);
}

//This event opens the Print Dialog with the message shown above:

private async void btnPrint_Click(object sender, RoutedEventArgs e)
{
    await wvTest.ExecuteScriptAsync(javascriptCode: "window.print();");
}
Stoke answered 13/7, 2020 at 4:5 Comment(6)
@PoulBak Agreed. Replaced with UWP.Stoke
@PoulBak That's what I had done. I may not have clicked on Save Tags button. I re-did it now.Stoke
Have you checked with another printer, like text printer or print to file?Suprarenal
@PoulBak That dropdown for Printer selection (shown in the image above) is not letting you see the other choices. I don't know why because all the code I have is shown all in this post. I have not set any printer options in the code. However, if I print the same html manually from a browser, the printer gives all other options to select. Maybe, it's happening since WinUI 3.0 is still in preview until this November. However, according to this GitHub post, the window.print(); should work.Stoke
I have the same bug, I think it is still not solved for UWPZandrazandt
May be related: github.com/microsoft/microsoft-ui-xaml/issues/2894Zandrazandt

© 2022 - 2024 — McMap. All rights reserved.