Skip to content

Convert an HTML invoice to PDF in C#

Since 1.1View as Markdown

Render an invoice written in HTML and CSS to a PDF, with a page footer.

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.

recipe-html-invoice-to-pdf.cs
#:package PdfNetStack.Html@1.2.0
using 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);
  • HtmlConverter.ConvertFileToDocument("invoice.html") reads a file, and its style sheets, images and fonts from the same folder.
  • ConvertToDocument returns a PdfDocument, so the invoice can be signed, encrypted or saved as PDF/A before it is written.
  • Fonts declared with @font-face are embedded. The generic families serif, sans-serif and monospace map to the standard PDF fonts, which need no embedding.