Html2PDFGenerator 1.0.7
dotnet add package Html2PDFGenerator --version 1.0.7
NuGet\Install-Package Html2PDFGenerator -Version 1.0.7
<PackageReference Include="Html2PDFGenerator" Version="1.0.7" />
<PackageVersion Include="Html2PDFGenerator" Version="1.0.7" />
<PackageReference Include="Html2PDFGenerator" />
paket add Html2PDFGenerator --version 1.0.7
#r "nuget: Html2PDFGenerator, 1.0.7"
#:package Html2PDFGenerator@1.0.7
#addin nuget:?package=Html2PDFGenerator&version=1.0.7
#tool nuget:?package=Html2PDFGenerator&version=1.0.7
π§Ύ 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
VARBINARYcolumn)
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 | Versions 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. |
-
net9.0
- DinkToPdf (>= 1.0.8)
- Microsoft.Extensions.DependencyInjection (>= 9.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.