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; }
}
Imports System.Web ' For HttpContext
Imports IronPdf
Imports IronPdf.Rendering
Imports IronPdf.Rendering.Abstractions

Public Class PdfConverter
	Public Sub ConvertViewToPdf()
		' Instantiate the ChromePdfRenderer class
		Dim renderer = New ChromePdfRenderer()

		' Sample HttpContext and path to the .cshtml file
		Dim httpContext As HttpContext = System.Web.HttpContext.Current
		Dim cshtmlPath As String = "/path/to/view.cshtml"

		' Rendering the view into a PDF
		' Assuming some data model is available for rendering the Razor view
		Dim dataModel = New SomeDataModel()

		' Render the view
		' The method takes HttpContext, the cshtml path, and an optional data object to populate the view
		Dim pdf As PdfDocument = renderer.RenderView(httpContext, cshtmlPath, dataModel)

		' Optionally save the PDF to the file system or present it in the browser
		pdf.SaveAs("output.pdf")
	End Sub
End Class

' Example model class used for rendering
Public Class SomeDataModel
	' Define properties that represent the data to be injected into the .cshtml view
	Public Property Name() As String
	Public Property Age() As Integer
End Class
$vbLabelText   $csharpLabel