Convert an HTML invoice to PDF in C#
Render an invoice written in HTML and CSS to a PDF, with a page footer.
Short answer
Section titled “Short answer”Pass the HTML to HtmlConverter.ConvertToPdf from the PdfNetStack.Html package. The @page rule
sets the paper size and margins, and its @bottom-center margin box numbers the pages. No browser
runs, and nothing is fetched from the network.
#:package PdfNetStack.Html@1.2.0using PdfNetStack.Html;
string html = """ <html lang="en"> <head> <title>Invoice 42</title> <style> @page { size: A4; margin: 2cm; @bottom-center { content: "Page " counter(page) " of " counter(pages) } } body { font-family: sans-serif; font-size: 10pt } table { width: 100%; border-collapse: collapse } th, td { padding: 6pt; border-bottom: 1px solid #CBD5E1; text-align: left } td.amount, th.amount { text-align: right } tfoot td { font-weight: bold } </style> </head> <body> <h1>Invoice 42</h1> <p>Issued 2026-10-29. Payment due within 30 days.</p> <table> <thead><tr><th>Item</th><th class="amount">Amount</th></tr></thead> <tbody> <tr><td>Consulting, 8 hours</td><td class="amount">800.00 EUR</td></tr> <tr><td>Support plan</td><td class="amount">200.00 EUR</td></tr> </tbody> <tfoot><tr><td>Total</td><td class="amount">1,000.00 EUR</td></tr></tfoot> </table> </body> </html> """;
byte[] pdf = HtmlConverter.ConvertToPdf(html);File.WriteAllBytes("invoice-42.pdf", pdf);Variations
Section titled “Variations”HtmlConverter.ConvertFileToDocument("invoice.html")reads a file, and its style sheets, images and fonts from the same folder.ConvertToDocumentreturns aPdfDocument, so the invoice can be signed, encrypted or saved as PDF/A before it is written.- Fonts declared with
@font-faceare embedded. The generic familiesserif,sans-serifandmonospacemap to the standard PDF fonts, which need no embedding.