1

I'm looking for a way to print ASP.NET/ Mono MVC2 view from ASP.NET application running in Windows 2003 server. I tried code below based on Programmatically "hello world" default SERVER-side printer in ASP.NET MVC

but this outputs raw html string. How to print view as formatted text using free software?

Order layout is created as html partial view. If there is other free way to print out formatted order, I can create layout in other form instead of html.

Only free solution which I have found requires to use Windows Forms WebBrowser control but this looks not reasonable in MVC2 application which is running under Mono also.

I looked into Rotativa ( http://nuget.org/packages/Rotativa/ ) but it looks like it doesnt allow to print html.

using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Web.Mvc;

public class PrintController : Controller
{
    string body;
    public ActionResult Complete()
    {
        body = RenderViewToString<TestOrder>("~/Views/Checkout/Order.ascx", new TestOrder() { Number = "1" });
        PrintOrder();
        return View("PaymentComplete");
    }

    void PrintOrder()
    {
        // https://stackoverflow.com/questions/12229823/programmatically-hello-world-default-server-side-printer-in-asp-net-mvc
        var doc = new PrintDocument();
        doc.PrinterSettings.PrinterName = "HP Laserjet 1200";
        doc.PrintPage += new PrintPageEventHandler(ProvideContent);
        doc.Print();
    }

    void ProvideContent(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawString(body,
          new Font("Arial", 12),
          Brushes.Black,
          e.MarginBounds.Left,
          e.MarginBounds.Top);
    }

    string RenderViewToString<T>(string viewPath, T model)
    { // https://stackoverflow.com/questions/483091/render-a-view-as-a-string
        ViewData.Model = model;
        using (var writer = new StringWriter())
        {
            var view = new WebFormView(viewPath);
            var vdd = new ViewDataDictionary<T>(model);
            var viewCxt = new ViewContext(ControllerContext, view, vdd, new TempDataDictionary(), writer);
            viewCxt.View.Render(viewCxt, writer);
            return writer.ToString();
        }
    }
}

public class TestOrder
{
    public string Number;
}
Community
  • 1
  • 1
Andrus
  • 26,339
  • 60
  • 204
  • 378
  • Look at http://stackoverflow.com/questions/3696842/print-html-in-c-done – Piotr Stapp Apr 09 '13 at 19:11
  • Thank you. Question you referenced uses WinForms WebBrowser control. Will this work from ASP.NET MVC2 application running under IIS service ? Should I really add winforms reference to MVC2 project ? – Andrus Apr 10 '13 at 11:43

1 Answers1

-1

There is an article about convert HTML to PDF using iTextSharp: http://www.dotnetspider.com/resources/43589-How-convert-HTML-PDF-ASP-NET.aspx

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
  • 1
    Thank you. This article does not describe how to print html content. I changed printer name in code question to ´HP Laserjet 1200´ maybe this was misleading. How to print order from html or other layout? Using your link I can convert html to pdf (referenced rotativa also allows) this but how to print pdf then ? – Andrus Apr 10 '13 at 11:39
  • Did you look at http://stackoverflow.com/questions/14328001/printing-pdf-file-page-by-page-using-system-drawing-printing – Piotr Stapp Apr 10 '13 at 18:49