Problem With Printing WebView in UWP Phone

SheraShera INMember ✭✭
edited March 2017 in Xamarin.Forms

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);
                }
        }

Best Answers

  • SheraShera IN ✭✭
    Accepted Answer

    @maxchen Thanks a lot.

Answers

  • SheraShera INMember ✭✭
    Accepted Answer

    @maxchen Thanks a lot.

  • JohnHardmanJohnHardman GBUniversity mod

    @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.

  • JohnHardmanJohnHardman GBUniversity mod

    @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?

  • BrianConradBrianConrad USMember ✭✭✭

    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.

  • BrianConradBrianConrad USMember ✭✭✭

    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.

  • amirvenusamirvenus USMember ✭✭✭
    Only if there was a cross-platform solution for XF iOS, Android, UWP, macOS and WPF
  • norton287norton287 Member ✭✭

    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

  • asadwinitasadwinit Member

    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

Sign In or Register to comment.