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.
using System.Web; // For HttpContext
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Rendering.Abstractions;
public class PdfConverter
{
public void ConvertViewToPdf()
{
// Instantiate the ChromePdfRenderer class
var renderer = new ChromePdfRenderer();
// Sample HttpContext and path to the .cshtml file
HttpContext httpContext = HttpContext.Current;
string cshtmlPath = "/path/to/view.cshtml";
// Rendering the view into a PDF
// Assuming some data model is available for rendering the Razor view
var dataModel = new SomeDataModel();
// Render the view
// The method takes HttpContext, the cshtml path, and an optional data object to populate the view
PdfDocument pdf = renderer.RenderView(httpContext, cshtmlPath, dataModel);
// Optionally save the PDF to the file system or present it in the browser
pdf.SaveAs("output.pdf");
}
}
// Example model class used for rendering
public class SomeDataModel
{
// Define properties that represent the data to be injected into the .cshtml view
public string Name { get; set; }
public int Age { get; set; }
}
using System.Web; // For HttpContext
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Rendering.Abstractions;
public class PdfConverter
{
public void ConvertViewToPdf()
{
// Instantiate the ChromePdfRenderer class
var renderer = new ChromePdfRenderer();
// Sample HttpContext and path to the .cshtml file
HttpContext httpContext = HttpContext.Current;
string cshtmlPath = "/path/to/view.cshtml";
// Rendering the view into a PDF
// Assuming some data model is available for rendering the Razor view
var dataModel = new SomeDataModel();
// Render the view
// The method takes HttpContext, the cshtml path, and an optional data object to populate the view
PdfDocument pdf = renderer.RenderView(httpContext, cshtmlPath, dataModel);
// Optionally save the PDF to the file system or present it in the browser
pdf.SaveAs("output.pdf");
}
}
// Example model class used for rendering
public class SomeDataModel
{
// Define properties that represent the data to be injected into the .cshtml view
public string Name { get; set; }
public int Age { get; set; }
}
How to Convert CSHTML Files to PDFs in C#
- Install the IronPDF C# library for converting CSHTML files to PDFs in the ASP.NET MVC Framework
- Instantiate the
ChromePdfRenderer
class - Pass the current
HttpContext
and the CSHTML file path to theRenderView
method - View the PDF document in the browser or download it to the desktop