May 12, 2026

Invoice PDF HTML Template Guide | CSS Layout & Design

invoicesHTML templatePDF generationCSSdesign

Once you understand how to call the API from Automate Invoice PDF Generation, the next step is raising the quality of your template design. This guide covers the HTML and CSS patterns for building professional invoice templates — page sizing, table layout, brand colors, multi-page support, and dynamic data binding.

Template Design Foundations

Before writing a single line of CSS, decide these three things:

  1. Page size: A4 portrait in this guide (the API's options.page-size accepts A3, A4, A5, Letter and Legal)
  2. Margins: 20mm top/bottom and 15mm left/right in this guide's examples
  3. Font stack: Fonts that are actually installed where the PDF is rendered, or a web font (check the generated PDF)
  4. Engine: The template in this guide uses Flexbox and @page margin boxes, so it assumes the quality engine (Chromium, Pro plan and above). The fast engine (wkhtmltopdf) supports CSS 2.1 plus some CSS3, so the layout may break there

Lock Page Settings with @page

@page {
  size: A4 portrait;
  margin: 20mm 15mm;
}

body {
  font-family: 'Inter', 'Helvetica Neue', Arial, sans-serif;
  font-size: 10pt;
  line-height: 1.6;
  color: #1a1a1a;
  margin: 0;
}

By default the FUNBREW quality engine (Gotenberg) takes the paper size and margins from the API options (page-size and margin-*; omitted margins are 10mm), not from the @page size. Put the same values in @page and in the API options, and check the generated PDF. See the CSS @page Rule PDF Guide for a full breakdown of page-level CSS.

Header Section: Company Info and Invoice Metadata

<header class="invoice-header">
  <div class="company-info">
    <h1 class="company-name">Acme Corporation</h1>
    <p>123 Main Street, Suite 100, New York, NY 10001</p>
    <p>hello@acme.example.com &bull; +1 (555) 000-0000</p>
  </div>
  <div class="invoice-meta">
    <h2 class="invoice-title">Invoice</h2>
    <table class="meta-table">
      <tr><th>Invoice #</th><td>INV-2026-0042</td></tr>
      <tr><th>Issue Date</th><td>May 12, 2026</td></tr>
      <tr><th>Due Date</th><td>June 30, 2026</td></tr>
    </table>
  </div>
</header>
.invoice-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  border-bottom: 2px solid #0052cc;
  padding-bottom: 12pt;
  margin-bottom: 16pt;
}

.company-name {
  font-size: 14pt;
  font-weight: 700;
  color: #0052cc;
  margin: 0 0 4pt;
}

.invoice-title {
  font-size: 24pt;
  font-weight: 700;
  color: #0052cc;
  margin: 0 0 8pt;
  text-align: right;
}

.meta-table {
  margin-left: auto;
  border-collapse: collapse;
  font-size: 9pt;
}

.meta-table th {
  text-align: right;
  padding: 2pt 8pt 2pt 0;
  color: #555;
  font-weight: normal;
}

.meta-table td {
  text-align: left;
  font-weight: 600;
}

Bill-To Section

<section class="bill-to">
  <p class="bill-to-label">Bill To</p>
  <p class="client-name">{{client_name}}</p>
  <p>{{client_address}}</p>
  <p>Attention: {{contact_name}}</p>
</section>
.bill-to {
  background: #f5f7fa;
  border-left: 4px solid #0052cc;
  padding: 10pt 12pt;
  margin-bottom: 20pt;
}

.bill-to-label {
  font-size: 8pt;
  color: #777;
  margin: 0 0 4pt;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.client-name {
  font-size: 14pt;
  font-weight: 700;
  margin: 0 0 4pt;
}

Line Items Table

The line items table is the heart of the invoice. Aim for clear column widths, right-aligned amounts, and striped rows for readability. {{#each}} and {{#if}} are Handlebars syntax: as shown below, you render the HTML in your app before sending it to the API (FUNBREW's template API /api/pdf/generate-from-template only does simple {{variable}} replacement, without loops or conditionals). The row number {{no}} is a value you number from 1 before passing the data (Handlebars' @index is zero-based).

<table class="line-items">
  <thead>
    <tr>
      <th class="col-no">#</th>
      <th class="col-desc">Description</th>
      <th class="col-qty">Qty</th>
      <th class="col-price">Unit Price</th>
      <th class="col-amount">Amount</th>
    </tr>
  </thead>
  <tbody>
    {{#each items}}
    <tr>
      <td class="col-no">{{no}}</td>
      <td class="col-desc">{{description}}</td>
      <td class="col-qty text-right">{{quantity}}</td>
      <td class="col-price text-right">{{unit_price_formatted}}</td>
      <td class="col-amount text-right">{{amount_formatted}}</td>
    </tr>
    {{/each}}
  </tbody>
</table>
.line-items {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 16pt;
  font-size: 9pt;
}

.line-items thead tr {
  background-color: #0052cc;
  color: #ffffff;
}

.line-items thead th {
  padding: 6pt 8pt;
  text-align: center;
  font-weight: 600;
}

.line-items tbody tr:nth-child(even) {
  background-color: #f0f4ff;
}

.line-items tbody td {
  padding: 5pt 8pt;
  border-bottom: 1px solid #e0e7ff;
  vertical-align: middle;
}

/* Column widths */
.col-no     { width: 5%; }
.col-desc   { width: 50%; }
.col-qty    { width: 8%; }
.col-price  { width: 17%; }
.col-amount { width: 17%; }

.text-right  { text-align: right; }
.text-center { text-align: center; }

/* Line up digits in numeric columns (works when the font supports it) */
.col-price, .col-amount, .amount { font-variant-numeric: tabular-nums; }

For deeper CSS guidance on table output, see the HTML to PDF Table Layout Guide.

Totals Section

<div class="totals-wrapper">
  <table class="total-table">
    <tr>
      <th>Subtotal</th>
      <td class="amount">{{subtotal_formatted}}</td>
    </tr>
    <tr>
      <th>Tax ({{tax_rate}}%)</th>
      <td class="amount">{{tax_amount_formatted}}</td>
    </tr>
    <tr class="grand-total-row">
      <th>Total Due</th>
      <td class="amount total">{{grand_total_formatted}}</td>
    </tr>
  </table>
</div>
.totals-wrapper {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20pt;
}

.total-table {
  border-collapse: collapse;
  font-size: 9pt;
}

.total-table th {
  text-align: left;
  background: #f5f7fa;
  padding: 4pt 10pt;
  border: 1px solid #ddd;
  min-width: 120pt;
  font-weight: normal;
}

.total-table .amount {
  text-align: right;
  padding: 4pt 10pt;
  border: 1px solid #ddd;
  min-width: 90pt;
}

.grand-total-row th,
.grand-total-row td {
  background-color: #0052cc;
  color: #ffffff;
  font-size: 11pt;
  font-weight: 700;
}

.grand-total-row .amount.total {
  font-size: 13pt;
}

Payment and Notes Section

<section class="notes-section">
  <div class="payment-info">
    <h3>Payment Details</h3>
    <p>Bank: First National Bank</p>
    <p>Account: 123-456789 &bull; Routing: 021000021</p>
    <p>Payable to: Acme Corporation</p>
  </div>
  {{#if notes}}
  <div class="custom-notes">
    <h3>Notes</h3>
    <p>{{notes}}</p>
  </div>
  {{/if}}
</section>
.notes-section {
  display: flex;
  gap: 20pt;
  font-size: 9pt;
  border-top: 1px solid #ddd;
  padding-top: 12pt;
}

.notes-section h3 {
  font-size: 9pt;
  font-weight: 700;
  margin: 0 0 6pt;
  color: #333;
}

Multi-Page Support: Repeating Headers and Page Numbers

When line items span multiple pages, keep column headers visible on every page and add page numbers. @page margin boxes (such as @bottom-center) and the page-number counters arrived in Chrome 131 (Chrome 131 release notes). On the quality engine they depend on the Chromium version in the rendering environment, and the fast engine (wkhtmltopdf) does not show them, so check the generated PDF.

/* Repeat thead on every page (CSS 2.1 says print user agents "may" repeat it; behavior depends on the engine) */
.line-items thead {
  display: table-header-group;
}

/* Page counter in the footer */
@page {
  @bottom-center {
    content: "Page " counter(page) " of " counter(pages);
    font-size: 8pt;
    color: #aaa;
  }
}

/* Prevent table rows from splitting across pages (page-break-inside is the legacy alias of break-inside, kept for older engines) */
.line-items tbody tr {
  break-inside: avoid;
  page-break-inside: avoid;
}

Sending to the PDF API

Once your template is ready, inject dynamic data and send it to the API.

const fs = require('fs');
const Handlebars = require('handlebars'); // npm install handlebars

const templateSource = fs.readFileSync('invoice-template.html', 'utf-8');
const template = Handlebars.compile(templateSource);

async function generateInvoicePdf(invoiceData) {
  // Number rows from 1 ({{no}}) before rendering. Handlebars HTML-escapes {{...}} values
  const html = template({
    ...invoiceData,
    items: invoiceData.items.map((item, i) => ({ ...item, no: i + 1 })),
  });

  const response = await fetch('https://pdf.funbrew.cloud/api/pdf/generate-from-html', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.FUNBREW_PDF_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      html,
      filename: `invoice-${invoiceData.invoiceNumber}.pdf`,
      options: {
        'page-size': 'A4',
        'margin-top': 20,    // mm (keep in sync with the @page margin)
        'margin-bottom': 20,
        'margin-left': 15,
        'margin-right': 15,
        engine: 'quality',   // needed for Flexbox and margin boxes; 403 on the Free plan
      },
    }),
  });

  const result = await response.json();
  if (!response.ok || !result.success) {
    throw new Error(`PDF generation failed: ${response.status} ${result.message ?? ''}`);
  }
  return result.data.download_url; // download URL of the generated PDF
}

The API returns JSON containing data.download_url and more, not the PDF bytes. Element backgrounds (the thead, the total row, and so on) are printed as-is on the quality engine, but backgrounds set on html or body are not printed by default (Gotenberg documentation). Set background colors on elements.

Paste your HTML into the Playground to preview the output before integrating with the API.

Template Design Checklist

  • @page and the API options (page-size, margin-*) set the same A4 size and margins
  • Background colors are set on elements (thead, the total row), not on html or body
  • Amount columns are right-aligned
  • thead has display: table-header-group for multi-page repeating headers
  • break-inside: avoid (plus the legacy page-break-inside) prevents rows from splitting mid-row
  • Numeric columns use font-variant-numeric: tabular-nums so digits line up
  • Tax rate and subtotal are clearly separated from the total

Summary

Key design decisions for invoice HTML templates:

  • Align @page with the API options: then check the generated PDF
  • Right-align amounts and use tabular numbers: easier to scan
  • Stripe rows and use thead repeat: readability across many line items
  • Avoid mid-row page breaks: prevents awkward splits in multi-page invoices

When your template is polished, proceed to Automate Invoice PDF Generation for monthly batch processing and email delivery automation. Check the API Documentation for all supported options. Add webhook callbacks to get notified when each PDF is ready.

Related

Powered by FUNBREW PDF