Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
If you're a .NET developer, you've likely faced the task of generating PDF files from web pages in your Web Applications. Thankfully, in ASP.NET Core, this process is made a breeze with the IronPDF PDF Library. This lets you whip up a PDF with a single line of code. Let's dive into how exactly you can use IronPDF for creating a PDF file.
This tutorial will cover the following topics:
The IronPDF .NET Library allows developers to create PDF documents easily in C#, F#, and VB.NET for .NET Core and .NET Framework. IronPDF's rendering is a pixel-perfect copy of desktop versions of Google Chrome. It processes PDF documents without Adobe Acrobat. IronPDF can be used to create a PDF file from ASP.NET web pages, HTML content, URLs, or from within Model View Controller apps.
Some important features of the IronPDF .NET library:
Let's start with how to use the IronPDF library to create a PDF Document.
This tutorial assumes that you have the latest version of Visual Studio installed.
Web Application
.NET Framework
To create a PDF document, the first step is to install the IronPDF library. You can install it using any given method below.
To install the IronPDF C# .NET Core Library from the NuGet Package Manager:
NuGet Package Manager
NuGet Package Manager - Solution Explorer
NuGet Package Manager - Solution Explorer
Open the NuGet Package Manager by clicking on Tools > NuGet Package Manager > Package Manager Console. Type the following command in the terminal.
PM> Install-Package IronPdf
NuGet Package Manager - Solution Explorer
The third way to include IronPDF in your project is to add a DLL file from the IronPDF library. You can download the DLL file from this link.
IronPDF is ready, and now create a PDF in ASP.NET Web Forms (ASPX) and ASP.NET Core Web Applications.
There are multiple ways to create a PDF document. Let's have a look at some of them below using code examples.
This section will demonstrate how to generate PDF files from ASP.NET WebForms, which only supports .NET Framework version 4. Hence, this requires IronPdf.Extensions.ASPX from NuGet official page to be installed. It is not available in .NET Core because ASPX is superseded by the MVC model.
Open the source file of the ASPX web page that you want to convert into a PDF document, in this case, create a new Default.aspx
page.
NuGet Package Manager - Solution Explorer
Open the Default.aspx.cs
file, and add the IronPDF namespace at the top of it.
using IronPdf;
using IronPdf;
Imports IronPdf
Next, write the following line of code in the Page_Load()
function:
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser)
With just one line of code, a new PDF document is created from an ASP.NET webpage.
the RenderThisPageAsPdf
method is used within the AspxToPdf
class to convert the ASPX page into a PDF file.
When you run the project, a PDF of the web page will appear in the browser. This has been done on the server side.
The above code only shows the PDF document in the browser. It is also possible to download the PDF document directly onto the computer by adding this line of code in the Page_Load()
function:
AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment);
AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment);
AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment)
This code will download the PDF file of the ASPX web page into the .NET project directory.
Output:
ASPX Page to PDF
This section will demonstrate how to generate PDF files in ASP.NET Core. IronPDF can convert everything in an HTML file, including images, CSS, forms, etc. directly to a PDF document. Add a button that will generate PDFs when clicked.
Add the markup below to any .cshtml
page of your choice (index.cshtml
will be used here).
<div>
<form method="post" asp-page="Index" asp-page-handler="GeneratePDF">
<inputtype="Submit" value="GeneratePDF"/>
</form>
</div>
<div>
<form method="post" asp-page="Index" asp-page-handler="GeneratePDF">
<inputtype="Submit" value="GeneratePDF"/>
</form>
</div>
In index.cshtml.cs
file, create a method called OnPostGeneratePDF
. This function will be used to render the HTML as a PDF.
public void OnPostGeneratePDF()
{
}
public void OnPostGeneratePDF()
{
}
Public Sub OnPostGeneratePDF()
End Sub
Next, add a new HTML page to your Web Application.
Add a New Web Page
Add some text to the body of this page, e.g. "Generating PDF files from HTML pages."
Finally, add the following code in the OnPostGeneratePDF
action method.
public void OnPostGeneratePDF() {
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("Pages/htmlpage.html");
pdf.SaveAs("MyPdf.pdf");
}
public void OnPostGeneratePDF() {
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("Pages/htmlpage.html");
pdf.SaveAs("MyPdf.pdf");
}
Public Sub OnPostGeneratePDF()
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlFileAsPdf("Pages/htmlpage.html")
pdf.SaveAs("MyPdf.pdf")
End Sub
Above, the RenderHtmlFileAsPdf
function is used to create PDFs from HTML files by specifying the path to the HTML file that will be converted.
Run the project and click on the "Generate PDF" button. The generated PDF file will appear in the ASP.NET Core project folder.
Output:
ASP.NET HTML Page to PDF
Visit the tutorial pages to learn how to convert MVC views to PDFs in ASP.NET Core.
IronPDF .NET Core is a complete solution for working with PDF documents. It provides the ability to convert from different formats to a new PDF document. It just takes a few lines of code to create and format PDF files programmatically.
The main highlight of IronPDF is the HTML converter, which renders HTML documents using an instance of a real, standards-compliant web browser behind the scenes. The HTML is rendered with complete accuracy, in a vector format suitable for the highest standards of commercial printing. The output is a clean and high-quality PDF.
IronPDF is ideal for developers and companies who need to manipulate PDF files within their software. Commercial licensing and pricing details are published on the website.
You can try the free version of the IronPDF library to test out its functionality. A free trial license key will allow you to test out IronPDF's entire feature set.
Additionally, a special offer allows you to get all five Iron Software products for the price of just two. More information about licensing can be found on this licensing page.
9 .NET API products for your office documents