Razor to PDF (Blazor Server)
This example demonstrates how a Razor component can be converted into a PDF document.
The IronPdf.Extensions.Blazor package is an extension of the main IronPdf package. To convert a Razor component's content page to a PDF, both the IronPdf.Extensions.Blazor and the primary IronPdf packages are required.
In the code example below, a model called PersonInfo has already been created. In the OnInitializedAsync
method, we insert multiple new PersonInfo objects into the persons List. We then associate the persons List with the string "persons" in the Parameters dictionary.
In the PrintToPdf
method, we instantiate the ChromePdfRenderer class. To convert the Razor component to a PDF document, we use the RenderRazorComponentToPdf
method. In the code example, we pass the Parameters dictionary to the method for rendering.
@page "/pdfexample"
@using IronPdf.Extensions.Blazor
@using IronPdf
@inject ILogger<PDFExample> Logger
<h3>PDF Example</h3>
<button @onclick="PrintToPdf">Print to PDF</button>
@code {
// List to store PersonInfo objects
private List<PersonInfo> persons = new List<PersonInfo>();
// Parameters dictionary to bind data to the Razor component
private Dictionary<string, object> Parameters;
// Method called when the component is initialized
protected override async Task OnInitializedAsync()
{
// Populate the List with some example data
persons.Add(new PersonInfo { Name = "John Doe", Age = 30 });
persons.Add(new PersonInfo { Name = "Jane Doe", Age = 28 });
// Initialize Parameters dictionary with the List of PersonInfo
Parameters = new Dictionary<string, object> { { "persons", persons } };
await base.OnInitializedAsync();
}
// Method to print the Razor component to PDF
private async Task PrintToPdf()
{
try
{
// Instantiate the ChromePdfRenderer class, which is used to render HTML to PDF
var renderer = new ChromePdfRenderer();
// Render the Razor component to a PDF file
var pdfDocument = await renderer.RenderRazorComponentToPdfAsync<PDFExample>(Parameters);
// Save the generated PDF to the desktop
pdfDocument.SaveAs("C:\\Users\\YourUsername\\Desktop\\RazorComponent.pdf");
// Log success message
Logger.LogInformation("PDF has been created and saved successfully.");
}
catch (Exception ex)
{
// Log any exceptions that occur during the PDF generation
Logger.LogError(ex, "An error occurred while creating the PDF.");
}
}
}
// Example model class to store person information
public class PersonInfo
{
public string Name { get; set; }
public int Age { get; set; }
}
@page "/pdfexample"
@using IronPdf.Extensions.Blazor
@using IronPdf
@inject ILogger<PDFExample> Logger
<h3>PDF Example</h3>
<button @onclick="PrintToPdf">Print to PDF</button>
@code {
// List to store PersonInfo objects
private List<PersonInfo> persons = new List<PersonInfo>();
// Parameters dictionary to bind data to the Razor component
private Dictionary<string, object> Parameters;
// Method called when the component is initialized
protected override async Task OnInitializedAsync()
{
// Populate the List with some example data
persons.Add(new PersonInfo { Name = "John Doe", Age = 30 });
persons.Add(new PersonInfo { Name = "Jane Doe", Age = 28 });
// Initialize Parameters dictionary with the List of PersonInfo
Parameters = new Dictionary<string, object> { { "persons", persons } };
await base.OnInitializedAsync();
}
// Method to print the Razor component to PDF
private async Task PrintToPdf()
{
try
{
// Instantiate the ChromePdfRenderer class, which is used to render HTML to PDF
var renderer = new ChromePdfRenderer();
// Render the Razor component to a PDF file
var pdfDocument = await renderer.RenderRazorComponentToPdfAsync<PDFExample>(Parameters);
// Save the generated PDF to the desktop
pdfDocument.SaveAs("C:\\Users\\YourUsername\\Desktop\\RazorComponent.pdf");
// Log success message
Logger.LogInformation("PDF has been created and saved successfully.");
}
catch (Exception ex)
{
// Log any exceptions that occur during the PDF generation
Logger.LogError(ex, "An error occurred while creating the PDF.");
}
}
}
// Example model class to store person information
public class PersonInfo
{
public string Name { get; set; }
public int Age { get; set; }
}
'INSTANT VB TODO TASK: The following line could not be converted:
page "/pdfexample" [using] IronPdf.Extensions.Blazor [using] IronPdf inject ILogger(Of PDFExample) Logger (Of h3) PDF Example</h3> <button onclick="PrintToPdf"> Print [to] PDF</button> code
If True Then
' List to store PersonInfo objects
private List(Of PersonInfo) persons = New List(Of PersonInfo)()
' Parameters dictionary to bind data to the Razor component
private Dictionary(Of String, Object) Parameters
' Method called when the component is initialized
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
' protected override async Task OnInitializedAsync()
' {
' ' Populate the List with some example data
' persons.Add(New PersonInfo { Name = "John Doe", Age = 30 });
' persons.Add(New PersonInfo { Name = "Jane Doe", Age = 28 });
'
' ' Initialize Parameters dictionary with the List of PersonInfo
' Parameters = New Dictionary<string, object> { { "persons", persons } };
'
' await MyBase.OnInitializedAsync();
' }
' Method to print the Razor component to PDF
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
' private async Task PrintToPdf()
' {
' try
' {
' ' Instantiate the ChromePdfRenderer class, which is used to render HTML to PDF
' var renderer = New ChromePdfRenderer();
'
' ' Render the Razor component to a PDF file
' var pdfDocument = await renderer.RenderRazorComponentToPdfAsync<PDFExample>(Parameters);
'
' ' Save the generated PDF to the desktop
' pdfDocument.SaveAs("C:\Users\YourUsername\Desktop\RazorComponent.pdf");
'
' ' Log success message
' Logger.LogInformation("PDF has been created and saved successfully.");
' }
' catch (Exception ex)
' {
' ' Log any exceptions that occur during the PDF generation
' Logger.LogError(ex, "An error occurred while creating the PDF.");
' }
' }
End If
' Example model class to store person information
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'public class PersonInfo
'{
' public string Name
' {
' get;
' set;
' }
' public int Age
' {
' get;
' set;
' }
'}
How to Convert Razor Components to PDFs in Blazor Server
- Install IronPDF from NuGet for Razor component conversion in C#
- Prepare a model to store the information
- Instantiate the
ChromePdfRenderer
class - Pass the
Dictionary
object containing the data to theRenderRazorComponentToPdfAsync
method - Download the resulting PDF document to the desktop