locked
Problem With Printing WebView in UWP Phone RRS feed

  • Question

  • User300485 posted

    Unable to print WebView in xamarin forms. I am making use of dependency service to print webview, it works fine in android and iOS but somehow page get cropped in UWP. Following is the code and screenshot for the same. Any help is appreciated.

             public class Print_UWP : IPrint
                {
                    PrintManager printmgr = PrintManager.GetForCurrentView();
                    PrintDocument PrintDoc = null;
                    PrintDocument printDoc;
                    PrintTask Task = null;
                    Windows.UI.Xaml.Controls.WebView ViewToPrint = new Windows.UI.Xaml.Controls.WebView();
                    public Print_UWP()
                    {
                        printmgr.PrintTaskRequested += Printmgr_PrintTaskRequested;
                    }
    
                    public async void PrintUWpAsync(string htmlSource)
                    {
                        ViewToPrint.NavigateToString(htmlSource);
                        if (PrintDoc != null)
                        {
                            printDoc.GetPreviewPage -= PrintDoc_GetPreviewPage;
                            printDoc.Paginate -= PrintDoc_Paginate;
                            printDoc.AddPages -= PrintDoc_AddPages;
                        }
                        this.printDoc = new PrintDocument();
                        try
                        {
                            printDoc.GetPreviewPage += PrintDoc_GetPreviewPage;
                            printDoc.Paginate += PrintDoc_Paginate;
                            printDoc.AddPages += PrintDoc_AddPages;
    
                            bool showprint = await PrintManager.ShowPrintUIAsync();
    
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine(e.ToString());
                        }
                        PrintDoc = null;
                        GC.Collect();
    
                    }
    
    
                    private void Printmgr_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
                    {
                        var deff = args.Request.GetDeferral();
                        Task = args.Request.CreatePrintTask("Invoice", OnPrintTaskSourceRequested);
    
                        deff.Complete();
    
                    }
                    async void OnPrintTaskSourceRequested(PrintTaskSourceRequestedArgs args)
                    {
                        var def = args.GetDeferral();
                        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            args.SetSource(printDoc.DocumentSource);
                        });
                        def.Complete();
                    }
    
                    private void PrintDoc_AddPages(object sender, AddPagesEventArgs e)
                    {
                        printDoc.AddPage(ViewToPrint);
                        printDoc.AddPagesComplete();
                    }
    
                    private void PrintDoc_Paginate(object sender, PaginateEventArgs e)
                    {
                        PrintTaskOptions opt = Task.Options;
                        printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
                    }
    
                    private void PrintDoc_GetPreviewPage(object sender, GetPreviewPageEventArgs e)
                    {
                        printDoc.SetPreviewPage(e.PageNumber, ViewToPrint);
                    }
            }
    
    Monday, March 20, 2017 11:26 AM

Answers

  • User78724 posted

    Have you tried to fix the issue from html itself?

    <!DOCTYPE html> <html> <head> <style> body { height: 842px; width: 595px; /* to centre page on screen*/ margin-left: auto; margin-right: auto; } </style> </head> <body> </body> </html>

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 6, 2017 1:07 AM
  • User300485 posted

    @maxchen Thanks a lot.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, April 7, 2017 10:04 AM

All replies

  • User78724 posted

    Have you tried to fix the issue from html itself?

    <!DOCTYPE html> <html> <head> <style> body { height: 842px; width: 595px; /* to centre page on screen*/ margin-left: auto; margin-right: auto; } </style> </head> <body> </body> </html>

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 6, 2017 1:07 AM
  • User300485 posted

    @maxchen Thanks a lot.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, April 7, 2017 10:04 AM
  • User89714 posted

    @Shera - Just looking at your code, you have PrintDoc and printDoc declared. However, unless I'm missing something, you never set PrintDoc to a non-null value. Might be worth double-checking.

    Wednesday, June 7, 2017 1:28 PM
  • User89714 posted

    @maxchen - Do you know of an equivalent that uses percentages for width and height, maintains the aspect ratio, and that forces those percentages to be used for child elements (so not just for body itself) even if the webpage being printed uses absolute measurements in CSS for child elements? Using third-party constructed HTML/CSS, I see the body being changed by the styling above, but child elements are fixed at their original size. This is a fault of the third-party CSS, but I wonder if there is an override that can be used when using printing code as per this thread?

    Wednesday, June 7, 2017 3:54 PM
  • User24062 posted

    There really needs to be a UWP webview printing API that is as easy to use as the Android one. Using PrintHelper is a bit overly complicated. The solution above works well for one page but lacks support for multiple pages.

    Tuesday, November 14, 2017 9:53 PM
  • User24062 posted

    BTW, there is a memory leak in the above code. It might be that the Webview never gets released when the code finishes. The GC.Collect doesn't seem to do the trick. It's also possible to have the Print Preview notify when printing is finished or canceled. That is where I call the code to dispose of memory.

    There are examples of how to use the Microsoft PrintHelper code but that code is really not usable in cross platform portable apps because it depends on a Windows.UI Page not a Xamarin Page and I haven't found a solutions for that.

    Sunday, November 26, 2017 10:09 PM
  • User330867 posted

    Only if there was a cross-platform solution for XF iOS, Android, UWP, macOS and WPF

    Saturday, July 21, 2018 2:49 PM
  • User385259 posted

    Any way to make this print more than one page? I've read the stackoverflow posts on this and they do not help at all.

    Thanks

    Monday, May 6, 2019 11:51 PM
  • User386165 posted

    I am printing with ZPl command. the printer is printing in reverse (bottom to top), and its printing in fix size of a page . I want to print as wrap content. and the page should not be wasted or content should not be missed. can anyone help me? Thanks in advance

    Friday, May 31, 2019 6:29 AM