IRONSOFTWAREHOME
CSHTML to PDF (MVC Framework)

// PM > Install-Package IronPdf.Extensions.Mvc.Framework

public ActionResult Persons()
{
    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")
    {
        // Provide the path to your view file
        var viewPath = "~/Views/Home/Persons.cshtml";
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render Razor view to PDF document
        PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons);
        Response.Headers.Add("Content-Disposition", "inline");

        // View the PDF
        return File(pdf.BinaryData, "application/pdf");
    }
    return View(persons);
}
Public Function Persons() As ActionResult
    Dim persons = 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
        ' Provide the path to your view file
        Dim viewPath = "~/Views/Home/Persons.cshtml"
        Dim renderer As New ChromePdfRenderer()

        ' Render Razor view to PDF document
        Dim pdf As PdfDocument = renderer.RenderView(Me.HttpContext, viewPath, persons)
        Response.Headers.Add("Content-Disposition", "inline")

        ' View the PDF
        Return File(pdf.BinaryData, "application/pdf")
    End If

    Return View(persons)
End Function
NuGet Download
PM > Install-Package IronPdf
CSHTML to PDF (MVC Framework)

CSHTML to PDF (MVC Framework)

This code example illustrates how to convert Views into PDF documents.

To achieve this, we utilize two packages: IronPdf.Extensions.Mvc.Framework and IronPdf, which work to facilitate the rendering of Views as PDFs. The IronPdf.Extensions.Mvc.Framework package extends the capabilities of IronPdf, specifically enabling the rendering of Views into PDFs.

To perform the conversion, use the RenderView method. This method requires a few key inputs: an HttpContext, the path to the ".cshtml" file, and the necessary data to populate the ".cshtml" template. By invoking the Persons action, you can seamlessly render the current View into a PDF document.

Additionally, you have access to a comprehensive set of functionalities offered by the RenderingOptions class. These include the ability to add page numbers with IronPDF, insert text and HTML headers and footers using IronPDF, and customize the PDF paper size to your requirements. You have the flexibility to make further modifications or export the resulting PDF document as necessary.

Explore the CSHTML to PDF Conversion Guide
View on GitHub

Ready to Get Started?

Nuget Downloads 20,809,720Version:2026.9just released

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required