javascript 将 MVC 操作结果发送到打印机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8297023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:57:50  来源:igfitidea点击:

Send MVC actionresult to printer

c#javascript.netasp.net-mvc

提问by Hugo Forte

I have a controller with an action:

我有一个带有动作的控制器:

SomeController/ActionToBePrinted

ActionToBePrinted() returns an html view.

ActionToBePrinted() 返回一个 html 视图。

This action is called from a normal mvc razor view when pressing a button - how would I go about sending the content of the view to a printer when the button is pressed?

按下按钮时,从普通的 mvc razor 视图调用此操作 - 当按下按钮时,我将如何将视图的内容发送到打印机?

Aloha, Hugo

阿罗哈,雨果

采纳答案by dknaack

You cant send direct to the printer.

您不能直接发送到打印机。

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

  2. You can show a html page as well and open the print dialog using javascript like this

  1. 我建议你创建一个自定义的 ActionResult,它返回一个 PDF 文件或类似的东西。ASP.NET MVC 操作结果和 PDF 内容

  2. 您也可以显示一个 html 页面并使用这样的 javascript 打开打印对话框

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

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

But always the user has to start the print process, you cant do this programmatically.

但始终用户必须开始打印过程,您不能以编程方式执行此操作。

回答by P.Brian.Mackey

You can perform a GET request (e.g. use window.open() and pass in URL or use AJAX) and put the returned HTML contents into a new window. Then use

您可以执行 GET 请求(例如使用 window.open() 并传入 URL 或使用 AJAX)并将返回的 HTML 内容放入新窗口中。然后使用

Window.print(). Then simply close the window when you are done.

Window.print()。完成后只需关闭窗口即可。

You could tie this directly into a single view by adding something in the body, but I prefer to use JavaScript in these cases. This keeps the design acting as a re-useable object or service that can be used across multiple views. In other words, you setup the controller-model, but no view. Instead, JavaScript steps in as the View.

您可以通过在正文中添加一些内容将其直接绑定到单个视图中,但在这些情况下我更喜欢使用 JavaScript。这使设计始终充当可在多个视图中使用的可重用对象或服务。换句话说,您设置了控制器模型,但没有视图。相反,JavaScript 作为视图介入。

Keep in mind that HTML is not a print format. So if you need to control the layout, you should be using a print technology such as PDF. XSLT provides an excellent means to create both HTML and PDF output using the same data, albeit it's a lot more work to create XSLT templates than it is to slap down window.print

请记住,HTML 不是打印格式。因此,如果您需要控制布局,则应该使用 PDF 等打印技术。XSLT 提供了一种使用相同数据创建 HTML 和 PDF 输出的极好方法,尽管创建 XSLT 模板比创建模板要多得多 window.print

Personally, I have an MVC page acting as a service that takes URL parameters. The page hooks into Adobe XSL-FO and uses the params to drive the output.

就我个人而言,我有一个 MVC 页面充当接受 URL 参数的服务。该页面挂钩到 Adob​​e XSL-FO 并使用参数来驱动输出。