Creating a PDF Generator in ASP.NET using IronPDF

There are many scenarios when .NET developers need to create PDF files from web pages in the web applications. In ASP.NET Core, this can be a very tedious task, but thanks to the IronPDF ".NET Core PDF Library", it has become easy to create PDF documents with just one line of code.

In this tutorial, we will learn how to use IronPDF to create a PDF file.

Topics Covered In this Tutorial

We will cover the following topics in this tutorial:

  • 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
  • Create a PDF document using ASP.NET Core web page
  • Create a PDF document using ASP.NET Core 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:

  • Creates PDF documents from HTML, CSS, and JavaScript
  • Generates PDF documents from URL or from Web pages
  • Load URLs with custom-network login credentials, user-agents, proxies, cookies, HTTP headers, and form variables (allowing login behind HTML login forms)
  • Read and fill PDF form-field data
  • Extract images and texts from PDF files
  • Digital signing

Let's start with how we can use the IronPDF library to create a PDF Document.

Create an ASP.NET Core Web App

This tutorial assumes that you have the Visual Studio 2022 installed.

  • Open Visual Studio 2022
  • Create a new ASP.NET Core web application
Web application

Web Applicationr

  • 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.
.NET Framework

.NET Framework

Install the IronPDF Library

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

1. NuGet Package Manager

We can 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.
NuGet Package Manager

NuGet Package Manager

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

NuGet Package Manager - Solution Explorer

  • Search for IronPDF. Select IronPDF and click on Install. The library will begin installing.
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
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 we can create a PDF in our 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 PDF using a ASP.NET Core Web Page

Open the source file of the ASPX web page that you want to convert into a PDF document. Here, we will create a PDF of the Default.aspx page.

NuGet Package Manager - Solution Explorer

Add the IronPDF Namespace

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, we created a new PDF document from an ASP.NET webpage.

We use the RenderThisPageAsPdf method 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. We can also 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 Core project directory.

Output:

ASPX Page to PDF

Convert an ASPX page into a PDF document

2. Create PDF using ASP.NET Core HTML File Name

IronPDF can converts everything in an HTML file, including images, CSS, forms, etc. directly to a PDF document. We can add this functionality into our ASP.NET core web app if we do not want to convert it in the Page_Load() function.

We will now add a button that will generate PDFs when clicked.

Add the markup below in any ".cshtml" page of choice (index.cshtml will be used here).

<div>
    <form method="post" asp-page="Index" asp-page-handler="GeneratePDF">
        <inputtype="Submit" value="GeneratePDF"/>
    </form>
</div>
HTML

Set the asp-page attribute to the page that was chosen previously.

In "index.cshtml.cs" file, create a method called OnPostGeneratePDF. We will use this function to render the HTML as a PDF.

public void OnPost GeneratePDF()
{

}
public void OnPost GeneratePDF()
{

}
Public void Function GeneratePDF() As OnPost

End Function
VB   C#

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 OnPost GeneratePDF() {
    var Renderer = new HtmlToPdf();
    var PDF = Renderer.RenderHTMLFileAsPdf("Pages/htmlpage.html");
    PDF.SaveAs("MyPdf.pdf");
}
public void OnPost GeneratePDF() {
    var Renderer = new HtmlToPdf();
    var PDF = Renderer.RenderHTMLFileAsPdf("Pages/htmlpage.html");
    PDF.SaveAs("MyPdf.pdf");
}
Public void Function GeneratePDF() As OnPost
	Dim Renderer = New HtmlToPdf()
	Dim PDF = Renderer.RenderHTMLFileAsPdf("Pages/htmlpage.html")
	PDF.SaveAs("MyPdf.pdf")
End Function
VB   C#

Above, we used the RenderHTMLFileAsPdf function to create PDFs from HTML files. We specify the path to the HTML file that will be converted.

Run the project and click on the “Generate PDF” button. Teh generated PDF file will appear in the ASP.NET Core project folder.

Output:

ASP.NET HTML Page to PDF

ASP.NET HTML Page to PDF

Click this link 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 format0 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 30-day trial license key will allow you to test out IronPDFs entire feature set.

Aditionally, 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 at this link.