Send MVC actionresult to printer

I have a controller with an action:

SomeController/ActionToBePrinted 

ActionToBePrinted () returns an html view.

This action is called from a regular mvc razor when a button is pressed - how can I send the contents of a view to a printer when a button is clicked?

Aloha, Hugo

+6
source share
2 answers

You cannot send directly to the printer.

  • I suggest you create a custom ActionResult that returns a PDF file or something like that. ASP.NET MVC Action Results and PDF Content

  • You can also show the html page and open the print dialog using javascript like this

<a href="javascript:window.print()">Click to Print This Page</A>

But always the user must start the printing process, you cannot do this programmatically.

+3
source

You can execute a GET request (for example, use window.open () and pass a URL or use AJAX) and put the returned HTML content in a new window. Then use

Window.print () . Then just close the window when done.

You can link this directly into a single view by adding something to the body, but I prefer to use JavaScript in these cases. This forces the project to act as a reusable object or service that can be used for several types. In other words, you are customizing the controller model, but there is no representation. Instead, JavaScript works as a view.

Keep in mind that HTML is not a print format. Therefore, if you need to control the layout, you should use printing technology such as PDF. XSLT provides excellent tools for creating both HTML and PDF files using the same data, although creating XSLT templates is much more work than for step-by-step window.print

Personally, I have an MVC page acting as a service that accepts URL parameters. The page intercepts Adobe XSL-FO and uses parameters to output the output.

+1
source

Source: https://habr.com/ru/post/902560/


All Articles