Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this tutorial, we explore converting views to PDF in ASP.NET MVC using Iron PDF. We begin by setting up a personal model class featuring properties such as ID, name, title, and description. Moving to the Home controller, we create a sample list of individuals: Alice, Bob, and Charlie, all labeled as software engineers. The application detects a post request to trigger PDF generation, utilizing Iron PDF's Chrome renderer for high-quality output. The core function, render view, merges the list with the view to produce a PDF, which is then served to the user. The view includes a form that posts back to the person's action, initiating the PDF creation upon clicking the print person button. For easy access, a navigation link is added in the layout cshtml file. Running the project, users can click the persons option in the navbar, generate, and download the PDF, which details Alice, Bob, and Charlie. This guide aims to simplify the process of converting ASP.NET MVC views to PDF using Iron PDF, providing a practical solution for developers.
Here's an example of how you might set this up in an ASP.NET MVC application:
// PersonModel.cs
public class Person
{
public int ID { get; set; } // Unique Identifier for each person
public string Name { get; set; } // Name of the person
public string Title { get; set; } // Job title, e.g., "Software Engineer"
public string Description { get; set; } // A brief description of the person
}
// PersonModel.cs
public class Person
{
public int ID { get; set; } // Unique Identifier for each person
public string Name { get; set; } // Name of the person
public string Title { get; set; } // Job title, e.g., "Software Engineer"
public string Description { get; set; } // A brief description of the person
}
' PersonModel.cs
Public Class Person
Public Property ID() As Integer ' - Unique Identifier for each person
Public Property Name() As String ' - Name of the person
Public Property Title() As String ' - Job title, e.g., "Software Engineer"
Public Property Description() As String ' - A brief description of the person
End Class
// HomeController.cs
using System.Collections.Generic;
using System.Web.Mvc;
using IronPdf;
public class HomeController : Controller
{
// Action to display a list of persons
public ActionResult Index()
{
var people = new List<Person>
{
new Person { ID = 1, Name = "Alice", Title = "Software Engineer", Description = "Expert in C#" },
new Person { ID = 2, Name = "Bob", Title = "Software Engineer", Description = "Expert in Java" },
new Person { ID = 3, Name = "Charlie", Title = "Software Engineer", Description = "Expert in Python" }
};
return View(people);
}
// Action to generate PDF from a view
[HttpPost]
public ActionResult GeneratePdf()
{
// Fetch the view as a string and construct a PDF
var Renderer = new ChromePdfRenderer(); // Initialize IronPDF's Chrome renderer
var htmlContent = RenderRazorViewToString("Index"); // Get HTML content of the view
var pdf = Renderer.RenderHtmlAsPdf(htmlContent); // Render the content to PDF
return File(pdf.BinaryData, "application/pdf", "Persons.pdf"); // Return PDF file for download
}
// Utility method to render a view to string
private string RenderRazorViewToString(string viewName)
{
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindView(ControllerContext, viewName, null);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
}
// HomeController.cs
using System.Collections.Generic;
using System.Web.Mvc;
using IronPdf;
public class HomeController : Controller
{
// Action to display a list of persons
public ActionResult Index()
{
var people = new List<Person>
{
new Person { ID = 1, Name = "Alice", Title = "Software Engineer", Description = "Expert in C#" },
new Person { ID = 2, Name = "Bob", Title = "Software Engineer", Description = "Expert in Java" },
new Person { ID = 3, Name = "Charlie", Title = "Software Engineer", Description = "Expert in Python" }
};
return View(people);
}
// Action to generate PDF from a view
[HttpPost]
public ActionResult GeneratePdf()
{
// Fetch the view as a string and construct a PDF
var Renderer = new ChromePdfRenderer(); // Initialize IronPDF's Chrome renderer
var htmlContent = RenderRazorViewToString("Index"); // Get HTML content of the view
var pdf = Renderer.RenderHtmlAsPdf(htmlContent); // Render the content to PDF
return File(pdf.BinaryData, "application/pdf", "Persons.pdf"); // Return PDF file for download
}
// Utility method to render a view to string
private string RenderRazorViewToString(string viewName)
{
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindView(ControllerContext, viewName, null);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
}
' HomeController.cs
Imports System.Collections.Generic
Imports System.Web.Mvc
Imports IronPdf
Public Class HomeController
Inherits Controller
' Action to display a list of persons
Public Function Index() As ActionResult
Dim people = New List(Of Person) From {
New Person With {
.ID = 1,
.Name = "Alice",
.Title = "Software Engineer",
.Description = "Expert in C#"
},
New Person With {
.ID = 2,
.Name = "Bob",
.Title = "Software Engineer",
.Description = "Expert in Java"
},
New Person With {
.ID = 3,
.Name = "Charlie",
.Title = "Software Engineer",
.Description = "Expert in Python"
}
}
Return View(people)
End Function
' Action to generate PDF from a view
<HttpPost>
Public Function GeneratePdf() As ActionResult
' Fetch the view as a string and construct a PDF
Dim Renderer = New ChromePdfRenderer() ' Initialize IronPDF's Chrome renderer
Dim htmlContent = RenderRazorViewToString("Index") ' Get HTML content of the view
Dim pdf = Renderer.RenderHtmlAsPdf(htmlContent) ' Render the content to PDF
Return File(pdf.BinaryData, "application/pdf", "Persons.pdf") ' Return PDF file for download
End Function
' Utility method to render a view to string
Private Function RenderRazorViewToString(ByVal viewName As String) As String
Using sw = New StringWriter()
Dim viewResult = ViewEngines.Engines.FindView(ControllerContext, viewName, Nothing)
Dim viewContext As New ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw)
viewResult.View.Render(viewContext, sw)
Return sw.GetStringBuilder().ToString()
End Using
End Function
End Class
<!-- Index.cshtml -->
@model List<Person>
@{
ViewBag.Title = "Persons List";
}
<h2>Persons List</h2>
<form action="/Home/GeneratePdf" method="post">
<table>
<thead>
<tr>
<th>Name</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach (var person in Model)
{
<tr>
<td>@person.Name</td>
<td>@person.Title</td>
<td>@person.Description</td>
</tr>
}
</tbody>
</table>
<button type="submit">Print Persons</button>
</form>
<!-- Index.cshtml -->
@model List<Person>
@{
ViewBag.Title = "Persons List";
}
<h2>Persons List</h2>
<form action="/Home/GeneratePdf" method="post">
<table>
<thead>
<tr>
<th>Name</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach (var person in Model)
{
<tr>
<td>@person.Name</td>
<td>@person.Title</td>
<td>@person.Description</td>
</tr>
}
</tbody>
</table>
<button type="submit">Print Persons</button>
</form>
Update the layout file to include a link to the Persons page.
<!-- _Layout.cshtml -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar">
<a href="/">Home</a>
<a href="/Home/Index">Persons</a> <!-- Link to the Persons List Page -->
</div>
<div class="container body-content">
@RenderBody()
</div>
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>
<!-- _Layout.cshtml -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar">
<a href="/">Home</a>
<a href="/Home/Index">Persons</a> <!-- Link to the Persons List Page -->
</div>
<div class="container body-content">
@RenderBody()
</div>
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>
With these elements in place, running the project enables users to navigate to the persons page, print and download the list as a PDF file.
Further Reading: How to Convert Views to PDFs in ASP.NET MVC