Cómo convertir vistas a PDFs en ASP NET Core MVC

How to Convert Views to PDFs in ASP.NET Core MVC

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

Quickstart: Effortlessly Convert CSHTML to PDF in ASP.NET Core

Transform your ASP.NET Core MVC Views to PDFs effortlessly using IronPDF. With just a single line of code, you can render your '.cshtml' files into high-quality PDF documents. Simplify your development process by integrating this functionality directly into your MVC application, allowing for seamless PDF generation from dynamic HTML views. Follow the guide to set up your environment and start converting today.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    // using IronPdf.Extensions.Mvc.Core
    new IronPdf.ChromePdfRenderer().RenderRazorViewToPdf(HttpContext, "Views/Home/Report.cshtml", model).SaveAs("report.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

ASP.NET Core Web App MVC (Model-View-Controller) is a web application provided by Microsoft for building web applications using ASP.NET Core.

  • Model: Represents data and business logic, manages data interactions, and communicates with data sources.
  • View: Presents the user interface, focuses on displaying data, and renders information to the user.
  • Controller: Handles user input, responds to requests, communicates with the Model, and orchestrates interactions between the Model and the View.

IronPDF simplifies the process of creating PDF files from Views within an ASP.NET Core MVC project. This makes PDF generation easy and direct in ASP.NET Core MVC.

IronPDF Extension Package

The IronPdf.Extensions.Mvc.Core package is an extension of the main IronPdf package. Both the IronPdf.Extensions.Mvc.Core and IronPdf packages are needed to render Views to PDF documents in an ASP.NET Core MVC.

Install-Package IronPdf.Extensions.Mvc.Core
C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf.Extensions.Mvc.Core

Render Views to PDFs

You'll need an ASP.NET Core Web App (Model-View-Controller) project to convert Views into PDF files.

Add a Model Class

  • Navigate to the "Models" folder.
  • Create a new C# class file named "Person." This class will act as a model to represent individual data. Use the following code snippet:
:path=/static-assets/pdf/content-code-examples/how-to/cshtml-to-pdf-mvc-core-model.cs
namespace ViewToPdfMVCCoreSample.Models
{
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
    }
}
Namespace ViewToPdfMVCCoreSample.Models
	Public Class Person
		Public Property Id() As Integer
		Public Property Name() As String
		Public Property Title() As String
		Public Property Description() As String
	End Class
End Namespace
$vbLabelText   $csharpLabel

Edit the Controller

Navigate to the "Controllers" folder and open the "HomeController" file. We are going to make changes only to the HomeController and add the "Persons" action. Please refer to the code below for guidance:

The code below first instantiates the ChromePdfRenderer class, passing an IRazorViewRenderer, the path to our "Persons.cshtml," and the List that contains the required data to the RenderRazorViewToPdf method. Users can utilize RenderingOptions to access a range of features, such as adding custom text, including HTML headers and footers in the resulting PDF, defining custom margins, and applying page numbers.

Por favor notaThe PDF document can be viewed in the browser using the following code: File(pdf.BinaryData, "application/pdf"). However, downloading the PDF after viewing it in the browser results in a damaged PDF document.

using IronPdf.Extensions.Mvc.Core;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using ViewToPdfMVCCoreSample.Models;

namespace ViewToPdfMVCCoreSample.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IRazorViewRenderer _viewRenderService;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public HomeController(ILogger<HomeController> logger, IRazorViewRenderer viewRenderService, IHttpContextAccessor httpContextAccessor)
        {
            _logger = logger;
            _viewRenderService = viewRenderService;
            _httpContextAccessor = httpContextAccessor;
        }

        public IActionResult Index()
        {
            return View();
        }

        public async Task<IActionResult> Persons()
        {
            // Example list of 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" }
            };

            // Check if the request method is POST
            if (_httpContextAccessor.HttpContext.Request.Method == HttpMethod.Post.Method)
            {
                // Create a new PDF renderer
                ChromePdfRenderer renderer = new ChromePdfRenderer();

                // Render View to PDF document
                PdfDocument pdf = renderer.RenderRazorViewToPdf(_viewRenderService, "Views/Home/Persons.cshtml", persons);

                Response.Headers.Add("Content-Disposition", "inline");

                // Output PDF document
                return File(pdf.BinaryData, "application/pdf", "viewToPdfMVCCore.pdf");
            }

            return View(persons);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
using IronPdf.Extensions.Mvc.Core;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using ViewToPdfMVCCoreSample.Models;

namespace ViewToPdfMVCCoreSample.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IRazorViewRenderer _viewRenderService;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public HomeController(ILogger<HomeController> logger, IRazorViewRenderer viewRenderService, IHttpContextAccessor httpContextAccessor)
        {
            _logger = logger;
            _viewRenderService = viewRenderService;
            _httpContextAccessor = httpContextAccessor;
        }

        public IActionResult Index()
        {
            return View();
        }

        public async Task<IActionResult> Persons()
        {
            // Example list of 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" }
            };

            // Check if the request method is POST
            if (_httpContextAccessor.HttpContext.Request.Method == HttpMethod.Post.Method)
            {
                // Create a new PDF renderer
                ChromePdfRenderer renderer = new ChromePdfRenderer();

                // Render View to PDF document
                PdfDocument pdf = renderer.RenderRazorViewToPdf(_viewRenderService, "Views/Home/Persons.cshtml", persons);

                Response.Headers.Add("Content-Disposition", "inline");

                // Output PDF document
                return File(pdf.BinaryData, "application/pdf", "viewToPdfMVCCore.pdf");
            }

            return View(persons);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
Imports IronPdf.Extensions.Mvc.Core
Imports Microsoft.AspNetCore.Mvc
Imports System.Diagnostics
Imports ViewToPdfMVCCoreSample.Models

Namespace ViewToPdfMVCCoreSample.Controllers
	Public Class HomeController
		Inherits Controller

		Private ReadOnly _logger As ILogger(Of HomeController)
		Private ReadOnly _viewRenderService As IRazorViewRenderer
		Private ReadOnly _httpContextAccessor As IHttpContextAccessor

		Public Sub New(ByVal logger As ILogger(Of HomeController), ByVal viewRenderService As IRazorViewRenderer, ByVal httpContextAccessor As IHttpContextAccessor)
			_logger = logger
			_viewRenderService = viewRenderService
			_httpContextAccessor = httpContextAccessor
		End Sub

		Public Function Index() As IActionResult
			Return View()
		End Function

		Public Async Function Persons() As Task(Of IActionResult)
			' Example list of persons
'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"
				}
			}

			' Check if the request method is POST
			If _httpContextAccessor.HttpContext.Request.Method = HttpMethod.Post.Method Then
				' Create a new PDF renderer
				Dim renderer As New ChromePdfRenderer()

				' Render View to PDF document
				Dim pdf As PdfDocument = renderer.RenderRazorViewToPdf(_viewRenderService, "Views/Home/Persons.cshtml", persons_Conflict)

				Response.Headers.Add("Content-Disposition", "inline")

				' Output PDF document
				Return File(pdf.BinaryData, "application/pdf", "viewToPdfMVCCore.pdf")
			End If

			Return View(persons_Conflict)
		End Function

		Public Function Privacy() As IActionResult
			Return View()
		End Function

		<ResponseCache(Duration := 0, Location := ResponseCacheLocation.None, NoStore := True)>
		Public Function [Error]() As IActionResult
			Return View(New ErrorViewModel With {.RequestId = If(Activity.Current?.Id, HttpContext.TraceIdentifier)})
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

After using the RenderRazorViewToPdf method, you'll receive a PdfDocument object that's open to further enhancements and modifications. You have the flexibility to convert the PDF to PDF/A or PDF/UA formats, add your digital signature to the generated PDF, or merge and split PDF documents as needed. Additionally, the library allows you to rotate pages, insert annotations or bookmarks, and imprint unique watermarks onto your PDF files.

Add a View

  • Right-click on the newly added Person action and select "Add View."

Right-click on Persons action

  • Choose "Razor View" for the new Scaffolded item.

Select scaffold

  • Select the "List" template and the "Person" model class.

Add view

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" />
}
HTML

Add a Section to the Top Navigation Bar

  • In the same "Views" folder navigate to the "Shared" folder -> _Layout.cshtml. Place the "Person" navigation item after "Home".

Make sure the value for the asp-action attribute matches exactly with our file name, which in this case is "Persons".

<header>
    <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
        <div class="container-fluid">
            <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">ViewToPdfMVCCoreSample</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                    aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                <ul class="navbar-nav flex-grow-1">
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Persons">Person</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</header>
<header>
    <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
        <div class="container-fluid">
            <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">ViewToPdfMVCCoreSample</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                    aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                <ul class="navbar-nav flex-grow-1">
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Persons">Person</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</header>
HTML

Edit Program.cs File

We will be registering the IHttpContextAccessor and IRazorViewRenderer interface to the dependency injection (DI) container. Please check the code below for your reference.

using IronPdf.Extensions.Mvc.Core;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();

// Register IRazorViewRenderer here
builder.Services.AddSingleton<IRazorViewRenderer, RazorViewRenderer>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
using IronPdf.Extensions.Mvc.Core;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();

// Register IRazorViewRenderer here
builder.Services.AddSingleton<IRazorViewRenderer, RazorViewRenderer>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
Imports IronPdf.Extensions.Mvc.Core
Imports Microsoft.AspNetCore.Mvc.ViewFeatures

Private builder = WebApplication.CreateBuilder(args)

' Add services to the container.
builder.Services.AddControllersWithViews()

builder.Services.AddSingleton(Of IHttpContextAccessor, HttpContextAccessor)()
builder.Services.AddSingleton(Of ITempDataProvider, CookieTempDataProvider)()

' Register IRazorViewRenderer here
builder.Services.AddSingleton(Of IRazorViewRenderer, RazorViewRenderer)()

Dim app = builder.Build()

' Configure the HTTP request pipeline.
If Not app.Environment.IsDevelopment() Then
	app.UseExceptionHandler("/Home/Error")
	' The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
	app.UseHsts()
End If

app.UseHttpsRedirection()
app.UseStaticFiles()

app.UseRouting()

app.UseAuthorization()

app.MapControllerRoute(name:= "default", pattern:= "{controller=Home}/{action=Index}/{id?}")

app.Run()
$vbLabelText   $csharpLabel

Run the Project

This will show you how to run the project and generate a PDF document.

Execute ASP.NET Core MVC Project

Download ASP.NET Core 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 an ASP.NET Core Web App (Model-View-Controller) project.

Download the ASP.NET Core MVC Sample Project

Ready to see what else you can do? Check out our tutorial page here: Convert PDFs

Preguntas Frecuentes

¿Cómo puedo convertir CSHTML a PDF en ASP.NET Core MVC?

Puedes usar el método `RenderRazorViewToPdf` de IronPDF para convertir archivos CSHTML a PDFs dentro de un proyecto ASP.NET Core MVC.

¿Qué pasos se requieren para configurar la generación de PDFs en un proyecto ASP.NET Core MVC?

Para configurar la generación de PDFs, descarga la biblioteca IronPDF a través de NuGet, crea una clase de modelo, edita el HomeController para usar el método `RenderRazorViewToPdf`, y configura la inyección de dependencias para `IHttpContextAccessor` e `IRazorViewRenderer` en el archivo 'Program.cs'.

¿Por qué es necesaria la inyección de dependencias para la conversión de PDFs en ASP.NET Core?

La inyección de dependencias para `IHttpContextAccessor` e `IRazorViewRenderer` es necesaria para renderizar adecuadamente las Vistas como PDFs, asegurando que el contexto de renderizado de Razor View esté correctamente establecido.

¿Puedo personalizar la salida de PDF al convertir Vistas en ASP.NET Core MVC?

Sí, usando IronPDF, puedes personalizar la salida de PDF ajustando encabezados, pies de página, márgenes y otras configuraciones dentro del método `RenderRazorViewToPdf`.

¿Cuál es el papel del método `RenderRazorViewToPdf` en la conversión de PDF?

El método `RenderRazorViewToPdf` es esencial para convertir Razor Views a documentos PDF, proporcionando opciones para personalizar la apariencia y el contenido del PDF.

¿Cómo puedo asegurarme de que mi proyecto ASP.NET Core MVC soporta la conversión de PDFs?

Asegúrate de que tu proyecto soporte la conversión de PDFs instalando el paquete IronPdf.Extensions.Mvc.Core y configurando los servicios y modelos necesarios dentro de tu proyecto.

¿Es posible añadir elementos interactivos a los PDFs generados desde Vistas?

Sí, después de generar un PDF usando IronPDF, puedes añadir elementos interactivos como formularios, enlaces y marcadores para mejorar la interacción del usuario dentro del documento.

¿Dónde puedo encontrar un proyecto de ejemplo para convertir Vistas a PDFs?

Puedes descargar un proyecto de ejemplo provisto en la guía, que demuestra la configuración completa para convertir Vistas a PDFs en una aplicación web ASP.NET Core.

¿Cómo instalo IronPDF en mi proyecto ASP.NET Core MVC?

Instala IronPDF usando el gestor de paquetes NuGet con el comando: Install-Package IronPdf.Extensions.Mvc.Core.

¿IronPDF es compatible con .NET 10 para convertir vistas CSHTML a PDF?

Sí: IronPDF es totalmente compatible con .NET 10 y admite la representación de vistas CSHTML en PDF mediante sus API existentes como `RenderRazorViewToPdf`, sin necesidad de configuración adicional.

Chaknith Bin
Ingeniero de Software
Chaknith trabaja en IronXL e IronBarcode. Tiene un profundo conocimiento en C# y .NET, ayudando a mejorar el software y apoyar a los clientes. Sus conocimientos derivados de las interacciones con los usuarios contribuyen a mejores productos, documentación y experiencia en general.
Revisado por
Jeff Fritz
Jeffrey T. Fritz
Gerente Principal de Programas - Equipo de la Comunidad .NET
Jeff también es Gerente Principal de Programas para los equipos de .NET y Visual Studio. Es el productor ejecutivo de la serie de conferencias virtuales .NET Conf y anfitrión de 'Fritz and Friends', una transmisión en vivo para desarrolladores que se emite dos veces a la semana donde habla sobre tecnología y escribe código junto con la audiencia. Jeff escribe talleres, presentaciones, y planifica contenido para los eventos de desarrolladores más importantes de Microsoft, incluyendo Microsoft Build, Microsoft Ignite, .NET Conf y la Cumbre de Microsoft MVP.
¿Listo para empezar?
Nuget Descargas 16,154,058 | Versión: 2025.11 recién lanzado