Zum Fußzeileninhalt springen
IRONPDF NUTZEN

Erstellen eines PDF-Generators in ASP.NET Core mit 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;
Imports 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);
' 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);
' 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.
}
Public Sub OnPostGeneratePDF()
	' Placeholder method that will handle PDF generation.
End Sub
$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");
}
Public Sub OnPostGeneratePDF()
	' Create a new instance of the ChromePdfRenderer class to handle PDF rendering.
	Dim renderer = New ChromePdfRenderer()

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

	' Save the generated PDF document with the specified name.
	pdf.SaveAs("MyPdf.pdf")
End Sub
$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.

Häufig gestellte Fragen

Wie kann ich ein PDF-Dokument in ASP.NET Core erstellen, ohne die Formatierung zu verlieren?

Sie können IronPDF verwenden, um hochwertige, pixelgetreue PDFs aus HTML-Inhalten, CSS und JavaScript zu erstellen. Die auf Google Chrome basierende Rendering-Engine von IronPDF stellt sicher, dass die Formatierung erhalten bleibt.

Was ist der einfachste Weg, ein PDF aus HTML in ASP.NET Core zu erstellen?

Die einfachste Methode, um ein PDF aus HTML in ASP.NET Core zu erstellen, ist die Verwendung der IronPDF-Methode RenderHtmlAsPdf, die HTML-Strings mühelos in PDF-Dokumente umwandelt.

Kann ich eine PDF-Bibliothek verwenden, um Webseiten in ASP.NET Core in PDFs zu konvertieren?

Ja, mit IronPDF können Sie Webseiten in ASP.NET Core in PDFs umwandeln, indem Sie den HTML-Inhalt der Webseite als PDF-Dokument mit der ChromePdfRenderer-Klasse rendern.

Gibt es eine Möglichkeit, eine PDF-Bibliothek in ASP.NET Core ohne Verwendung von NuGet zu installieren?

Ja, neben der Verwendung des NuGet-Paket-Managers können Sie die IronPDF-DLL-Datei direkt zu Ihrem Projekt hinzufügen, um die Bibliothek in Ihrer ASP.NET Core-Anwendung zu installieren.

Wie kann ich sicherstellen, dass meine ASP.NET Core-Anwendung die PDF-Generierung aus URLs unterstützt?

Mit IronPDF können Sie PDFs aus URLs generieren, indem Sie die Fähigkeit nutzen, Webseiten als PDFs zu rendern. Es ist einfach, jede zugängliche URL in ein PDF-Dokument zu konvertieren.

Welche Vorteile bietet die Verwendung einer PDF-Bibliothek in ASP.NET Core?

Die Verwendung von IronPDF in ASP.NET Core bietet mehrere Vorteile, darunter die Möglichkeit, PDFs aus verschiedenen Inhaltstypen zu generieren, Text und Bilder zu extrahieren, digital zu signieren und die Formatierung durch eine zuverlässige Rendering-Engine zu erhalten.

Wie füge ich Netzwerkanmeldedaten hinzu, wenn ich PDFs in ASP.NET Core generiere?

IronPDF ermöglicht es Ihnen, benutzerdefinierte Netzwerkanmeldedaten während der PDF-Erstellung hinzuzufügen, was den Zugriff auf geschützte Ressourcen bei der Erstellung von PDFs in ASP.NET Core-Anwendungen sicherstellt.

Kann ich die PDF-Generierungsfähigkeiten einer Bibliothek ohne Kauf testen?

Ja, Sie können die vollständige Funktionalität von IronPDF mit der kostenlosen Testversion testen, die es Ihnen ermöglicht, die PDF-Generierungsfähigkeiten zu bewerten, bevor Sie einen Kauf tätigen.

Ist es möglich, eine PDF-Bibliothek für ASP.NET WebForms neben ASP.NET Core zu verwenden?

Ja, IronPDF ist rückwärtskompatibel mit ASPX und ermöglicht Ihnen die PDF-Erstellung sowohl in ASP.NET WebForms als auch in ASP.NET Core-Anwendungen.

Ist IronPDF mit .NET 10 kompatibel?

Ja. IronPDF ist vollständig kompatibel mit .NET 10 sowie mit .NET 8 und .NET 9. Es unterstützt .NET 10 in allen wichtigen Projekttypen – Web, Desktop, Konsole und Cloud – und erfordert keine Anpassungen oder Workarounds. IronPDF ist zukunftssicher, auch wenn neue .NET-Innovationen veröffentlicht werden. (Siehe IronPDFs plattformübergreifende Unterstützung für .NET 10)

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen