IronPDF How-Tos CSHTML to PDF (Headlessly) How to Convert Razor Views to PDFs Headlessly ByChaknith Bin May 12, 2024 Updated June 22, 2025 Share: 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. View the IronPDF YouTube Playlist Get started with IronPDF Start using IronPDF in your project today with a free trial. First Step: Start for Free How to Convert Razor Views to PDFs Headlessly Download the C# library for converting Razor Views to PDFs in ASP.NET Core Web App Create a new Razor View and edit the file to display the data Use the RenderAsync method to convert from Razor View to HTML Convert HTML to PDF using the RenderHtmlAsPdf method Download the sample project for a quick start Install the Razor.Templating.Core package to convert Razor Views to HTML documents in an ASP.NET Core Web App. # Install the Razor.Templating.Core package using NuGet Package Manager Install-Package Razor.Templating.Core # Install the Razor.Templating.Core package using NuGet Package Manager Install-Package Razor.Templating.Core SHELL 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". 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 () => { // Set your IronPDF license key IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"; // Enable detailed logging for troubleshooting IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All; // Render the Razor view to an HTML string string html = await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml"); // Create a new instance of ChromePdfRenderer ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render the HTML string as a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf(html, "./wwwroot"); // Return the PDF file as a response return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf"); }); app.MapGet("/PrintPdf", async () => { // Set your IronPDF license key IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"; // Enable detailed logging for troubleshooting IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All; // Render the Razor view to an HTML string string html = await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml"); // Create a new instance of ChromePdfRenderer ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render the HTML string as a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf(html, "./wwwroot"); // Return the PDF file as a response return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf"); }); app.MapGet("/PrintPdf", Async Function() ' Set your IronPDF license key IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01" ' Enable detailed logging for troubleshooting IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All ' Render the Razor view to an HTML string Dim html As String = Await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml") ' Create a new instance of ChromePdfRenderer Dim renderer As New ChromePdfRenderer() ' Render the HTML string as a PDF document Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html, "./wwwroot") ' Return the PDF file as a response Return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf") End Function) $vbLabelText $csharpLabel Change The Asset Links 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. 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. Click here to download the project. Frequently Asked Questions What is headless rendering? Headless rendering refers to the process of rendering web content without a graphical user interface (GUI) or browser window. Does the Razor package perform headless rendering? No, the IronPdf.Extensions.Razor package does not offer headless rendering capabilities. What package can be used to convert Razor Views to HTML? The Razor.Templating.Core package can be used to convert Razor Views to HTML. How do you convert HTML to a PDF in C#? You can use the RenderHtmlAsPdf method from the ChromePdfRenderer class in IronPDF to convert HTML to PDF. Do I need an ASP.NET Core Web App to convert Razor Views to PDFs? Yes, you need an ASP.NET Core Web App (Model-View-Controller) project to convert Views into PDF files. How do you add a new Razor View in an ASP.NET Core project? Right-click on the 'Home' folder, choose 'add', and then 'Add View'. Create an empty Razor View and name it appropriately, such as 'Data.cshtml'. What should be changed in the _Layout.cshtml file for PDF conversion? Change the '~/' in the link tags to './' as '~/' does not work well with IronPDF. What is the role of the RenderAsync method? The RenderAsync method is used to convert Razor Views to an HTML string. How do you enable detailed logging in PDF libraries? In IronPDF, set IronPdf.Logging.Logger.LoggingMode to IronPdf.Logging.Logger.LoggingModes.All for detailed logging. Where can I download a sample project for converting Razor Views to PDFs? You can download the complete code as a zipped file from the IronPDF website and open it in Visual Studio as an ASP.NET Core Web App (Model-View-Controller) project. Chaknith Bin Chat with engineering team now 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. Ready to Get Started? Free NuGet Download Total downloads: 14,143,061 View Licenses