andresmeza.com

Dev Stuff

Language / Idioma
Category Collection

Html to pdf using dotnet 6 free!!

In this article, we will explore how to use Dotnet 6 to create an API to convert HTML to PDF using Chromium and Puppeteer. We will create two endpoints – one that accepts the HTML content as base64 encoded payload in the request body, and another that accepts the URL of the HTML page to convert. We will be using the PuppeteerSharp library, which is a .NET port of the popular Node.js library Puppeteer.

Prerequisites:

  • Visual Studio 2019 or later
  • Dotnet 6 SDK
  • PuppeteerSharp library (can be installed via NuGet package manager)

Step 1: Create a new .NET 6 project in Visual Studio

We’ll create a new ASP.NET Core Web API project. Open Visual Studio and go to File > New > Project. Select ASP.NET Core Web Application as the project type, give it a name, and click Create.

Step 2: Install the PuppeteerSharp package

Open the Package Manager Console and run the following command to install the PuppeteerSharp package:

Install-Package PuppeteerSharp

Step 3: Create the HTML to PDF conversion endpoint that accepts base64 encoded payload

Create a new controller and add the following code to it:

using Microsoft.AspNetCore.Mvc;
using PuppeteerSharp;
using System.IO;
using System.Threading.Tasks;

[ApiController]
[Route("[controller]")]
public class HtmlToPdfController : ControllerBase
{
    [HttpPost("convertFromBase64")]
    public async Task<IActionResult> ConvertFromBase64([FromBody] string base64Html)
    {
        var options = new LaunchOptions { Headless = true };
        using var browser = await Puppeteer.LaunchAsync(options);
        using var page = await browser.NewPageAsync();
        await page.SetContentAsync(base64Html);
        var pdfBytes = await page.PdfDataAsync();
        return File(pdfBytes, "application/pdf");
    }
}

This endpoint takes in a base64 encoded HTML payload in the request body and converts it to a PDF using Puppeteer. The converted PDF is then returned as a file.

Step 4: Create the HTML to PDF conversion endpoint that accepts a URL

Add the following code to the same controller:

[HttpGet("convertFromUrl")]
public async Task<IActionResult> ConvertFromUrl([FromQuery] string url)
{
    var options = new LaunchOptions { Headless = true };
    using var browser = await Puppeteer.LaunchAsync(options);
    using var page = await browser.NewPageAsync();
    await page.GoToAsync(url);
    var pdfBytes = await page.PdfDataAsync();
    return File(pdfBytes, "application/pdf");
}

This endpoint takes in a URL in the query string and converts the HTML page at the specified URL to a PDF using Puppeteer. The converted PDF is then returned as a file.

Step 5: Test the endpoints

Run the project and test the endpoints using a tool like Postman or cURL. You should be able to convert HTML to PDF using both endpoints.

That’s it! You have now learned how to use Dotnet 6 to create an API to convert HTML to PDF using Chromium and Puppeteer. This is just one example of the many ways you can use Dotnet 6 to build powerful APIs.