HtmlToPdf.AspNetCore 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package HtmlToPdf.AspNetCore --version 1.3.0
                    
NuGet\Install-Package HtmlToPdf.AspNetCore -Version 1.3.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="HtmlToPdf.AspNetCore" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HtmlToPdf.AspNetCore" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="HtmlToPdf.AspNetCore" />
                    
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 HtmlToPdf.AspNetCore --version 1.3.0
                    
#r "nuget: HtmlToPdf.AspNetCore, 1.3.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 HtmlToPdf.AspNetCore@1.3.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=HtmlToPdf.AspNetCore&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=HtmlToPdf.AspNetCore&version=1.3.0
                    
Install as a Cake Tool

HtmlToPdf.AspNetCore

A fast, accurate HTML to PDF conversion library for .NET with perfect RTL (Right-to-Left) language support. Built on Chromium for pixel-perfect rendering with full Arabic, Urdu, Persian, and Hebrew text support.


✨ Features

  • 🌍 Perfect RTL Support - Native support for Arabic, Urdu, Persian, Hebrew and other RTL languages
  • Fast Performance - Optimized for high-throughput PDF generation
  • 🎯 Pixel-Perfect Rendering - Chromium-based rendering with full CSS3, JavaScript, and web font support
  • 🔧 Simple API - Easy to use with just one line of code
  • 🚀 Automatic Browser Management - Auto-downloads Chromium if not installed
  • 📱 Cross-Platform - Works on Windows, Linux, and macOS
  • 🔒 Thread-Safe - Safe for concurrent use in web applications
  • ✍️ Document Signing - Add signatures, stamps, and watermarks to your PDFs
  • 🖼️ Image Signatures - Support for image-based signatures (PNG, JPG, etc.)
  • 🔏 Digital Stamps - Create official-looking stamps and seals
  • 💧 Watermarks - Add transparent watermarks (DRAFT, CONFIDENTIAL, etc.)
  • 📂 Smart File Naming - Automatically derive filenames from HTML content or PDF metadata
  • 🔄 Auto-Rename - Rename existing files on disk using smart suggested names

📦 Installation

Using .NET CLI

dotnet add package HtmlToPdf.AspNetCore

Using Package Manager

Install-Package HtmlToPdf.AspNetCore

🚀 Quick Start

Basic Usage

using HtmlToPdf;

var html = "<h1>Hello World</h1><p>This is a simple PDF document.</p>";
byte[] pdf = Pdf.Render(html);

File.WriteAllBytes("output.pdf", pdf);

ASP.NET Core Controller Example

using HtmlToPdf;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/pdf")]
public class PdfController : ControllerBase
{
    [HttpPost]
    public IActionResult GeneratePdf([FromBody] string html)
    {
        byte[] pdfBytes = Pdf.Render(html);
        return File(pdfBytes, "application/pdf", "document.pdf");
    }
}

📂 Smart File Naming

Automatically name your PDF based on the HTML content. The library reads <title>, <h1>, or <h2> tags (in that priority order) and sanitizes the result into a valid filename.

Auto-Name from HTML Content

using HtmlToPdf;

var html = @"
<html>
  <head><title>Monthly Sales Report</title></head>
  <body><h1>Sales Summary - March 2026</h1></body>
</html>";

// Saves as: C:\Reports\Monthly Sales Report.pdf
string savedPath = Pdf.RenderToFile(html, @"C:\Reports");

Console.WriteLine($"Saved: {savedPath}");
// Output: Saved: C:\Reports\Monthly Sales Report.pdf

If a file with the same name already exists, a numeric suffix is automatically appended: Monthly Sales Report.pdfMonthly Sales Report (2).pdf


Suggest Name from Existing File

You can also suggest names for files already on disk. Supports .html (title/headings) and .pdf (metadata).

// For an HTML file
string htmlName = Pdf.GetSuggestedFileName("report.html");

// For a PDF file (extracts from PDF Title metadata)
string pdfName = Pdf.GetSuggestedFileName("document.pdf");

Rename File on Disk

Automatically rename an existing file using its smart suggested name.

// Renames "temp_123.pdf" to "Invoice #456.pdf" based on its metadata
string newPath = Pdf.RenameFileWithSuggestedName("temp_123.pdf");

Collision Handling: If the target filename already exists, the library automatically appends a counter (e.g., Report (2).pdf).


Save to an Explicit Path

Use RenderToPath when you want full control over the filename:

string savedPath = Pdf.RenderToPath(html, @"C:\Reports\custom-name.pdf");

Naming Priority

HTML element Example Used as filename?
<title> <title>Invoice #1234</title> ✅ First priority
<h1> <h1>Annual Report</h1> ✅ If no <title>
<h2> <h2>Section Overview</h2> ✅ If no <title> or <h1>
(none found) Falls back to document.pdf

🌍 RTL Language Support

Arabic Example

using HtmlToPdf;

var arabicHtml = @"
<div dir='rtl' style='font-family: Arial, sans-serif; text-align: right;'>
    <h1>التقرير الشهري</h1>
    <p>هذا تقرير يحتوي على نص باللغة العربية مع دعم كامل للتشكيل والاتجاه من اليمين لليسار.</p>
</div>";

byte[] arabicPdf = Pdf.Render(arabicHtml, new PdfOptions
{
    Language = PdfLanguage.Arabic,
    MarginMm = 20
});

Urdu Example

using HtmlToPdf;

var urduHtml = @"
<div dir='rtl' style='font-family: Jameel Noori Nastaleeq;'>
    <h1>رپورٹ</h1>
    <p>یہ اردو زبان میں لکھی گئی ایک رپورٹ ہے جو کامل طور پر دائیں سے بائیں کی طرف چلتی ہے۔</p>
</div>";

byte[] urduPdf = Pdf.Render(urduHtml, new PdfOptions
{
    Language = PdfLanguage.Urdu,
    PageSize = PageSize.A5
});

Hebrew Example

using HtmlToPdf;

var hebrewHtml = @"
<div dir='rtl' style='font-family: David, Arial Hebrew;'>
    <h1>דוח חודשי</h1>
    <p>זהו דוח בעברית המדגים תמיכה מלאה בכתיבה מימין לשמאל.</p>
</div>";

byte[] hebrewPdf = Pdf.Render(hebrewHtml, new PdfOptions
{
    Language = PdfLanguage.Hebrew,
    MarginMm = 15
});

🔀 Mixed RTL / LTR Content

using HtmlToPdf;

var mixedHtml = @"
<div>
    <p><span dir='rtl'>هذا النص بالعربية</span> contains 
    <span dir='ltr'>English text</span> and 
    <span dir='rtl'>المزيد من العربية</span></p>

    <p><span dir='ltr'>Product Code: INV-2024-001</span> - 
    <span dir='rtl'>رمز المنتج</span></p>
</div>";

byte[] mixedPdf = Pdf.Render(mixedHtml);

✍️ Document Signing

Text Signatures

using HtmlToPdf;

string html = @"
<div class='signature-box'>
<p><strong>Signed by:</strong> John Doe</p>
<p><strong>Date:</strong> " + DateTime.Now.ToString("yyyy-MM-dd") + @"</p>
<div class='signature-line'>John Doe</div>
</div>";

byte[] pdf = Pdf.Render(html);

Image Signatures

using HtmlToPdf;

byte[] signatureImageBytes = File.ReadAllBytes("signature.png");
string signatureBase64 = Convert.ToBase64String(signatureImageBytes);

string html = $@"
<img src='data:image/png;base64,{signatureBase64}' style='max-width:200px;'/>";

byte[] pdf = Pdf.Render(html);

Multiple Signatures

using HtmlToPdf;

byte[] sig1 = File.ReadAllBytes("signature1.png");
byte[] sig2 = File.ReadAllBytes("signature2.png");

string html = $@"
<img src='data:image/png;base64,{Convert.ToBase64String(sig1)}'/>
<img src='data:image/png;base64,{Convert.ToBase64String(sig2)}'/>
";

byte[] pdf = Pdf.Render(html);

🔏 Stamps and Seals

<div class="stamp">
APPROVED
<br/>
2026-03-12
</div>
.stamp {
border:3px solid red;
border-radius:50%;
width:150px;
height:150px;
text-align:center;
font-weight:bold;
transform:rotate(-15deg);
}

💧 Watermarks

<div class="watermark">CONFIDENTIAL</div>
.watermark{
position:fixed;
top:50%;
left:50%;
transform:translate(-50%,-50%) rotate(-45deg);
font-size:72px;
opacity:0.2;
}

⚙️ Configuration Options

using HtmlToPdf;

var options = new PdfOptions
{
    PageSize = PageSize.A4,
    Landscape = false,
    MarginMm = 15,

    Language = PdfLanguage.Auto,
    Scale = 1.0f,
    PrintBackground = false,

    TimeoutMs = 7000
};

byte[] pdf = Pdf.Render(html, options);

📄 Page Size Options

public enum PageSize
{
    A4,
    Letter,
    Legal,
    A3,
    A5
}

🌐 Language Options

public enum PdfLanguage
{
    Auto,
    Arabic,
    Urdu,
    English,
    Hebrew
}

📋 Requirements

Runtime

Supports .NET 8.0, .NET 9.0, and .NET 10.0

Supported Browsers

One of the following must be available:

  • Google Chrome
  • Microsoft Edge
  • Chromium

If no browser is detected, the library will automatically download Chromium (~200MB).


🎯 Performance Tips

  • Cache generated PDFs for identical HTML
  • Use inline CSS instead of external stylesheets
  • Compress large images before embedding
  • Increase TimeoutMs for complex JavaScript pages
  • Call Pdf.Warmup() at application startup to avoid cold-start latency

🔒 Error Handling

using HtmlToPdf;

try
{
    byte[] pdf = Pdf.Render(html);
}
catch (HtmlToPdfException ex)
{
    Console.WriteLine($"PDF generation failed: {ex.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"Unexpected error: {ex.Message}");
}

🐛 Troubleshooting

Browser not found

  • Install Chrome / Edge / Chromium
  • Allow internet access for auto download

RTL text issues

  • Use dir="rtl"
  • Set proper fonts
  • Specify Language in PdfOptions

Watermarks not visible

  • Use position: fixed
  • Increase z-index

File not saved with expected name

  • Call Pdf.GetSuggestedFileNameFromHtml(html) to preview the derived name before saving
  • Ensure the HTML has a <title>, <h1>, or <h2> tag

📝 License

MIT License


❤️ Author

Muammar Siddiqui

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 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 is compatible.  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.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

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.3.1 0 6/9/2026
1.3.0 123 4/3/2026
1.2.0 111 4/1/2026
1.1.0 131 3/12/2026
1.0.2 126 2/1/2026
1.0.1 116 2/1/2026
1.0.0 124 2/1/2026