Reputation: 245
I have a report develop with asp.net mvc4 using HTML and I want to export this report to PDF file with asp.net MVC4 anybody help me please? thank!
Upvotes: 3
Views: 13671
Reputation: 523
If the report is another view in the same application you can use the following C# to get the HTML string of that view and then convert the HTML string to PDF to produce a PDF in a buffer that can be saved on server or sent to a browser for download. The code is using evo html to pdf converter for .net to convert the HTML string to PDF. The advantage of this approach is that the session data is available in report view during conversion:
[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;
}
Upvotes: 1
Reputation: 17182
Use Razor PDF for generating PDF Reports. Here goes simple process.
Step 1 - Create MVC application
Step 2 - Install Rotativa PDF Nuget.
Step 3 - Place a simple model like this
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Step 4 - Create a simple view for the model we created above, name the view as Index.
@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>
Step 5 - Create a simple controller action.
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); }
Run the application and navigate to IndexNew Controller Action, a PDF will be generated out of GeneratePDF() Action and will be downloaded by the browser as shown below -
Upvotes: 6
Reputation: 177
Take a look at this application:
http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC
and if you want to download the file directly you can do it with a 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");
}
Upvotes: 4