Introduction
This article describes how to create PDF documents at runtime in ASP.NET core using C#. We will use the IronPDF library for generating PDF files in the ASP.NET core application.
IronPDF is a C PDF library that allows you to create PDFs without using Adobe Acrobat. This product offers the solution for developers who wish to export HTML content into a PDF file. IronPDF can be used with ASP.NET core to create PDF documents in a way that HTML pages are built. It gives developers the power to convert web pages into PDFs or build PDF files from scratch in ASP.NET core without knowing anything about Adobe Acrobat or any other third-party software. Let’s see how we can use it in the ASP.NET core application for generating the PDF file.
Create an ASP.NET Core Web App
Follow the following steps for creating ASP.NET Core Web App.
- Open Visual Studio and click the “Create New Project” button.
- From the template list, select ASP.NET Core Web App.
![https://lh4.googleusercontent.com/denxs0E0CLA_JZCqNlCkPxNQLRTccWouW9dohOtf14mnF3G5JKV7HcFXYSh6ONOjLh2qTI_2c2YBLEShJX3XocCQ9PRfqPChc2jmNgSDEqFgCEgPBZzG5PtS2VWiThpMbhzoTZHwIutLRv8RoA]()
- Give a proper name to your application.
![https://lh4.googleusercontent.com/I-mFgahdC7Pbw2KZXcGrJzIZF-B74AKJLTbtF5xv7MBktXujoaJuI1ytc1cRmI3ENP_ld_MKFrEbEhUETlzCnxeRBS7JbBQtev4NKEdCW8A-cpDfmGvnrbPQvOc8O9cnNStiEAzN2o_8L9cNUQ]()
- Select .NET 6.0 framework and click on create button.
![https://lh4.googleusercontent.com/oYQyLE9G4wdL9H7EQpKs3AOtPV1odFlPiKibaLhXU1twY68sU0OiCb22Vda3Lv3bnGKfWPpecVIOI2pcdRIMMs5Vqv2Rveqbn7QNdrECGMjZK3ARa-qB6FSpFqKeET9TiVZtVeIg9Likkg8rCg]()
By following the steps, you’ll be able to create an ASP.NET core application on your local machine.
Installing IronPDF Library
There are multiple ways to install the IronPDF library.
- Using NuGet Command Line
- Using NuGet GUI
- Download from NuGet Website
The easiest way to install IronPDF is using the NuGet command line. Go to the NuGet command line, write the following statement, and hit enter button.
PM> Install-Package IronPDF
IronPDF installation will start. After installation, you will be able to use IronPDF in your project. For confirmation that IronPDF has been installed, expand the Dependencies section in Solution Explorer and click on the Packages section. You will see the IronPDF package there.
![https://lh4.googleusercontent.com/GDTGvCSbCLEDeRXnQSXHhrfddtZiK2Rxny0GVyJGFPEig-QQweBlZotbXbiktU7LtP4IZ4PwxIJmT_DsTYZF7mROSistGsxTY6XK6gmHT3G_2_EKwGJ0N2z9YZbS1phcv18VT8fL8NXQG28eWg]()
Now, our project is ready to use IronPDF. Let’s see how we can use it in the ASP.NET core application.
Using IronPDF in ASP.NET Core,
We will create a button for generating PDFs. We will write the following code on the “Index.cshtml” page for that purpose.
<div>
<form method="post" asp-page="Index" asp-page-handler="GeneratePDF">
<inputtype="Submit" value="GeneratePDF"/>
</form>
</div>
First of all, a form created in a div and set the page location by using asp-page property to index page. And create an input with the type Submit, which converts the input to a button. And set the text on the button “Generate PDF.”
![https://lh5.googleusercontent.com/Aur2nmF6Imk51e1ZWzVfr8egPKVDfXgHyU9hEtReNCt5dsrJTrVJ8mHciRO4HHetIOQXVmVstgMZGIRB07iBa_SR3ILOuv0lIMcXDXe-BKFI5LEhuM0x2Tf4CE2CpV1PuglFEbWmq8zrDtGuDA]()
HTML String to PDF
We have created a button for generating PDFs. Now it's time to write post-action to generate PDF. To create the action, go to the Index.cshtml.cs file and import IronPDF at the top of the code file for using IronPDF by writing this line of code.
Using IronPDF;
Now create a post-action with the name of OnPostGeneratePDF and write the following code in the file:
public void OnPost GeneratePDF()
{
var renderer = new HtmlToPdf();
renderer.RenderHtmlAsPdf("<h1>This is test file</h1>").SaveAs("Example.pdf");
}
In the above code, we are converting HTML content to create a PDF. So, we create an object of HtmlToPdf class. After that we use RenderHtmlAsPdf function for converting HTML to PDF. In the parameter, we provide the HTML string. And then, we save the file using the SaveAs function, and in the parameter, we provide the file's name. In our case, we provide the name as “Example.pdf”. It will be the name of our output PDF file.
![https://lh3.googleusercontent.com/P2kxV-Em-X67a5eUXZmPsjNcraeRKX7SzRWBFiJBZE7lL2vmHjG75VaQ3hQfM7V9ZHboyA3a_te8JnvKPrN9k9fTxt6Mrm2zLlkBb_7mIuWZJrjRfb6kDOulbdSmDsWjPupDp7GihGWlHTottA]()
Now, run the project. You will see the welcome page with a “Generate PDF” button.
![https://lh3.googleusercontent.com/9r7kpvQRtsZ92RFN-gDOzPPSQnKRxM-d2kpQZ1sHF4Pm5uHu5vXwT-sF73iF264Gb7rwZNfunzakRB1BGkmPQVU9IdSAokhecCFXIsqracrf-e5K9XhJPjHHSjRDHYaxEpNEUhobpi0XKxCbdw]()
When you click on the “Generate PDF” button, it will create a PDF file you can see in the project folder.
![https://lh3.googleusercontent.com/01JoPDg2lakibk_rNshxigr2Zfcbtoh2BwpN6afMUZfQKZuLkEiCm17ZofUhOov1tp-D1jefE8HCas-qvkXFO27Spv3gtymofuxulP1zmN4C42SzUwMPPR5coQOKjNKjyfv_6IIAqedh_ackOw]()
Now open the PDF file. You will see the output which has been generated by the IronPDF. The content in the PDF is the same as we give input in the program.
![]()
HTML File to PDF
We can convert HTML files to PDF using IronPDF in the ASP.NET core application. You have to create an HTML page. Here is the screenshot of how you can add an HTML page to the ASP.NET core application.
![https://lh5.googleusercontent.com/_74vefHdVFp8Kqtvs6HqGDtKCF87WHHbWR336qAuFheB-5DQBx8m4HH39CY6aF9aoftZC2J2rCDXvHMGbn9VBaympioRPRThb7qxgDOCSGf0OjO4jZurIW5oO7MP0C6c74THMsIiC5NxBcok3w]()
Now add some text to the HTML file, which we have to convert into PDF.
![https://lh6.googleusercontent.com/1VIPcnR2d6l1XXkkO-hNtpirhCFPH9OEWWXwnQnqNDFH4pFwTyueR_uLqL_2Xp_kcDhuPRKUMPr487QDxRGOxsR44ugYYikC34dWY4FUPr7hAUEw7e0VzsxSZiLsufRakk55G_DrGzUU1hkKAA]()
Now add the following code in the OnPostGeneratePDF action to convert the HTML file to PDF.
public void OnPost GeneratePDF() {
var Renderer = new HtmlToPdf();
var PDF = Renderer.RenderHTMLFileAsPdf("Pages/htmlpage.html");
PDF.SaveAs("MyPdf.pdf");
}
![https://lh5.googleusercontent.com/VK3cTFL04JqMu-rxVwgPeFzHI3h_471HO_TnK6Un4rH5DXJacN4qTZE4egh8DBzeYwiOqhRHbzUtyo85IuiSxWHTFVzBTc9ir6L2WZmYjGiqhgVsB2Hn1Dp2WOfKFePc5VEZOnMnEf58S6cq5Q]()
In the above code, we use RenderHTMLFileAsPdf function and in parameter we give the path of HTML file. Now run the project and click on the “Generate PDF” button as we do before. You’ll see the pdf file in the project folder.
![https://lh6.googleusercontent.com/AHHvZtfHVElOQ19LMan44lCf3YHf5sgCqsCVyyuFBpZCtMFV_ucOMx4LfNnEjl5JXA-jnlJ0sYe18jzCojzzbZD9QMBgWufFrbyGB_7g_PE-fndntFYGmV8DCp1k39Ca5uJJXONzTDvXMrCYag]()
When you open the PDF file, it will look like this.
![https://lh6.googleusercontent.com/L-Smwu73Tc-X7cSytS_vib3d_BIKQAeolXMrxGl9WCm2rEc_mB06-Vt2CzWcvoftDgCMO38yrwl4Gzr8i1HLbuM65Pz9ScVW1Rz6F_98IkPJAWfdCo0mT_ASKa4eEgUqjQkA-LUilmYyp7X46Q]()
Conclusion
IronPDF is a great tool for the .NET developers for manipulating PDF in their .NET projects. It provides multiple features which are handy for developers to peace up their work. You can format PDF files, delete or add pages, and many more. IronPDF is free for development.