I override a ViewResult, to return a Pdf stream to the client. The pdf is generated in a MVC View (.cshtml file). And added some Extensions for the controller to make the experience smooth.
I have two options:
Create the Pdf from a "normal" view that returns Html. It's very easy, but you do not have "fine" control over the layout.
Create a Pdf using code, the layout code is in the View (using server side code). This is easy for customization, and testing. Because you do not need to recompile the whole project. Also IMHO the View (.cshtml file) is responsible for the layout, if the result is html or pdf does not mather, its still the view.
发布评论
评论(3)
使用 iTextSharp for C#,它是从 JAVA 的 iText 移植
而来的
ITextSharp HTML 到 PDF?
use iTextSharp for C# which is ported from iText from JAVA
check out
ITextSharp HTML to PDF?
我使用 iTextSharp 创建了一个项目来执行此操作,并发布在 bitbucket 上。
我重写
ViewResult
,将 Pdf 流返回给客户端。 pdf 在 MVC 视图(.cshtml 文件)中生成。并为控制器添加了一些扩展,使体验更加流畅。我有两个选择:
I've created a project for doing this using iTextSharp, and published on bitbucket.
I override a
ViewResult
, to return a Pdf stream to the client. The pdf is generated in a MVC View (.cshtml file). And added some Extensions for the controller to make the experience smooth.I have two options:
您需要调用 RenderView 来获取 HTML 结果,然后转换为 PDF。查看此条目以获取类似的解决方案
http://www.jimzimmerman.com/blog/2009/10/06/PdfResult+A+Custom+ActionResult+In+ASPNET+MVC.aspx
You will need to call RenderView to get the HTML result and then convert to PDF. Check this entry out for a similar solution
http://www.jimzimmerman.com/blog/2009/10/06/PdfResult+A+Custom+ActionResult+In+ASPNET+MVC.aspx