Asp.Net MVC 如何获取视图以生成 PDF [英] Asp.Net MVC how to get view to generate PDF

查看:24
本文介绍了Asp.Net MVC 如何获取视图以生成 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在控制器上调用一个动作.让控制器从模型中获取数据.然后视图运行并生成一个 PDF.我发现的唯一例子是 Lou http://whereslou.com/2009/04/12/returning-pdfs-from-an-aspnet-mvc-action.他的代码非常优雅.该视图使用 ITextSharp 生成 PDF.唯一的缺点是他的示例使用了 Spark 视图引擎.有没有办法用标准的 Microsoft 视图引擎做类似的事情?

I would like to call an action on a controller. Have the controller get the data from the model. The view then runs and generates a PDF. The only example I have found is in an article by Lou http://whereslou.com/2009/04/12/returning-pdfs-from-an-aspnet-mvc-action. His code is very elegant. The view is using ITextSharp to generate the PDF. The only downside is his example uses the Spark View Engine. Is there a way to do a similar thing with the standard Microsoft view engine?

推荐答案

我们对这个问题的最终答案是使用 旋转.

our final answer to this problem was to use Rotativa.

它像其他一些解决方案一样包含 WKhtmltopdf.exe,但它是迄今为止我发现的最容易使用的

It wraps up the WKhtmltopdf.exe like some of the other solutions, but it's by far the easiest to use that I have found

我对所有其他也能很好地解决问题的答案进行了投票,但这是我们用来解决上述问题中提出的问题的方法.它与其他答案不同.

I went and up voted all the other answers that also solve the problem well, but this is what we used to solve the problem posed in the question above. It is different from the other answers.

这是一个 Rotativa教程.

安装后,这就是你的全部需求

after you install it, this is all your need

public ActionResult PrintInvoice(int invoiceId)
{
  return new ActionAsPdf(
                 "Invoice", 
                 new { invoiceId= invoiceId }) 
                 { FileName = "Invoice.pdf" };
}

非常非常简单.

这篇关于Asp.Net MVC 如何获取视图以生成 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆