Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Silent pdf printing from WebView2 #4422

Closed
b-mi opened this issue Mar 14, 2024 · 4 comments
Closed

[Feature]: Silent pdf printing from WebView2 #4422

b-mi opened this issue Mar 14, 2024 · 4 comments
Assignees
Labels
feature request feature request tracked We are tracking this work internally.

Comments

@b-mi
Copy link

b-mi commented Mar 14, 2024

Describe the feature/enhancement you need

There is support for silent printing of the current web page. So there is support for silent printing of DISPLAYED content.

But we need ability to print pdf file silently through WebView2 without displaying WebView2 control and pdf file in it.

It is about printing via webview2 without displaying webview2.

The scenario/use case where you would use this feature

We have ERP system with many created pdf files. Users ask direct print of pdf without displaying pdf file. Users does not want always to display PDF content before printing. Thank you.

How important is this request to you?

Impactful. My app's user experience would be significantly compromised without it.

Suggested implementation

No response

What does your app do? Is there a pending deadline for this request?

No response

AB#50160785

@b-mi b-mi added the feature request feature request label Mar 14, 2024
@victorhuangwq
Copy link
Collaborator

@monica-ch is this possible?

@monica-ch
Copy link
Contributor

@b-mi Could you verify if the PrintToPdfAsync works when the WebView2 control is made visible for rendering, but not actually included in the application's user interface hierarchy?

The idea is to make the WebView2 control visible, triggering it to render its content for printing, even though it's not placed in the user interface and therefore not visible to the user. This approach would allow the application to print the rendered content without the need to display the WebView2 control to the user.

@b-mi
Copy link
Author

b-mi commented Jun 19, 2024

Yes, it looks like in WebView2 version 1.0.2535.41 it is possible to simulate direct printing in a WPF application. This code works for me:

        async private void printPdfAsync_Click(object sender, RoutedEventArgs e)
        {
            using (new WaitCursor())
            {
                label.Text = "printing...";
                var rtn = await pdfPrintDirect(pdfFilePath);
                label.Text = "done...";

            }
        }

       private async Task<bool> pdfPrintDirect(string filePath)
       {
           var wvl = new WebView2();
           var w = new Window();
           double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
           w.Top = screenHeight + 100; // out of visibility
           // make window invisible
           w.Focusable = false;
           w.IsHitTestVisible = false;
           w.Content = wvl;
           w.ShowInTaskbar = false;

           var tcs = new TaskCompletionSource<bool>();
           w.Closed += (o, e) =>
           {
               // done
               tcs.SetResult(true);
           };

           w.Show();
           var printerName = "Microsoft Print to PDF";
           await wvl.EnsureCoreWebView2Async();

           var uri = new Uri(filePath, UriKind.Absolute);
           var str = uri.ToString();

           wvl.CoreWebView2.NavigationCompleted += async (o, e2) =>
           {
               CoreWebView2PrintSettings sets = wvl.CoreWebView2.Environment.CreatePrintSettings();
               sets.Duplex = Microsoft.Web.WebView2.Core.CoreWebView2PrintDuplex.Default;
               sets.Collation = Microsoft.Web.WebView2.Core.CoreWebView2PrintCollation.Default;
               sets.ScaleFactor = 1;
               sets.ShouldPrintBackgrounds = false;
               //sets.ColorMode = Microsoft.Web.WebView2.Core.CoreWebView2PrintColorMode.Grayscale;
               sets.MediaSize = Microsoft.Web.WebView2.Core.CoreWebView2PrintMediaSize.Default;
               sets.Orientation = Microsoft.Web.WebView2.Core.CoreWebView2PrintOrientation.Portrait;
               sets.PageRanges = "";
               sets.PrinterName = printerName;
               var rtn = await wvl.CoreWebView2.PrintAsync(sets);
               w.Close();
           };


           wvl.CoreWebView2.Navigate(str);
           return await tcs.Task;
       }

@nishitha-burman
Copy link
Collaborator

Closing as the issue has been resolved. Please reopen if you have further questions. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request feature request tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

4 participants