CSHTML to PDF (MVC Core)
This code example demonstrates the process of converting Views into PDFs.
Two packages, IronPdf.Extensions.Mvc.Core
and IronPdf
, work together to enable the rendering of Views into PDFs. The IronPdf.Extensions.Mvc.Core
package serves as an extension to IronPdf
, enabling the rendering of Views to PDFs.
Use the RenderRazorViewToPdf
method to render Views to PDFs. This method requires an IRazorViewRenderer
, the path to the ".cshtml" file, and the data required to display on the ".cshtml" file. Please visit the How to Convert View to PDF in ASP.NET Core MVC how-to article to learn more.
This action also enables you to access the full range of features provided by the RenderingOptions class, such as applying page numbers in PDFs with IronPDF, adding text and HTML headers and footers with IronPDF, and customizing PDF paper size. The resulting PDF document can be further edited or exported as needed.
How to Convert CSHTML Files to PDFs in ASP.NET MVC Core
- Install the C# library for converting CSHTML files to PDFs in C#
- Instantiate the
ChromePdfRenderer
class - Use the
RenderRazorViewToPdf
method for rendering - Pass an
IRazorViewRenderer
object and the CSHTML file path to the method - Download the resulting PDF document to the desktop
using IronPdf; using System.IO; public class PdfController : Controller { // This method converts a Razor view to a PDF document public IActionResult GeneratePdfFromView() { // Instantiate a renderer for PDF generation var renderer = new ChromePdfRenderer(); // Define the path to the Razor view (CSHTML file) string viewPath = Path.Combine(Directory.GetCurrentDirectory(), "Views", "Example.cshtml"); // Optional: Define any data model required by the view var model = new YourDataModel(); // Render the Razor view to a PDF using var pdfDocument = renderer.RenderRazorViewToPdf(viewPath, model); // Set the file name for the resulting PDF var fileName = "Example.pdf"; // Send the PDF file as a response to be downloaded return File(pdfDocument.BinaryData, "application/pdf", fileName); } }
using IronPdf; using System.IO; public class PdfController : Controller { // This method converts a Razor view to a PDF document public IActionResult GeneratePdfFromView() { // Instantiate a renderer for PDF generation var renderer = new ChromePdfRenderer(); // Define the path to the Razor view (CSHTML file) string viewPath = Path.Combine(Directory.GetCurrentDirectory(), "Views", "Example.cshtml"); // Optional: Define any data model required by the view var model = new YourDataModel(); // Render the Razor view to a PDF using var pdfDocument = renderer.RenderRazorViewToPdf(viewPath, model); // Set the file name for the resulting PDF var fileName = "Example.pdf"; // Send the PDF file as a response to be downloaded return File(pdfDocument.BinaryData, "application/pdf", fileName); } }
Imports IronPdf Imports System.IO Public Class PdfController Inherits Controller ' This method converts a Razor view to a PDF document Public Function GeneratePdfFromView() As IActionResult ' Instantiate a renderer for PDF generation Dim renderer = New ChromePdfRenderer() ' Define the path to the Razor view (CSHTML file) Dim viewPath As String = Path.Combine(Directory.GetCurrentDirectory(), "Views", "Example.cshtml") ' Optional: Define any data model required by the view Dim model = New YourDataModel() ' Render the Razor view to a PDF Dim pdfDocument = renderer.RenderRazorViewToPdf(viewPath, model) ' Set the file name for the resulting PDF Dim fileName = "Example.pdf" ' Send the PDF file as a response to be downloaded Return File(pdfDocument.BinaryData, "application/pdf", fileName) End Function End Class