Introduction

PDF generation is a common requirement in many applications, whether for generating reports, invoices, or other documents. In this article, we'll delve into how to generate PDFs in a .NET Core 8.0 API using the DinkToPdf library, a popular wrapper for the wkhtmltopdf tool. Specifically, we'll focus on creating an offer letter PDF on the server side by converting HTML content into a PDF document.

Prerequisites

Before we start, ensure you have the following installed.

namespaces of DinkToPdf

C#
using DinkToPdf;
using DinkToPdf.Contracts;

Project Setup

  1. Create a New .NET Core Web API Project
    • Open Visual Studio 2022 and create a new .NET Core Web API project.
  2. Alternatively, Use the Terminal
    • Open your terminal and run the following command
Bash
dotnet new webapi -n HtmlToPdfDotNetCoreApi
cd HtmlToPdfDotNetCoreApi

Add the DinkToPdf Library

Add the DinkToPdf library to your project via NuGet by running the following command in your terminal:

Bash
dotnet add package DinkToPdf
dotnet add package DinkToPdf.Contracts

Most importantly, download the wkhtmltopdf DLL

  1. Download the wkhtmltopdf binaries from the official website.
  2. Place the downloaded binaries in a folder named wkhtmltopdf within your project directory.

Let's configure DinkToPdf. First, create a folder for the wkhtmltopdf binaries

  1. Place the downloaded binaries in a folder named wkhtmltopdf within your project directory.
  2. Open the Program.cs file and register the DinkToPdf service.
C#
builder.Services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

Create an API controller in your project and name what you want to give.

C#
using DinkToPdf;
using DinkToPdf.Contracts;
using Microsoft.AspNetCore.Mvc;
namespace HtmlToPdfDotNetCoreApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class HTMLTOPDFController : ControllerBase
    {
        private readonly IConverter _converter;
        public HTMLTOPDFController(IConverter converter)
        {
            _converter = converter;
        }
        [HttpPost("create-pdf")]
        public IActionResult CreatePdf()
        {
            var templateContent = @"<!DOCTYPE html>
<html lang=""en"">
<head>
    <meta charset=""UTF-8"">
    <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
    <title>Offer Letter</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
        }
        .container {
            background-color: #fff;
            padding: 20px;
            margin: 0 auto;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            max-width: 800px;
        }
        .header, .footer {
            text-align: center;
        }
        .header h1, .footer p {
            margin: 0;
        }
        .content {
            margin: 20px 0;
        }
        .content p {
            line-height: 1.6;
        }
        .details {
            margin: 20px 0;
        }
        .details td {
            padding: 5px 0;
        }
    </style>
</head>
<body>
    <div class=""container"">
        <div class=""header"">
            <h1>Offer Letter</h1>
        </div>
        <div class=""content"">
            <p>Dear John Doe,</p>
            <p>We are pleased to offer you the position of Software Engineer at Tech Corp. We believe your skills and experience will be a valuable asset to our team.</p>
            <div class=""details"">
                <table>
                    <tr>
                        <td><strong>Start Date:</strong></td>
                        <td>August 1, 2024</td>
                    </tr>
                    <tr>
                        <td><strong>Salary:</strong></td>
                        <td>$80,000 per year</td>
                    </tr>
                    <tr>
                        <td><strong>Benefits:</strong></td>
                        <td>Health, Dental, 401(k)</td>
                    </tr>
                </table>
            </div>
            <p>We look forward to working with you and are confident that you will make significant contributions to our company.</p>
            <p>Please sign and return this letter by July 20, 2024, to confirm your acceptance of this offer.</p>
            <p>Sincerely,</p>
            <p>Jane Smith<br>
               HR Manager<br>
               Tech Corp</p>
        </div>
        <div class=""footer"">
            <p>© 2024 Tech Corp. All rights reserved.</p>
        </div>
    </div>
</body>
</html>
";
            
            var globalSettings = new GlobalSettings
            {
                ColorMode = ColorMode.Color,
                Orientation = Orientation.Portrait,
                PaperSize = PaperKind.A4,
                Margins = new MarginSettings { Top = 10, Bottom = 10, Left = 10, Right = 10 }
            };

            var objectSettings = new ObjectSettings
            {
                PagesCount = true,
                HtmlContent = templateContent,
                WebSettings = { DefaultEncoding = "utf-8" },
                HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 },
                FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" }
            };

            var pdf = new HtmlToPdfDocument()
            {
                GlobalSettings = globalSettings,
                Objects = { objectSettings }
            };

            byte[] pdfBytes = _converter.Convert(pdf);
            return File(pdfBytes, "application/pdf", "ConvertedDocument.pdf");
        }
    }
}

The GlobalSettings class is used to define settings that apply to the entire PDF document.

Object Settings

The ObjectSettings class is used to define settings for individual objects within the PDF.

Let's test our API. Press F5 to launch the application and display the Swagger UI in your browser.

NEt Core API

Offer Letter

Conclusion

In this article, we've covered how to set up a .NET Core 8.0 Web API project and use the DinkToPdf library to generate PDFs. This powerful combination allows you to create dynamic and customizable PDF documents for various use cases.

Happy coding!