NetHtml2Pdf 1.0.0

Additional Details

This package is under development, do not use

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package NetHtml2Pdf --version 1.0.0
                    
NuGet\Install-Package NetHtml2Pdf -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="NetHtml2Pdf" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NetHtml2Pdf" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="NetHtml2Pdf" />
                    
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 NetHtml2Pdf --version 1.0.0
                    
#r "nuget: NetHtml2Pdf, 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 NetHtml2Pdf@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=NetHtml2Pdf&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=NetHtml2Pdf&version=1.0.0
                    
Install as a Cake Tool

NetHtml2Pdf

A .NET library for converting HTML content to PDF using QuestPDF with extensible architecture.

Overview

NetHtml2Pdf is a modern, extensible HTML-to-PDF conversion library that supports various HTML elements including tables, paragraphs, line breaks, divisions, and sections. The library is designed to ensure maintainability, testability, and extensibility. The goal is to create a pure .NET cross-platform library that runs on Windows and Linux without requiring GDI+ dependencies.

Defaults

  • Page size: Letter (8.5×11 in)
  • Margins: 1 inch on all sides
  • Font: Inter (bundled) for deterministic rendering
  • CSS: minimal inline subset (e.g., color, background-color, border width, padding/margin, font-size, font-weight); unsupported properties are ignored
  • Unsupported elements: ignored, inner text preserved
  • Pagination: explicit via builder (no auto-pagination)

Quick Start

1. Install the Package

dotnet add package NetHtml2Pdf

2. Basic Usage (single page)

using NetHtml2Pdf;

var html = @"
    <section>
        <h1>My Document</h1>
        <p>This is a sample document.</p>
    </section>
    
    <table>
        <thead>
            <tr><th>Name</th><th>Age</th></tr>
        </thead>
        <tbody>
            <tr><td>Alice</td><td>30</td></tr>
            <tr><td>Bob</td><td>25</td></tr>
        </tbody>
    </table>
";

var converter = new HtmlConverter();
var pdfBytes = await converter.ConvertToPdfBytes(html);
await File.WriteAllBytesAsync("output.pdf", pdfBytes);

3. Explicit Pagination (multi-page)

using NetHtml2Pdf;

var builder = new PdfDocumentBuilder();
builder.AddPdfPage("<section><h1>Page 1</h1><p>Hello</p></section>");
builder.AddPdfPage("<section><h1>Page 2</h1><p>World</p></section>");

var pdf = await builder.RenderAsync();
await File.WriteAllBytesAsync("multi-page.pdf", pdf);

4. Headers and Footers

Render a header and footer on every page using HTML fragments (same supported subset as body):

using NetHtml2Pdf;

var builder = new PdfDocumentBuilder();
builder.SetHeaderHtml("<div><strong>HeaderText</strong></div>");
builder.SetFooterHtml("<div><em>FooterText</em></div>");

builder.AddPdfPage("<section><h1>Page 1</h1><p>Alpha</p></section>");
builder.AddPdfPage("<section><h1>Page 2</h1><p>Beta</p></section>");

var pdf = await builder.RenderAsync();
await File.WriteAllBytesAsync("with-header-footer.pdf", pdf);
Styling notes
  • Supported inline CSS in header/footer is the same minimal subset as body:
    • text-align, padding, margin, color, font-size, font-weight/font-style
  • Use <strong>/<em> for bold/italic; unsupported properties (e.g., display, position) are ignored.
  • Height adapts to content; keep header/footer concise for predictable layout.

Example (right-aligned header, centered footer):

builder.SetHeaderHtml("<div style='text-align: right; padding: 8px; color: #333; font-size: 12px;'>My Company</div>");
builder.SetFooterHtml("<div style='text-align: center; padding: 6px; color: #666; font-size: 10px;'><em>Confidential</em></div>");

Dependencies

  • QuestPDF: PDF generation library
  • AngleSharp: HTML parsing library
  • .NET 8.0: Target framework

Building and Testing

# Clone the repository
git clone <repository-url>
cd NetHtml2Pdf

# Restore dependencies
dotnet restore

# Build all projects
dotnet build

# Run tests
dotnet test

# Run console application
cd src/NetHtml2Pdf.TestConsole
dotnet run example.html

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes following good coding practices
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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