Introduction
In this article, we will explore how to generate PDF files in a .NET Core 6 Web API using the PDFSharp library. We will focus on converting HTML content to PDF format, allowing for easy integration of HTML templates or dynamically generated HTML into PDF documents. PDFSharp is a powerful and widely used library that provides extensive functionality for creating and manipulating PDF files in .NET Core applications.
Prerequisites
Before proceeding, make sure you have a basic understanding of .NET Core and Web API development. Additionally, ensure that you have installed the necessary software, including Visual Studio 2022 or later, and have a working knowledge of HTML and C#.
Creating a New Project in Visual Studio 2022
Creating a New Project in Visual Studio 2022 Start the Visual Studio software and select "Create a new project."

In the "Create a new project" dialog, select "ASP.NET Core Web API."

In the "Configure your new project" dialog, enter "GenerateHtmlToPdf" for the project name. It's important to name the project "GenerateHtmlToPdf" with the same capitalization to match each namespace when the code is copied. Select "Next."

In the "Additional information" dialog, select ".NET 6.0 (Long-term support)" and select "Create."

Visual Studio 2022 will generate the project structure for the selected application. In this example, we are using ASP.Net Core Web API, so we can create a controller to write the code or use the existing controller. There, you can enter the code and build/run the application.

Next, we can add the PDFSharp library to test the code.
Install PdfSharp Library through NuGet Package Manager
The Visual Studio software provides the NuGet Package Manager option to install the package directly to the solution.
In Visual Studio, Select Tools > NuGet Package Manager > Manage NuGet Packages for the solution. The below screenshot shows how to open the Nuget Package Manager.

To get started, install the PDFSharp library by adding the NuGet package to your .NET Core 6 Web API project. Open your project in Visual Studio, right-click on the project in the Solution Explorer, and select "Manage NuGet Packages." Search for "PDFSharp" and install the package into your project.


To Create a Request Class File
In the PdfRequest class, you have properties Name, Std, and Fees. The Fees property is of type FeesStructure, allowing you to encapsulate the fees-related information within the FeesStructure class.
The FeesStructure class contains properties such as Id, FeesDescription, and Amount, representing the individual elements of the fee structure.
Remember to use proper naming conventions, such as starting property names with uppercase letters (e.g., Fees instead of fees, and using consistent casing throughout your code.
With these classes, you can create instances of PdfRequest and set the values for the properties accordingly when processing or transferring data related to PDF requests.
public class PdfRequest
{
public string Name { get; set; }
public string Std { get; set; }
public FeesStructure fees { get; set; }
}
public class FeesStructure
{
public int id { get; set; }
public string FeesDescription { get; set; }
public string Amount { get; set; }
}
Again to create PdfController inside the PdfGenerate function
[Route("api/[controller]")]
[ApiController]
public class PDFController : ControllerBase
{
[HttpGet("FeesStructure")]
public async Task<ActionResult> GeneratePdf()
{
}
}





Ryan RiveraPosted Jun 16, 2024, 11:14 AM
You might want to try ZetPDF.com for generating PDF files in C#. It has worked well for me.
gaurav rawatPosted Mar 6, 2024, 8:36 AM
How do make req.Name font bold. i try <b> , <strong>, css font-weight:bold but its not working
Brian HerbertPosted Sep 27, 2023, 11:49 AM
In the nuget package (https://www.nuget.org/packages/Polybioz.HtmlRenderer.PdfSharp.Core) it says that "HtmlRenderer.PdfSharp.Core is a partial port of HtmlRenderer.PdfSharp for .NET Core". Does is support custom fonts?
Chekwube EzePosted Jul 18, 2023, 3:17 PM
Good Job. However I found out that PdfGenerator does not have a method for AddPdfPages.