CSHTML to PDF (Razor Pages)
This code example demonstrates the process of converting a Razor page into PDFs.
To achieve this in an ASP.NET Core Web App, you'll require two packages: IronPdf.Extensions.Razor and IronPdf. These packages work together to enable the rendering of Razor pages into PDFs.
By rendering from Razor pages, you gain access to the full range of features provided by the RenderingOptions class. The resulting PDF document can be further edited or exported as needed.
How to Convert Razor Pages to PDFs in C#
- Install the IronPDF Library for Razor Page Conversion
- Instantiate the
ChromePdfRenderer
class - Pass the current Razor page to the
RenderRazorToPdf
method - View the PDF document in the browser or download it to the desktop
using IronPdf;
using Microsoft.AspNetCore.Mvc.RazorPages;
public class PdfModel : PageModel
{
public void OnGet()
{
// Create a new instance of ChromePdfRenderer that will be used to render Razor pages to PDF
var renderer = new ChromePdfRenderer();
// Optionally, you can set rendering options (e.g., margins, page size)
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.Title = "Sample PDF from Razor Page";
// Render the current Razor page to PDF
// Here, "this" refers to the current PageModel, which contains the Razor page's content
PdfDocument pdf = renderer.RenderRazorToPdf(this);
// Save the rendered PDF to a file on the server
pdf.SaveAs("SampleRazorPage.pdf");
// Display the PDF in the browser
// Response logic would normally be handled in a controller, demonstrating inline for simplicity
this.Response.ContentType = "application/pdf";
this.Response.Headers.Add("Content-Disposition", "inline; filename=SampleRazorPage.pdf");
this.Response.Body.Write(pdf.BinaryData, 0, pdf.BinaryData.Length);
// Optionally allow download of the PDF
// Commenting this section as it might not be needed for current scenarios; uncomment to use
// this.Response.Headers.Add("Content-Disposition", "attachment; filename=SampleRazorPage.pdf");
}
}
using IronPdf;
using Microsoft.AspNetCore.Mvc.RazorPages;
public class PdfModel : PageModel
{
public void OnGet()
{
// Create a new instance of ChromePdfRenderer that will be used to render Razor pages to PDF
var renderer = new ChromePdfRenderer();
// Optionally, you can set rendering options (e.g., margins, page size)
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.Title = "Sample PDF from Razor Page";
// Render the current Razor page to PDF
// Here, "this" refers to the current PageModel, which contains the Razor page's content
PdfDocument pdf = renderer.RenderRazorToPdf(this);
// Save the rendered PDF to a file on the server
pdf.SaveAs("SampleRazorPage.pdf");
// Display the PDF in the browser
// Response logic would normally be handled in a controller, demonstrating inline for simplicity
this.Response.ContentType = "application/pdf";
this.Response.Headers.Add("Content-Disposition", "inline; filename=SampleRazorPage.pdf");
this.Response.Body.Write(pdf.BinaryData, 0, pdf.BinaryData.Length);
// Optionally allow download of the PDF
// Commenting this section as it might not be needed for current scenarios; uncomment to use
// this.Response.Headers.Add("Content-Disposition", "attachment; filename=SampleRazorPage.pdf");
}
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc.RazorPages
Public Class PdfModel
Inherits PageModel
Public Sub OnGet()
' Create a new instance of ChromePdfRenderer that will be used to render Razor pages to PDF
Dim renderer = New ChromePdfRenderer()
' Optionally, you can set rendering options (e.g., margins, page size)
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
renderer.RenderingOptions.Title = "Sample PDF from Razor Page"
' Render the current Razor page to PDF
' Here, "this" refers to the current PageModel, which contains the Razor page's content
Dim pdf As PdfDocument = renderer.RenderRazorToPdf(Me)
' Save the rendered PDF to a file on the server
pdf.SaveAs("SampleRazorPage.pdf")
' Display the PDF in the browser
' Response logic would normally be handled in a controller, demonstrating inline for simplicity
Me.Response.ContentType = "application/pdf"
Me.Response.Headers.Add("Content-Disposition", "inline; filename=SampleRazorPage.pdf")
Me.Response.Body.Write(pdf.BinaryData, 0, pdf.BinaryData.Length)
' Optionally allow download of the PDF
' Commenting this section as it might not be needed for current scenarios; uncomment to use
' this.Response.Headers.Add("Content-Disposition", "attachment; filename=SampleRazorPage.pdf");
End Sub
End Class
In this example, the Razor page's content is rendered into a PDF using IronPdf's ChromePdfRenderer
. You can specify rendering options, such as paper size and title, through RenderingOptions
. The generated PDF can be saved as a file or directly served as content for browser display. The response headers can be adjusted to allow for inline display or file download.