Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
In modern web applications, generating documents from HTML content is a common requirement. Whether you need to create invoices, reports, or any other types of documents, converting HTML to PDF can be accomplished efficiently with the IronPDF library in ASP.NET using C#.
Today, we will explore how to set up IronPDF and use it to convert HTML content into a PDF document.
IronPDF is a powerful PDF library that allows developers to read, create, and manipulate PDF documents. With a rich feature set and a quick, simple installation process, you can take your PDF projects to the next level in no time thanks to IronPDF. Its intuitive API is easy to learn, making it an ideal choice if you are looking to generate dynamic PDF documents, perform PDF security tasks, PDF annotations, etc directly from your web application.
Before you start, ensure you have:
Launch Visual Studio and select the ASP.NET project type that best suits your needs. For today's example, I will be creating an ASP.NET Core Web App (Model-view Controller).
Then, enter the name for your project and choose the location to house the project.
Finally, choose your .NET Framework for the project, and change any additional settings for the project such as the authentication type, or enabling container support and docker.
To create a new controller to house our HTML to PDF code within, first right-click on the "Controllers" folder in the solution explorer and choose "Add -> Controller".
This will prompt a new window to open, where you can choose what form of controller you want to add to the project. We have picked an empty MVC Controller.
Finally, we give the new Controller a name and click "Add" to add it to our project.
Now that we have created our ASP.NET project, we can begin writing the code for converting HTML file content to a PDF. We will start with a simple example of HTML string to PDF, before looking at converting HTML content with customization.
using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace IronPdfTest.Controllers
{
public class PdfController : Controller
{
public IActionResult GeneratePdf()
{
// String of HTML code to be converted to PDF
string htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF generated from HTML.</p>";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Convert HTML string to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
return File(pdf.BinaryData, "application/pdf", "generatedDocument.pdf");
}
}
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace IronPdfTest.Controllers
{
public class PdfController : Controller
{
public IActionResult GeneratePdf()
{
// String of HTML code to be converted to PDF
string htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF generated from HTML.</p>";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Convert HTML string to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
return File(pdf.BinaryData, "application/pdf", "generatedDocument.pdf");
}
}
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Namespace IronPdfTest.Controllers
Public Class PdfController
Inherits Controller
Public Function GeneratePdf() As IActionResult
' String of HTML code to be converted to PDF
Dim htmlContent As String = "<h1>Hello, IronPDF!</h1><p>This is a PDF generated from HTML.</p>"
Dim renderer As New ChromePdfRenderer()
' Convert HTML string to PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
Return File(pdf.BinaryData, "application/pdf", "generatedDocument.pdf")
End Function
End Class
End Namespace
ASP.NET MVC uses controllers to handle user requests. When a user navigates to a specific URL, ASP.NET will call a method in the controller associated with that route.
When a user visits a URL linked to the GeneratePdf action, the method executes.
IActionResult: This is the return type, representing the response that the web application will send back to the user. It could be a view (HTML page), file download, etc. In this case, it's a PDF file.
GeneratePdf() Method:
Inside the method, we define a string htmlContent that contains the HTML you want to convert to a PDF. For example, "
This is a PDF generated from HTML.
".In an ASP.NET MVC application, you define routes that map URLs to controller methods (actions). For example, if you navigate to /Pdf/GeneratePdf in the browser, ASP.NET will look for the PdfController and call its GeneratePdf method. Ensure your routing configuration allows access to the GeneratePdf action. If you are using ASP.NET Core MVC, this is usually configured automatically. If you are using Web API, make sure your routes are properly set up.
Now that we have the basics down, let's look at creating a PDF file from HTML content with some customization settings set for the output PDF. IronPDF provides a powerful set of PDF customization tools, such as margins, headers/footers, custom PDF sizing, and more.
using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace IronPdfTest.Controllers
{
// Controller for our PDF converter
public class PdfController : Controller
{
public IActionResult GeneratePdf()
{
string url= "<h1>Hello, IronPDF!</h1><p>This is a PDF generated from HTML.</p>";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Creating the cover page
PdfDocument cover = renderer.RenderHtmlAsPdf("<h1>Cover Page</h1>");
// Adding custom options for our final PDF
renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Landscape;
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A3;
renderer.RenderingOptions.TextHeader.CenterText = "IronPDF";
renderer.RenderingOptions.TextHeader.FontSize = 12;
renderer.RenderingOptions.MarginTop = 20;
renderer.RenderingOptions.FirstPageNumber = 2;
// Creating our main PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(url);
// Appending the cover to the main PDF
pdf.InsertPdf(cover, 0);
return File(pdf.BinaryData, "application/pdf", "generatedDocument.pdf");
}
}
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace IronPdfTest.Controllers
{
// Controller for our PDF converter
public class PdfController : Controller
{
public IActionResult GeneratePdf()
{
string url= "<h1>Hello, IronPDF!</h1><p>This is a PDF generated from HTML.</p>";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Creating the cover page
PdfDocument cover = renderer.RenderHtmlAsPdf("<h1>Cover Page</h1>");
// Adding custom options for our final PDF
renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Landscape;
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A3;
renderer.RenderingOptions.TextHeader.CenterText = "IronPDF";
renderer.RenderingOptions.TextHeader.FontSize = 12;
renderer.RenderingOptions.MarginTop = 20;
renderer.RenderingOptions.FirstPageNumber = 2;
// Creating our main PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(url);
// Appending the cover to the main PDF
pdf.InsertPdf(cover, 0);
return File(pdf.BinaryData, "application/pdf", "generatedDocument.pdf");
}
}
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Namespace IronPdfTest.Controllers
' Controller for our PDF converter
Public Class PdfController
Inherits Controller
Public Function GeneratePdf() As IActionResult
Dim url As String= "<h1>Hello, IronPDF!</h1><p>This is a PDF generated from HTML.</p>"
Dim renderer As New ChromePdfRenderer()
' Creating the cover page
Dim cover As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Cover Page</h1>")
' Adding custom options for our final PDF
renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Landscape
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A3
renderer.RenderingOptions.TextHeader.CenterText = "IronPDF"
renderer.RenderingOptions.TextHeader.FontSize = 12
renderer.RenderingOptions.MarginTop = 20
renderer.RenderingOptions.FirstPageNumber = 2
' Creating our main PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(url)
' Appending the cover to the main PDF
pdf.InsertPdf(cover, 0)
Return File(pdf.BinaryData, "application/pdf", "generatedDocument.pdf")
End Function
End Class
End Namespace
Today we have taken a closer look at how HTML to PDF conversion can be used with ASP.NET, and explored the process of creating PDF files from HTML within an ASP.NET project. By following the steps outlined above, you can easily integrate PDF generation into your web applications, allowing you to create high-quality, printable documents from HTML content.
IronPDF sports a rich set of features that can be leveraged to produce high-quality PDF documents, for more advanced features and detailed customization, refer to the IronPDF documentation, and with its fast installation, you can have IronPDF set up within your project in no time.
9 .NET API products for your office documents