Html2PDFGenerator 1.0.7

dotnet add package Html2PDFGenerator --version 1.0.7
                    
NuGet\Install-Package Html2PDFGenerator -Version 1.0.7
                    
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="Html2PDFGenerator" Version="1.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Html2PDFGenerator" Version="1.0.7" />
                    
Directory.Packages.props
<PackageReference Include="Html2PDFGenerator" />
                    
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 Html2PDFGenerator --version 1.0.7
                    
#r "nuget: Html2PDFGenerator, 1.0.7"
                    
#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 Html2PDFGenerator@1.0.7
                    
#: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=Html2PDFGenerator&version=1.0.7
                    
Install as a Cake Addin
#tool nuget:?package=Html2PDFGenerator&version=1.0.7
                    
Install as a Cake Tool

🧾 Html2PDFGenerator

Html2PDFGenerator is a lightweight .NET library that allows you to easily convert HTML strings into PDF.
It’s simple, dependency-injected, and works seamlessly in ASP.NET Core projects.


πŸš€ Features

  • Convert HTML string β†’ PDF bytes in just one line
  • Works with ASP.NET Core dependency injection
  • Supports complex HTML, CSS, and inline styles
  • No external service required β€” fully local conversion

πŸ“¦ Installation

Install via NuGet Package Manager:

dotnet add package Html2PDFGenerator

powershell
Copy code
Install-Package Html2PDFGenerator

Or search for Html2PDFGenerator in Visual Studio’s NuGet Package Manager UI.

βš™οΈ Configuration

In your Program.cs (or Startup file if using older .NET):

using HTML2PDF;

var builder = WebApplication.CreateBuilder(args);

// Register PDF generator service
builder.Services.AddPDFGenerator();

var app = builder.Build();

🧠 Usage Example

In your controller or service:

using Microsoft.AspNetCore.Mvc;
using HTML2PDF;

public class MyPDFController : Controller
{
    private readonly IPDFGenerator _pdf;

    public MyPDFController(IPDFGenerator pdf)
    {
        _pdf = pdf;
    }

    [HttpGet("generate-pdf")]
    public IActionResult GeneratePDF()
    {
        string htmlContent = "<h1>Hello World 🌍</h1><p>This is a sample PDF generated from HTML.</p>";

        byte[] pdfBytes = _pdf.GeneratePDFFromHtml(htmlContent);

        return File(pdfBytes, "application/pdf", "Sample.pdf");
    }
}

🎯 Advanced Usage β€” Custom PDF Settings

For more control, you can use the CustomPdfRequest model to customize page layout, size, margins, DPI, image quality, and header/footer positioning.

Model Definition:

public class CustomPdfRequest
{
    public int? Orientation { get; set; } = 0; // 0 = Portrait, 1 = Landscape
    public PaperSizeRequest? PaperSize { get; set; } = new() { Height = "297", Width = "210" };
    public MarginSettings? Margins { get; set; }
    public int? DPI { get; set; } = 96;
    public bool? UseCompression { get; set; } = true;
    public int? ImageDPI { get; set; } = 300;
    public int? ImageQuality { get; set; } = 100;
    public string? ViewportSize { get; set; } = "1280x1024";
    public string? HeaderPosition { get; set; } = "Center"; // Left, Center, Right
    public string? FooterPosition { get; set; } = "Right";  // Left, Center, Right
    public bool? EnableIntelligentShrinking { get; set; } = false;
    public double? Zoom { get; set; } = 1.0; // Zoom factor (e.g., 1.0 = 100%, 0.75 = 75%)
}

public class PaperSizeRequest
{
    public string? Height { get; set; } = "297"; // mm
    public string? Width { get; set; } = "210";  // mm
}

public class MarginSettings
{
    public int? Top { get; set; } = 10;
    public int? Bottom { get; set; } = 10;
    public int? Left { get; set; } = 10;
    public int? Right { get; set; } = 10;
}

🧾 Example: Generates a simple PDF from raw HTML with customized layout options.

[HttpPost("generate-custom-pdf")]
public IActionResult GenerateCustomPDF([FromBody] CustomPdfRequest request)
{

    string htmlContent = "<h1>Hello World 🌍</h1><p>This is a sample PDF generated from HTML.</p>";

    byte[] pdfBytes = _pdf.GeneratePDFFromHtml(htmlContent, request);

    return File(pdfBytes, "application/pdf", "Sample.pdf");
}

🧾 Example: Adds custom header and footer β€” supports both plain text and HTML file paths.

[HttpPost("generate-custom-pdf-header-footer")]
public IActionResult GenerateCustomPDFWithHeaderFooter([FromBody] CustomPdfRequest Request)
{
    string HtmlContent = "<h1>Hello World 🌍</h1><p>This is a sample PDF generated from HTML.</p>";

    // (Optional) Header and Footer – could be text or .html file path
    string HeaderHtml = "Invoice Report - Generated on " + DateTime.Now.ToString("dd MMM yyyy");
    string FooterHtml = "Page [page] of [toPage]";

    byte[] PdfBytes = _pdf.GeneratePDFFromHtml(HtmlContent, Request, HeaderHtml, FooterHtml);

    return File(PdfBytes, "application/pdf", "Sample.pdf");
}

πŸ“ Output

The method returns a byte[] array representing the generated PDF.

You can:

  • Return it as a downloadable file via File()
  • Save it to disk using File.WriteAllBytes()
  • Attach it to emails
  • Store it in your database (as a VARBINARY column)

Example:

File.WriteAllBytes("C:\\Reports\\Invoice.pdf", pdfBytes);

🎯 Save Generated PDF to Disk

You can pass the save path to the method in order to save the generated PDF at a certain path.

🧾 Example: Saving the generated PDF at a specific path with custom settings.

[HttpPost("generate-and-save-pdf")]
public string GenerateAndSavePDF([FromBody] CustomPdfRequest request)
{
    string HtmlContent = "<h1>Hello World 🌍</h1><p>This is a sample PDF generated from HTML.</p>";
    string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "Documents", "PDFs", "Sample.pdf");

    return _pdf.GeneratePDFFromHtml(HtmlContent, request, SavePath);
}

🧾 Example: Save the generated PDF with custom header and footer β€” supports text or HTML file paths.

[HttpPost("generate-and-save-custom-pdf-header-footer")]
public string GenerateAndSaveCustomPDFWithHeaderFooter([FromBody] CustomPdfRequest Request)
{
    string HtmlContent = "<h1>Hello World 🌍</h1><p>This is a sample PDF generated from HTML.</p>";

    // (Optional) Header and Footer – could be text or .html file path
    string HeaderHtml = "Invoice Report - Generated on " + DateTime.Now.ToString("dd MMM yyyy");
    string FooterHtml = "Page [page] of [toPage]";

    string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "Documents", "PDFs", "Sample.pdf");

    return _pdf.GeneratePDFFromHtml(HtmlContent, Request, HeaderHtml, FooterHtml, SavePath);
}

πŸ“ Output

The method returns the full file path representing of the saved PDF.

Example:

C:\Personal\Documents\GeneratedAndSavedPDFs\Sample.pdf

🧩 Requirements

  • βœ… .NET 8.0 or higher
  • βœ… Windows / Linux / macOS compatible
  • βœ… The package automatically loads wkhtmltopdf native DLLs (no manual setup required)

πŸ§‘β€πŸ’» About the Author

Full-stack developer passionate about building clean, efficient, and developer-friendly tools for the .NET ecosystem. You can use this package freely in any project β€” feedback and improvements are always welcome!


⭐ If you find this package helpful, please consider giving it a star on NuGet.org!

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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
1.0.7 120 3/25/2026
1.0.6 112 3/17/2026
1.0.5 277 11/10/2025
1.0.4 203 11/7/2025
1.0.3 236 11/3/2025
1.0.2 231 11/3/2025
1.0.1 238 11/3/2025
1.0.0 230 11/3/2025