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

  • Question

  • User89714 posted

    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?

    Sunday, February 23, 2020 12:08 PM

All replies

  • User74 posted

    Could you display it off page with translateY, or behind a frame by loading it in a grid cell with a frame over it?

    Sunday, February 23, 2020 12:46 PM
  • User89714 posted

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

    Monday, February 24, 2020 9:51 AM
  • User89714 posted

    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.

    Tuesday, February 25, 2020 4:43 PM
  • User369978 posted

    Any update ?

    Monday, March 9, 2020 5:04 AM
  • User89714 posted

    @ColeX said: Any update ?

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

    Monday, March 9, 2020 11:21 AM