LS.HtmlToPdf.Puppeteer 1.0.0

dotnet add package LS.HtmlToPdf.Puppeteer --version 1.0.0
                    
NuGet\Install-Package LS.HtmlToPdf.Puppeteer -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LS.HtmlToPdf.Puppeteer" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LS.HtmlToPdf.Puppeteer" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="LS.HtmlToPdf.Puppeteer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LS.HtmlToPdf.Puppeteer --version 1.0.0
                    
#r "nuget: LS.HtmlToPdf.Puppeteer, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LS.HtmlToPdf.Puppeteer@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LS.HtmlToPdf.Puppeteer&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=LS.HtmlToPdf.Puppeteer&version=1.0.0
                    
Install as a Cake Tool

LS.HtmlToPdf.Puppeteer

Simple and lightweight HTML-to-PDF rendering library powered by PuppeteerSharp.
Built for scenarios like invoice generation, receipt printing, and report rendering in .NET.

Features

  • Convert valid HTML into PDF (byte[])
  • Easy integration via IPdfRenderService
  • Chromium auto-management
  • Support for header/footer templates via PuppeteerSharp
  • Helper methods for placeholder injection and section composition

Installation

dotnet add package LS.HtmlToPdf.Puppeteer

Quick Start

1. Register Service

builder.Services.AddSingleton<IPdfRenderService, PuppeteerPdfRenderService>();

2. Prepare HTML and data

var htmlTemplate = File.ReadAllText("Templates/Invoice.html");
var replacements = new Dictionary<string, string>
{
    ["{{CompanyName}}"] = "LS Tools Ltd.",
    ["{{InvoiceNumber}}"] = "INV-20250624",
    ["{{Date}}"] = "2025-06-24"
};

var filledHtml = HtmlTemplateHelper.ReplacePlaceholders(htmlTemplate, replacements);
HtmlTemplateHelper.EnsureValid(filledHtml);

3. Render PDF

var pdfBytes = await pdfService.RenderHtmlAsync(filledHtml);
File.WriteAllBytes("invoice.pdf", pdfBytes);

Optional: Custom Header/Footer

Use PuppeteerSharp's PdfOptions to inject JS-based templates:

var options = new PdfOptions
{
    DisplayHeaderFooter = true,
    HeaderTemplate = "<div style='font-size:10px; margin-left:20px;'>Invoice - {{date}}</div>",
    FooterTemplate = "<div style='font-size:10px; margin-left:20px;'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></div>",
    MarginOptions = new MarginOptions
    {
        Top = "60px",
        Bottom = "40px"
    }
};

var pdfBytes = await pdfService.RenderHtmlAsync(filledHtml, options);

HtmlTemplateHelper API

The HtmlTemplateHelper class provides a set of static utilities to help process HTML templates before rendering to PDF. These methods simplify common tasks like injecting dynamic content, validating structure, and assembling fragments.

Method Description Typical Use Case
ReplacePlaceholders(string html, Dictionary<string, string> data) Replaces placeholders in the HTML with provided values. Populate dynamic values like {{CustomerName}}, {{Total}}, etc.
EnsureValid(string html) Validates the HTML structure (e.g. presence of <html>, <body>, </body>). Ensure the template is suitable for rendering, catch missing root tags early.
HtmlEncode(string input) Converts special characters to HTML entities. Prevents injection or encoding issues when inserting raw text into HTML.
LoadSection(string path) Loads a partial HTML file from disk. Use for headers, footers, or reusable sections like invoice line templates.
InjectSection(string template, string placeholder, string htmlSection) Replaces a placeholder in a full HTML template with a section of content. Insert dynamically constructed parts into base template, e.g. items table.
Concat(params string[] sections) Concatenates multiple HTML fragments with line breaks. Assemble multiple blocks (e.g. item rows) into one section before injection.

Example

var headerHtml = HtmlTemplateHelper.LoadSection("header.html");
var footerHtml = HtmlTemplateHelper.LoadSection("footer.html");
var fullHtml = HtmlTemplateHelper.InjectSection(baseTemplate, "{{Header}}", headerHtml);
fullHtml = HtmlTemplateHelper.InjectSection(fullHtml, "{{Footer}}", footerHtml);

Example: Rendering Multi-line Invoice Items

var itemsHtml = string.Join("\n", items.Select(item => $@"
    <div class='invoice-row'>
        <div>{item.Description}</div>
        <div style='text-align:right'>{item.Amount}</div>
    </div>"));

html = HtmlTemplateHelper.InjectSection(html, "{{InvoiceItems}}", itemsHtml);

License

MIT License
© 2025 Laoseng

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 142 6/24/2025