USING IRONPDF

Creating a PDF Generator in ASP.NET Core using IronPDF

Updated September 26, 2022
Share:

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.

Topics Covered In this Tutorial

This tutorial will cover the following topics:

  • IronPDF
  • Create an ASP.NET Core Web App
  • Install the IronPDF Library
    1. NuGet Package Manager
    2. NuGet Package Manager Console
    3. Using the DLL file
  • Create a PDF document using ASP.NET Core Web Applications
    1. Create a PDF document using an ASP.NET WebForms (ASPX)
    2. Create a PDF document in ASP.NET Core from an HTML File
  • Summary

IronPDF

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.

Create an ASP.NET Core Web App

This tutorial assumes that you have the latest version of Visual Studio installed.

  • Open Visual Studio
  • Create a new ASP.NET Core Web Application

Creating a PDF Generator in ASP.NET using IronPDF, Figure 1: Web Application Web Application

  • Give a name to the project (E.g Pdf_Generation)
  • The latest and most stable version of the .NET Framework is 6.0. Select this version of the framework.

Creating a PDF Generator in ASP.NET using IronPDF, Figure 2: .NET Framework .NET Framework

Install the IronPDF Library

To create a PDF document, the first step is to install the IronPDF library. You can install it using any given method below.

1. NuGet Package Manager

To install the IronPDF C# .NET Core Library from the NuGet Package Manager:

  • Open the NuGet Package Manager by clicking on Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

Creating a PDF Generator in ASP.NET using IronPDF, Figure 3: NuGet Package Manager NuGet Package Manager

  • Or, right-click on the project in the Solution Explorer and click Manage NuGet Packages.

Creating a PDF Generator in ASP.NET using IronPDF, Figure 4: NuGet Package Manager - Solution Explorer NuGet Package Manager - Solution Explorer

  • Search for IronPDF. Select IronPDF and click on Install. The library will begin installing.

Creating a PDF Generator in ASP.NET using IronPDF, Figure 5: NuGet Package Manager - Solution Explorer NuGet Package Manager - Solution Explorer

2. NuGet Package Manager Console

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

Creating a PDF Generator in ASP.NET using IronPDF, Figure 6: NuGet Package Manager - Solution Explorer NuGet Package Manager - Solution Explorer

3. Using a DLL file

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.

  • Download the DLL zip file and extract it to a specific folder.
  • Open a project in Visual Studio. In the Solution Explorer, right-click on References and navigate to the IronPDF DLL file.

Create a PDF Document in ASP.NET Core Web Applications

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.

1. Create a PDF using an ASP.NET WebForms (ASPX)

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.

Creating a PDF Generator in ASP.NET using IronPDF, Figure 7: NuGet Package Manager - Solution Explorer 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
VB   C#

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)
VB   C#

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)
VB   C#

This code will download the PDF file of the ASPX web page into the .NET project directory.

Output:

Creating a PDF Generator in ASP.NET using IronPDF, Figure 8: ASPX Page to PDF ASPX Page to PDF

2. Create PDF using ASP.NET Core from an HTML File

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>
HTML

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
VB   C#

Next, add a new HTML page to your Web Application.

Creating a PDF Generator in ASP.NET using IronPDF, Figure 9: Add a New Web Page 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
VB   C#

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:

Creating a PDF Generator in ASP.NET using IronPDF, Figure 10: ASP.NET HTML Page to PDF ASP.NET HTML Page to PDF

Visit the tutorial pages to learn how to convert MVC views to PDFs in ASP.NET Core.

Summary

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.

< PREVIOUS
How to Create PDF Files in .NET Using IronPDF
NEXT >
Blazor PDF Viewer Tutorial

Ready to get started? Version: 2024.5 just released

Start Free Trial Total downloads: 9,229,207
View Licenses >