How to Create PDF Document in ASP.NET Core 6 Web API

Welcome to mycodebit.com.In this article we will learn How to Create PDF Document in ASP.NET Core 6 Web API. This is a very common task that we face daily as an ASP.NET Core web developer. In this post, I’m using Visual Studio 2022 and .NET version 6.0.

You can download the complete source code from here.

Let’s start with the practical.

Create a new Project:

To create a new project first open visual studio and click on “create a new project”. After that select “ASP.NET Core Web API” as a project template.

Configure Project:

Configure the project setting after project template selection. Give a name to the project and select a location to save the project, click on next and select a .NET version 6.0. Click on create project.

Now we have successfully created our project.

Install-Package DinkToPdf:

DinkToPdf is an open-source library that we can use as a package to build pdf documents in our ASP.NET Core web API project. We can install this package by “Package Manager Console”.

Install-Package DinkToPdf

Configure the Package into Program.cs:

After installing the package we have to configure the DinkToPdf service into Program.cs file so we can use it in our project.

// Add services to the container.
builder.Services.AddSingleton(typeof(IConverter),
    new SynchronizedConverter(new PdfTools()));

And do not forget to add these libraries in your program.cs.

using DinkToPdf;
using DinkToPdf.Contracts;

Download the libraries:

We have some compulsory libraries to generate PDFs in our project. Download these libraries into your project folder.

Download link

After clicking on libraries you will find the option to download the file.

Create PDF Document in ASP.NET Core 6 Web API

Code Example:

Let’s move to the code now. I have created a method in the controller to generate PDF files.

[HttpGet("GeneratePDF")]
        public IActionResult GeneratePDF()
        {
            var html = [email protected]"
           <!DOCTYPE html>
           <html lang=""en"">
           <head>
               Heading Here.
           </head>
          <body>
          <h1>Welcome to mycodebit.com</h1>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</p>
          </body>
          </html>
          ";
            GlobalSettings globalSettings = new GlobalSettings();
            globalSettings.ColorMode = ColorMode.Color;
            globalSettings.Orientation = Orientation.Portrait;
            globalSettings.PaperSize = PaperKind.A4;
            globalSettings.Margins = new MarginSettings { Top = 25, Bottom = 25 };
            ObjectSettings objectSettings = new ObjectSettings();
            objectSettings.PagesCount = true;
            objectSettings.HtmlContent = html;
            WebSettings webSettings = new WebSettings();
            webSettings.DefaultEncoding = "utf-8";
            HeaderSettings headerSettings = new HeaderSettings();
            headerSettings.FontSize = 15;
            headerSettings.FontName = "Ariel";
            headerSettings.Right = "Page [page] of [toPage]";
            headerSettings.Line = true;
            FooterSettings footerSettings = new FooterSettings();
            footerSettings.FontSize = 12;
            footerSettings.FontName = "Ariel";
            footerSettings.Center = "This is for demonstration purposes only.";
            footerSettings.Line = true;
            objectSettings.HeaderSettings = headerSettings;
            objectSettings.FooterSettings = footerSettings;
            objectSettings.WebSettings = webSettings;
            HtmlToPdfDocument htmlToPdfDocument = new HtmlToPdfDocument()
            {
                GlobalSettings = globalSettings,
                Objects = { objectSettings },
            };

            var pdfFile = _converter.Convert(htmlToPdfDocument); ;
            return File(pdfFile,
            "application/octet-stream", "DemoPdf.pdf");
        }

This method will return a file in the response and when we click on the file we can download it.

Create PDF Document in ASP.NET Core 6 Web API

Click on the file and it will be downloaded. Here is our downloaded file.

Create PDF Document in ASP.NET Core 6 Web API

Conclusion:

If you face any problem do not hesitate to comment below and you can give any suggestions to contact us.

I have enlisted the Top 15 ASP.NET Core interview questions according to the market. If you want to prepare yourself for ASP.NET Core Interview questions you can our this post.

Comments

prachi

how can we add parameters directly through api and generate a pdf and the template should be of email

Shayan

Write a function to receive parameters, parse that parameters to HTML, convert it to the PDF, and send the email. You can find the tutorials about sending email using smtp and Gmail on my blog.

prachi

i want a template to be converted to pdf through api by passing parameters to the template

Leave a Reply

Your email address will not be published.