locked
Printing From WebView Xamarin Forms RRS feed

  • Question

  • User368189 posted

    Hello guys,

    I want to print a web view with the default printer functionalities on UWP

    Friday, October 26, 2018 9:12 AM

Answers

  • User369978 posted

    Have you checked https://forums.xamarin.com/discussion/91163/problem-with-printing-webview-in-uwp-phone

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 29, 2018 6:10 AM

All replies

  • User369978 posted

    Have you checked https://forums.xamarin.com/discussion/91163/problem-with-printing-webview-in-uwp-phone

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 29, 2018 6:10 AM
  • User368189 posted

    But I want To Skip the dialog of printing

    Thursday, November 1, 2018 10:51 AM
  • User2122 posted

    I so wanted to do this but it was too hard. Finally built it into Forms9Patch - a MIT licensed open source project.

    Verifying that Printing is available

    Before printing, you should verify that printing is available on your device. To do so, call: if (Forms9Patch.PrintService.CanPrint) { // do the printing here }

    Print the contents of a Xamarin.Forms.WebView

    using Forms9Patch; ... var myWebView = new Xamarin.Forms.WebView myWebView.Source = new HtmlWebViewSource { Html = "some HTML text here" }; ... myWebView.Print("my_print_job_name");

    Note that your WebView does not have to be attached to a Layout. This allows you to Print without having to display the WebView in your app’s UI.

    Printing an HTML string

    using Forms9Patch; ... var myHtmlString = @" <!DOCTYPE html> <html> <body> <h1>Convert to PNG</h1> <p>This html will be converted to a PNG, PDF, or print.</p> </body> </html> "; ... myHtmlString.Print("my_print_job_name");

    PLEASE NOTE: iOS sometimes places the page breaks in weird places. I have a StackOverflow Bounty on why this happens and how to fix it.

    Using EmbeddedResource as a source for a Xamarin.Forms.WebView

    This is sort of an experimental feature I’ve built that I’ve found it useful. As such the documentation is sparse. It allow you to put HTML content in a folder in your app’s EmbeddedResources folder and then use it as a source for a WebView. A much nicer solution than using platform specific approach provided by Xamarin. It also supports putting all of the HTML content into a zip file. Please take a look at the source code to see how it works.

    Tuesday, December 31, 2019 7:46 PM