A .NET 10 console application demonstrating how to generate a PDF document from an HTML template in C#.
The app renders a Mustache HTML template against a strongly-typed C# model, then converts the result to a PDF using DinkToPdf — a managed wrapper around the native libwkhtmltox library. No external process is spawned; conversion happens entirely in-process.
The included template is a quotation document built with Materialize CSS and demonstrates:
- Dynamic data binding via Mustache placeholders
- Table layout (line items)
- Conditions / terms section
- Signature fields
- Optional watermark
HtmlToPdfCore/
├── HtmlToPdfCore.sln
└── src/
├── HtmlToPdfCore.csproj
├── Program.cs # Entry point
├── Services/
│ ├── HtmlParser.cs # Mustache template renderer
│ └── HtmlToPdfService.cs # HTML → PDF conversion via DinkToPdf
├── Models/
│ ├── QuoteItem.cs # Single line-item record
│ └── QuoteTemplateData.cs # Full quote view model
├── Data/
│ └── SampleDataFactory.cs # Hardcoded demo data (replace with your source)
└── Templates/
├── quote.html # Mustache HTML template
├── css/
├── images/
└── js/
cd src
dotnet runThe generated PDF is written to bin/Debug/net10.0/Output/.
- Edit
Templates/quote.html— use{{Placeholder}}for scalar values and{{#Items}}...{{/Items}}for lists. - Update
Models/QuoteTemplateData.csandModels/QuoteItem.csto match your data shape. - Replace
Data/SampleDataFactory.cswith your own data source (database, API, etc.). - Swap
Templates/images/logo.pngwith your own logo. - Adjust paper size, margins, and orientation in
Services/HtmlToPdfService.cs→BuildPdfDocument().
| Package | Purpose |
|---|---|
Haukcode.DinkToPdf |
HTML → PDF via native libwkhtmltox |
Stubble.Core |
Mustache template rendering |
Serilog + Serilog.Sinks.Console |
Structured logging |
Microsoft.Extensions.Hosting |
DI / hosting abstractions |