How to Convert Views to PDFs in ASP.NET MVC
A View is a component in the ASP.NET framework used for generating HTML markup in web applications. It is a part of the Model-View-Controller (MVC) pattern, commonly used in ASP.NET MVC and ASP.NET Core MVC applications. Views are responsible for presenting data to the user by rendering HTML content dynamically.
ASP.NET Web Application (.NET Framework) MVC is a web application framework provided by Microsoft. It follows a structured architectural pattern known as Model-View-Controller (MVC) to organize and streamline the development of web applications.
- Model: Manages data, business logic, and data integrity.
- View: Presents the user interface and renders information.
- Controller: Handles user input, processes requests, and orchestrates interactions between the Model and View.
IronPDF simplifies the process of creating PDF files from Views within an ASP.NET MVC project. This makes PDF generation easy and direct in ASP.NET MVC.
How to Convert Views to PDFs in ASP.NET MVC
IronPDF Extension Package
The IronPdf.Extensions.Mvc.Framework package is an extension of the main IronPdf package. Both the IronPdf.Extensions.Mvc.Framework and IronPdf packages are required to render Views to PDF documents in an ASP.NET MVC.
Install-Package IronPdf.Extensions.Mvc.Framework
Install with NuGet
Install-Package IronPdf.Extensions.Mvc.Framework
Render Views to PDFs
To convert Views into PDF files, you will need an ASP.NET Web Application (.NET Framework) MVC project.
Add a Model Class
- Navigate to the "Models" folder
- Create a new C# class file named "Person." This class will serve as a model to represent individual data. Use the following placeholder for guidance:
:path=/static-assets/pdf/content-code-examples/how-to/cshtml-to-pdf-mvc-framework-model.cs
// The following code defines a namespace for an MVC sample project related to converting views to PDFs. // This namespace contains a single class, Person, which represents a model in an MVC (Model-View-Controller) framework. // The Person class is typically used to represent data in a neatly organized way, such as information about an individual. namespace ViewToPdfMVCSample.Models { // The Person class is defined as a public class so that it can be accessed by other parts of the application. // This class contains properties typically associated with a person. public class Person { // Property representing the unique identifier for a person. // The 'Id' property is of type 'int', which can hold an integer value. public int Id { get; set; } // Property for the name of the person. // The 'Name' property is a string, which is suitable for holding text. public string Name { get; set; } // Property for the professional title of the person, like 'Mr.', 'Dr.', or job titles. // This 'Title' property is a string to allow various professional or personal titles. public string Title { get; set; } // Property for the description of the person. // This can be used for additional notes or information about the person and is set as a string. public string Description { get; set; } } }
' The following code defines a namespace for an MVC sample project related to converting views to PDFs. ' This namespace contains a single class, Person, which represents a model in an MVC (Model-View-Controller) framework. ' The Person class is typically used to represent data in a neatly organized way, such as information about an individual. Namespace ViewToPdfMVCSample.Models ' The Person class is defined as a public class so that it can be accessed by other parts of the application. ' This class contains properties typically associated with a person. Public Class Person ' Property representing the unique identifier for a person. ' The 'Id' property is of type 'int', which can hold an integer value. Public Property Id() As Integer ' Property for the name of the person. ' The 'Name' property is a string, which is suitable for holding text. Public Property Name() As String ' Property for the professional title of the person, like 'Mr.', 'Dr.', or job titles. ' This 'Title' property is a string to allow various professional or personal titles. Public Property Title() As String ' Property for the description of the person. ' This can be used for additional notes or information about the person and is set as a string. Public Property Description() As String End Class End Namespace
Edit the Controller
Navigate to the "Controllers" folder and open the "HomeController" file. We are going to add the "Persons" action. Please refer to the code below for guidance:
In the provided code, the ChromePdfRenderer class is first created. To use the RenderView
method, you'll need to provide it with an HttpContext, specify the path to the "Persons.cshtml" file, and provide a List containing the necessary data. When rendering the View, users have the option to utilize RenderingOptions to customize margins, add custom text and HTML headers and footers, and apply page numbers to the resulting PDF document.
Please note
File(pdf.BinaryData, "application/pdf", "viewToPdfMVC.pdf")
.using IronPdf; using System.Collections.Generic; using System.Web.Mvc; using ViewToPdfMVCSample.Models; namespace ViewToPdfMVCSample.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } // GET: Person public ActionResult Persons() { // Create a list of Person objects var persons = new List<Person> { new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" }, new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" }, new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" } }; if (HttpContext.Request.HttpMethod == "POST") { // Define the path to the View file var viewPath = "~/Views/Home/Persons.cshtml"; // Instantiate the ChromePdfRenderer ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render the view to a PDF document PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons); // Set headers to view the PDF in-browser Response.Headers.Add("Content-Disposition", "inline"); // Return the generated PDF file return File(pdf.BinaryData, "application/pdf"); } return View(persons); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } }
using IronPdf; using System.Collections.Generic; using System.Web.Mvc; using ViewToPdfMVCSample.Models; namespace ViewToPdfMVCSample.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } // GET: Person public ActionResult Persons() { // Create a list of Person objects var persons = new List<Person> { new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" }, new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" }, new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" } }; if (HttpContext.Request.HttpMethod == "POST") { // Define the path to the View file var viewPath = "~/Views/Home/Persons.cshtml"; // Instantiate the ChromePdfRenderer ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render the view to a PDF document PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons); // Set headers to view the PDF in-browser Response.Headers.Add("Content-Disposition", "inline"); // Return the generated PDF file return File(pdf.BinaryData, "application/pdf"); } return View(persons); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } }
Imports IronPdf Imports System.Collections.Generic Imports System.Web.Mvc Imports ViewToPdfMVCSample.Models Namespace ViewToPdfMVCSample.Controllers Public Class HomeController Inherits Controller Public Function Index() As ActionResult Return View() End Function ' GET: Person Public Function Persons() As ActionResult ' Create a list of Person objects 'INSTANT VB NOTE: The local variable persons was renamed since Visual Basic will not allow local variables with the same name as their enclosing function or property: Dim persons_Conflict = New List(Of Person) From { New Person With { .Name = "Alice", .Title = "Mrs.", .Description = "Software Engineer" }, New Person With { .Name = "Bob", .Title = "Mr.", .Description = "Software Engineer" }, New Person With { .Name = "Charlie", .Title = "Mr.", .Description = "Software Engineer" } } If HttpContext.Request.HttpMethod = "POST" Then ' Define the path to the View file Dim viewPath = "~/Views/Home/Persons.cshtml" ' Instantiate the ChromePdfRenderer Dim renderer As New ChromePdfRenderer() ' Render the view to a PDF document Dim pdf As PdfDocument = renderer.RenderView(Me.HttpContext, viewPath, persons_Conflict) ' Set headers to view the PDF in-browser Response.Headers.Add("Content-Disposition", "inline") ' Return the generated PDF file Return File(pdf.BinaryData, "application/pdf") End If Return View(persons_Conflict) End Function Public Function About() As ActionResult ViewBag.Message = "Your application description page." Return View() End Function Public Function Contact() As ActionResult ViewBag.Message = "Your contact page." Return View() End Function End Class End Namespace
Once you obtain the PdfDocument object through the RenderView
method, you can make various improvements and adjustments to it. You can convert the PDF to PDFA or PDFUA formats, sign digital signature to the created PDF, or merge and split PDF documents as required. Moreover, the library enables you to rotate pages, insert annotations or bookmarks, and apply distinct watermarks to your PDF files.
Add a View
- Right-click on the newly added Person action and select "Add View."
- Choose "MVC 5 View" for the new Scaffolded item.
- Select the "List" template and the "Person" model class.
This will create a .cshtml file named "Persons."
- Navigate to the "Views" folder -> "Home" folder -> "Persons.cshtml" file.
To add a button that invokes the "Persons" action, use the code below:
@using (Html.BeginForm("Persons", "Home", FormMethod.Post)) { <input type="submit" value="Print Person" /> }
@using (Html.BeginForm("Persons", "Home", FormMethod.Post)) { <input type="submit" value="Print Person" /> }
Add a Section to the Top Navigation Bar
- In the "Views" folder, navigate to the "Shared" folder -> "_Layout.cshtml" file. Place the "Person" navigation item after "Home."
Ensure that the values for the ActionLink method match exactly with our file name, which in this case is "Persons."
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark"> <div class="container"> @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }) <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" title="Toggle navigation" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse d-sm-inline-flex justify-content-between"> <ul class="navbar-nav flex-grow-1"> <li>@Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Persons", "Persons", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li> </ul> </div> </div> </nav>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark"> <div class="container"> @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }) <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" title="Toggle navigation" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse d-sm-inline-flex justify-content-between"> <ul class="navbar-nav flex-grow-1"> <li>@Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Persons", "Persons", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li> </ul> </div> </div> </nav>
Run the Project
This will show you how to run the project and generate a PDF document.

Output PDF
Download ASP.NET MVC Project
You can download the complete code for this guide. It comes as a zipped file that you can open in Visual Studio as a ASP.NET Web Application (.NET Framework) MVC project.
Download the MVC sample project for PDF conversion
Frequently Asked Questions
What is a View in ASP.NET MVC?
A View in ASP.NET MVC is a component used for generating HTML markup in web applications. It presents data to the user by rendering HTML content dynamically as part of the Model-View-Controller (MVC) pattern.
How can you simplify the process of creating PDF files from Views within an ASP.NET MVC project?
Using IronPDF simplifies the process of creating PDF files from Views within an ASP.NET MVC project, making PDF generation easy and direct.
What is the role of the Model in MVC?
In MVC, the Model manages data, business logic, and data integrity. It interacts with the database and processes the data to be presented by the View.
How can I install a package for rendering Views to PDF documents in ASP.NET MVC?
You can install the IronPdf.Extensions.Mvc.Framework package using NuGet with the command: Install-Package IronPdf.Extensions.Mvc.Framework.
What is used to render Views into PDF documents?
The ChromePdfRenderer class from IronPDF is used to render Views into PDF documents. It requires an HttpContext, the view path, and a data list to generate the PDF.
How do you add a model class in ASP.NET MVC?
To add a model class in ASP.NET MVC, navigate to the 'Models' folder, create a new C# class file, and define it with properties representing the data structure.
What is the command to render a view to a PDF document?
To render a view to a PDF document, use the RenderView method from the ChromePdfRenderer class, providing the HttpContext, view path, and data list.
How can you customize PDF output in a web application framework?
With IronPDF, you can customize PDF output using RenderingOptions to adjust margins, add custom headers and footers, and apply page numbers to the PDF document.
How do you add a View in ASP.NET MVC?
To add a View, right-click on the action in the controller and select 'Add View.' Choose 'MVC 5 View' as the scaffolded item and select the appropriate template and model class.
How can I download a sample project for converting Views to PDF?
You can download the complete MVC sample project for PDF conversion from the provided link on the webpage, which includes a zipped file to open in Visual Studio.