2014-01-21 28 views

回答

4

看看这个应用程序:

http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC

,如果你想直接下载该文件,你可以用FileContentResult做到这一点:

protected FileContentResult ViewPdf(string pageTitle, string viewName, object model) 
{ 
string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model); 
byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle); 
return File(buffer, "application/pdf","file.pdf"); 
} 
+0

感谢您的回答! –

6

使用剃刀PDF生成PDF报告。这里是简单的过程。

1步 - 创建MVC应用程序

第2步 - 安装Rotativa PDF的NuGet。

第3步 - 放置一个简单的模型,这样

public class Person 
{ 
    public string Name { get; set; } 
    public int Age { get; set; } 
} 

第4步 - 创建我们上面创建的模型一个简单的视图,命名视图索引。

@model IEnumerable<WebApplication1.Controllers.Person> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Name) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Age) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Name) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Age) 
     </td> 
    </tr> 
} 

</table> 

第5步 - 创建一个简单的控制器操作。

public ActionResult IndexNew() 
    { 
     return new ActionAsPdf("GeneratePDF"); 
    } 

    public ActionResult GeneratePDF() 
    { 
     List<Person> persons = new List<Person>(); 
     persons.Add(new Person() { Age = "29", Name = "Rami1" }); 
     persons.Add(new Person() { Age = "28", Name = "Rami2" }); 
     return View("Index", persons); 
    } 

运行应用程序和导航到IndexNew控制器动作,一个PDF将出GeneratePDF的()动作来产生和,如下所示将通过浏览器下载 -

enter image description here

+1

顶部,你说你正在使用RazorPDF,稍后又说你正在使用Rotativa,你是否真的在这个例子中使用了两个? –

0

如果报告是同一应用程序中的另一个视图,则可以使用以下C#获取该视图的HTML字符串,然后将该HTML字符串转换为PDF以在缓冲区中生成PDF,以便将其保存在服务器上或发送到t o浏览器下载。该代码使用evo html to pdf converter for .net将HTML字符串转换为PDF。此方法的优点是会话数据在转换期间可用于报表视图中:

[HttpPost] 
public ActionResult ConvertPageInSameSessionToPdf(FormCollection collection) 
{ 
    object model = null; 
    ViewDataDictionary viewData = new ViewDataDictionary(model); 

    // The string writer where to render the HTML code of the view 
    StringWriter stringWriter = new StringWriter(); 

    // Render the Index view in a HTML string 
    ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, "Report_View", null); 
    ViewContext viewContext = new ViewContext(
      ControllerContext, 
      viewResult.View, 
      viewData, 
      new TempDataDictionary(), 
      stringWriter 
      ); 
    viewResult.View.Render(viewContext, stringWriter); 

    // Get the view HTML string 
    string htmlToConvert = stringWriter.ToString(); 

    // Get the base URL 
    String currentPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri; 
    String baseUrl = currentPageUrl.Substring(0, currentPageUrl.Length - "Report_View".Length); 

    // Convert the HTML string to a PDF document in a memory buffer 
    byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlToConvert, baseUrl); 

    // Send the PDF file to browser 
    FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); 
    fileResult.FileDownloadName = "Report.pdf"; 

    return fileResult; 
}