I recently had a freelancer ask me for help with the automated generation of their invoices. They were tech savvy, and were using a templating engine to generate beautiful HTML documents from a template and some data files. They'd automated everything up until that point, but some of their clients wanted the invoices sent as PDF documents. The freelancer's existing workflow was to manually open each invoice in Chrome, and use the "Print to PDF" functionality to generate a PDF. This obviously didn't scale well.
This isn't the type of task I usually do, but my first instinct was to use Puppeteer.
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.
The freelancer's tech stack of choice was .NET (which was why they asked me for help), so they didn't want to deal with the Node version. Luckily, there is an excellent .NET port of Puppeteer called Puppeteer Sharp.
The API is a dream to use. Converting a HTML document to PDF was a simple as this:
var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
await using var page = await browser.NewPageAsync();
await page.GoToAsync("file://path/of/the/invoice.html");
await page.PdfAsync(outputFilePath);
This code was effortless to slip into their automation script, and successfully generated PDF versions of their invoices.
This same approach can be used to convert arbitrary pages to PDF in your .NET apps (or scripts). I hope you found this article helpful.