푸터 콘텐츠로 바로가기
IRONPDF 사용

Creating a PDF Generator in ASP.NET Core using IronPDF

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

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 direct download page for the IronPDF package.

  • Download the DLL zip file and extract it to a specific folder.
  • Open the 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 the official NuGet page for ASPX 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;
$vbLabelText   $csharpLabel

Next, write the following line of code in the Page_Load() function:

// This code renders the current ASPX page as a PDF file and displays it in the browser.
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
// This code renders the current ASPX page as a PDF file and displays it in the browser.
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
$vbLabelText   $csharpLabel

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 is 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:

// This code downloads the generated PDF file of the current ASPX page to the client.
AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment);
// This code downloads the generated PDF file of the current ASPX page to the client.
AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment);
$vbLabelText   $csharpLabel

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">
        <input type="Submit" value="GeneratePDF"/>
    </form>
</div>
<div>
    <form method="post" asp-page="Index" asp-page-handler="GeneratePDF">
        <input type="Submit" value="GeneratePDF"/>
    </form>
</div>
HTML

In the index.cshtml.cs file, create a method called OnPostGeneratePDF. This function will be used to render the HTML as a PDF.

public void OnPostGeneratePDF()
{
    // Placeholder method that will handle PDF generation.
}
public void OnPostGeneratePDF()
{
    // Placeholder method that will handle PDF generation.
}
$vbLabelText   $csharpLabel

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()
{
    // Create a new instance of the ChromePdfRenderer class to handle PDF rendering.
    var renderer = new ChromePdfRenderer();

    // Render the HTML file as a PDF by specifying the path and save the resulting PDF file.
    var pdf = renderer.RenderHtmlFileAsPdf("Pages/htmlpage.html");

    // Save the generated PDF document with the specified name.
    pdf.SaveAs("MyPdf.pdf");
}
public void OnPostGeneratePDF()
{
    // Create a new instance of the ChromePdfRenderer class to handle PDF rendering.
    var renderer = new ChromePdfRenderer();

    // Render the HTML file as a PDF by specifying the path and save the resulting PDF file.
    var pdf = renderer.RenderHtmlFileAsPdf("Pages/htmlpage.html");

    // Save the generated PDF document with the specified name.
    pdf.SaveAs("MyPdf.pdf");
}
$vbLabelText   $csharpLabel

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 Iron Software licensing information page.

자주 묻는 질문

서식을 잃지 않고 ASP.NET Core에서 PDF 문서를 생성하려면 어떻게 해야 하나요?

IronPDF를 사용하면 HTML 콘텐츠, CSS 및 JavaScript에서 픽셀 하나하나가 완벽한 고품질 PDF를 만들 수 있습니다. Google Chrome을 기반으로 하는 IronPDF의 렌더링 엔진은 서식이 유지되도록 보장합니다.

ASP.NET Core에서 HTML로 PDF를 만드는 가장 쉬운 방법은 무엇인가요?

ASP.NET Core에서 HTML로 PDF를 만드는 가장 간단한 방법은 HTML 문자열을 PDF 문서로 쉽게 변환하는 IronPDF의 RenderHtmlAsPdf 메서드를 사용하는 것입니다.

PDF 라이브러리를 사용하여 ASP.NET Core에서 웹 페이지를 PDF로 변환할 수 있나요?

예, IronPDF를 사용하면 ChromePdfRenderer 클래스를 사용하여 웹 페이지의 HTML 콘텐츠를 PDF 문서로 렌더링하여 ASP.NET Core에서 웹 페이지를 PDF로 변환할 수 있습니다.

NuGet을 사용하지 않고 ASP.NET Core에 PDF 라이브러리를 설치할 수 있는 방법이 있나요?

예, NuGet 패키지 관리자를 사용하는 것과는 별도로 프로젝트에 IronPDF DLL 파일을 직접 추가하여 ASP.NET Core 애플리케이션에 라이브러리를 설치할 수 있습니다.

ASP.NET Core 애플리케이션이 URL에서 PDF 생성을 지원하려면 어떻게 해야 하나요?

IronPDF를 사용하면 웹 페이지를 PDF로 렌더링하는 기능을 활용하여 URL에서 PDF를 생성할 수 있으므로 액세스 가능한 모든 URL을 PDF 문서로 쉽게 변환할 수 있습니다.

ASP.NET Core에서 PDF 라이브러리를 사용하면 어떤 이점이 있나요?

ASP.NET Core에서 IronPDF를 사용하면 다양한 콘텐츠 유형에서 PDF를 생성하고, 텍스트와 이미지를 추출하고, 디지털 서명을 수행하고, 안정적인 렌더링 엔진을 통해 서식을 유지하는 등 여러 가지 이점을 얻을 수 있습니다.

ASP.NET Core에서 PDF를 생성할 때 네트워크 자격 증명을 추가하려면 어떻게 해야 하나요?

IronPDF를 사용하면 PDF 생성 중에 사용자 지정 네트워크 자격 증명을 추가할 수 있어 ASP.NET Core 애플리케이션에서 PDF를 생성하는 동안 보호된 리소스에 액세스할 수 있습니다.

구매하지 않고도 라이브러리의 PDF 생성 기능을 테스트할 수 있나요?

예, 무료 평가판을 사용하여 IronPDF의 전체 기능을 테스트할 수 있으며, 이를 통해 구매하기 전에 PDF 생성 기능을 평가할 수 있습니다.

ASP.NET WebForms용 PDF 라이브러리를 ASP.NET Core와 함께 사용할 수 있나요?

예, IronPDF는 ASPX와 역호환되므로 ASP.NET WebForms 및 ASP.NET Core 애플리케이션 모두에서 PDF 생성에 사용할 수 있습니다.

IronPDF는 .NET 10과 호환되나요?

예. IronPDF는 .NET 8 및 .NET 9와 함께 .NET 10과 완벽하게 호환됩니다. 웹, 데스크톱, 콘솔, 클라우드 등 모든 주요 프로젝트 유형에서 .NET 10을 지원하며 해결 방법이나 조정이 필요하지 않습니다. IronPDF는 최신 .NET 혁신이 출시됨에 따라 미래에도 계속 사용할 수 있습니다. (IronPDF의 .NET 10에 대한 크로스 플랫폼 지원 참조)

커티스 차우
기술 문서 작성자

커티스 차우는 칼턴 대학교에서 컴퓨터 과학 학사 학위를 취득했으며, Node.js, TypeScript, JavaScript, React를 전문으로 하는 프론트엔드 개발자입니다. 직관적이고 미적으로 뛰어난 사용자 인터페이스를 만드는 데 열정을 가진 그는 최신 프레임워크를 활용하고, 잘 구성되고 시각적으로 매력적인 매뉴얼을 제작하는 것을 즐깁니다.

커티스는 개발 분야 외에도 사물 인터넷(IoT)에 깊은 관심을 가지고 있으며, 하드웨어와 소프트웨어를 통합하는 혁신적인 방법을 연구합니다. 여가 시간에는 게임을 즐기거나 디스코드 봇을 만들면서 기술에 대한 애정과 창의성을 결합합니다.