如何在MVC3特定视图的PDF报告 [英] How to make a pdf report of a particular view in mvc3
本文介绍了如何在MVC3特定视图的PDF报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得表格形式的所有学生的信息,所以我创建相应的视图。现在,我可以作出这样的观点的PDF报告(通过传递以任何行动),这看起来完全一样观点看???
I wanted to get all student information in tabular format so i created appropriate view. Now can i make a pdf report of that view(By passing that view to any action) which will look exactly as view is looking???
推荐答案
简短的回答是不容易,你可以使用iTextSharp的(可在的NuGet)和尝试以下操作:
Short answer is not easily, you can use iTextSharp (available on NuGet) and try the following:
public EmptyResult CreatePdf()
{
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
var html = RenderRazorViewToString("PdfView", pdfViewModel);
var worker = new HTMLWorker(document);
var css = new StyleSheet();
css.LoadTagStyle(HtmlTags.TH, HtmlTags.BGCOLOR, "#616161");
css.LoadTagStyle(HtmlTags.TH, HtmlTags.COLOR, "#fff");
css.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONT, "verdana");
css.LoadTagStyle(HtmlTags.TH, HtmlTags.FONTWEIGHT, "bold");
css.LoadStyle("even", "bgcolor", "#EEE");
worker.SetStyleSheet(css);
var stringReader = new StringReader(html);
worker.Parse(stringReader);
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "PdfView.pdf");
Response.BinaryWrite(output.ToArray());
return new EmptyResult();
}
private string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
您需要的样式和结果HTML摆弄得到它与HtmlWorker寿工作。
You will need to fiddle with the styles and the resultant Html to get it to work with the HtmlWorker tho.
心连心
硅
这篇关于如何在MVC3特定视图的PDF报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文