HtmlToPdf.AspNetCore
1.3.0
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
<PackageReference Include="HtmlToPdf.AspNetCore" Version="1.3.0" />
<PackageVersion Include="HtmlToPdf.AspNetCore" Version="1.3.0" />
<PackageReference Include="HtmlToPdf.AspNetCore" />
paket add HtmlToPdf.AspNetCore --version 1.3.0
#r "nuget: HtmlToPdf.AspNetCore, 1.3.0"
#:package HtmlToPdf.AspNetCore@1.3.0
#addin nuget:?package=HtmlToPdf.AspNetCore&version=1.3.0
#tool nuget:?package=HtmlToPdf.AspNetCore&version=1.3.0
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.pdf→Monthly 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
LanguageinPdfOptions
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 | Versions 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. |
-
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.