How to Convert Razor Views to PDFs Headlessly

by Chaknith Bin

The term 'headless rendering' refers to the process of rendering web content without a graphical user interface (GUI) or browser window. While the IronPdf.Extensions.Razor package is very useful, it does not offer headless rendering capabilities. Headless rendering can address the use case gap that the IronPdf.Extensions.Razor package cannot fulfill.

We will utilize the Razor.Templating.Core package to convert from cshtml (Razor Views) to html, and then utilize IronPDF to generate PDF documents from it.

This article is inspired by the following YouTube video:

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer


Install the Razor.Templating.Core package to convert Razor Views to html documents in an ASP.NET Core Web App.

Install-Package Razor.Templating.Core

Render Razor Views to PDFs

You'll need an ASP.NET Core Web App (Model-View-Controller) project to convert Views into PDF files.

Add a View

  • Right-click on the "Home" folder. Choose "add" and "Add View."
  • Create an empty Razor View and name it "Data.cshtml".

Add view

Edit Data.cshtml File

Add the HTML string that you would like to render to PDF:

<table class="table">
    <tr>
        <th>Name</th>
        <th>Title</th>
        <th>Description</th>
    </tr>
    <tr>
        <td>John Doe</td>
        <td>Software Engineer</td>
        <td>Experienced software engineer specializing in web development.</td>
    </tr>
    <tr>
        <td>Alice Smith</td>
        <td>Project Manager</td>
        <td>Seasoned project manager with expertise in agile methodologies.</td>
    </tr>
    <tr>
        <td>Michael Johnson</td>
        <td>Data Analyst</td>
        <td>Skilled data analyst proficient in statistical analysis and data visualization.</td>
    </tr>
</table>
<table class="table">
    <tr>
        <th>Name</th>
        <th>Title</th>
        <th>Description</th>
    </tr>
    <tr>
        <td>John Doe</td>
        <td>Software Engineer</td>
        <td>Experienced software engineer specializing in web development.</td>
    </tr>
    <tr>
        <td>Alice Smith</td>
        <td>Project Manager</td>
        <td>Seasoned project manager with expertise in agile methodologies.</td>
    </tr>
    <tr>
        <td>Michael Johnson</td>
        <td>Data Analyst</td>
        <td>Skilled data analyst proficient in statistical analysis and data visualization.</td>
    </tr>
</table>
HTML

Edit Program.cs File

Inside the "Program.cs" file, add the following code. The code below uses the RenderAsync method from the Razor.Templating.Core library to convert Razor Views to HTML. Secondly, it instantiates the ChromePdfRenderer class and passes the returned HTML string to the RenderHtmlAsPdf method. Users can utilize RenderingOptions to access a range of features, such as adding custom text, including HTML headers and footers in the resulting PDF, defining custom margins, and applying page numbers.

app.MapGet("/PrintPdf", async () =>
{
    IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

    string html = await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml");

    ChromePdfRenderer renderer = new ChromePdfRenderer();
    PdfDocument pdf = renderer.RenderHtmlAsPdf(html, "./wwwroot");

    return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf");
});
app.MapGet("/PrintPdf", async () =>
{
    IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

    string html = await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml");

    ChromePdfRenderer renderer = new ChromePdfRenderer();
    PdfDocument pdf = renderer.RenderHtmlAsPdf(html, "./wwwroot");

    return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf");
});
app.MapGet("/PrintPdf", Async Function()
	IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
	IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All

	Dim html As String = Await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml")

	Dim renderer As New ChromePdfRenderer()
	Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html, "./wwwroot")

	Return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf")
End Function)
VB   C#

Navigate to the "Views" folder -> "Shared" folder -> "_Layout.cshtml" file. In the link tags, change the "~/" to "./".

This is important because the "~/" does not work well with IronPDF.

Run the Project

This will show you how to run the project and generate a PDF document.

Execute ASP.NET Core MVC Project

Output PDF

Download ASP.NET Core MVC Project

You can download the complete code for this guide. It comes as a zipped file that you can open in Visual Studio as an ASP.NET Core Web App (Model-View-Controller) project.

Download the ASP.NET Core MVC Project Code

Chaknith related to Download ASP.NET Core MVC Project

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.