Forum Xamarin.iOS


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to print a web page without first rendering it on a page?

JohnHardmanJohnHardman GBUniversity admin
edited February 2020 in Xamarin.iOS

Given a URL, how can I print what is at that URL on iOS, without displaying a WKWebView (or WebView in XF)?
I find that if the URL is that of an asset such as a .png file then it works, but if it is that of a page then it doesn't.

I've tried a few ways, the simplest being:

                    if (Foundation.NSUrl.FromString(url) is Foundation.NSUrl nsUrl
                        && nsUrl.Scheme != null)
                        UIKit.UIPrintInfo printInfo = UIKit.UIPrintInfo.PrintInfo;
                        printInfo.JobName = printJobConfiguration.JobName;
                        printInfo.Duplex = UIKit.UIPrintInfoDuplex.None;
                        printInfo.OutputType = UIKit.UIPrintInfoOutputType.General;

                        UIKit.UIPrintInteractionController printController = UIKit.UIPrintInteractionController.SharedPrintController;
                        printController.ShowsPageRange = true;
                        printController.ShowsPaperSelectionForLoadedPapers = true;
                        printController.PrintInfo = printInfo;
                        printController.PrintingItem = Foundation.NSData.FromUrl(nsUrl);

                        printController.Present(true, (printInteractionController, completed, error) =>
                            // error reporting goes here

With the above implementation, the print preview that appears has the correct number of pages, but they are all blank.

I've also instantiated a new WKWebView, issued a LoadRequest and waited for EstimatedProgress to be 1.0, before printing the WKWebView. Again, that doesn't render the content. I am assuming because the WKWebView itself is not rendered on a page.

Any ideas how to do this without rendering the WKWebView on a page?


  • JohnHJohnH GBMember ✭✭✭✭✭
    Could you display it off page with translateY, or behind a frame by loading it in a grid cell with a frame over it?
  • JohnHardmanJohnHardman GBUniversity admin

    Thanks @JohnH - I'll give it a go and let you know. TBH, I had been hoping to do it without hooking into a page, but that may be the easiest thing to do (I have printing of rendered WebViews working already).

  • JohnHardmanJohnHardman GBUniversity admin

    For the time being, I've gone with a 1x1 WebView on a transparent page, that I display using iOS-specific code rather than a Xamarin.Forms PushAsync. I'd still prefer a solution that doesn't hook into the UI at all, but this will do until I find something better.

    I'll publish the result in my printing NuGet as soon as I have sorted out one minor NuGet packaging issue.

  • ColeXColeX Member, Xamarin Team Xamurai

    Any update ?

  • JohnHardmanJohnHardman GBUniversity admin

    @ColeX said:
    Any update ?

    I haven't had time to sort the packaging issue yet. Will get to it

Sign In or Register to comment.