How to HTML To PDF Headers Footers

Headers and footers in HTML-to-PDF conversion add running elements to every page — page numbers, document titles, dates, and company logos. Unlike manually editing a finished PDF, API-based headers and footers are defined in your template and rendered automatically during generation. SublimePDF supports both CSS-based running headers and Chromium's native header/footer templates with dynamic variables.

Follow the step-by-step instructions below, then use the free tool directly — no registration or download required.

Open Tool →

How to HTML To PDF Headers Footers — Step by Step

1

Use the headerTemplate and footerTemplate options

Pass 'headerTemplate' and 'footerTemplate' HTML strings in your API request options. These templates are injected into every page's header and footer areas. Example: "footerTemplate": "<div style='font-size:10px; text-align:center; width:100%;'><span class='pageNumber'></span> of <span class='totalPages'></span></div>".

2

Enable displayHeaderFooter

Set "displayHeaderFooter": true in the options object. Without this flag, headerTemplate and footerTemplate are ignored even if provided.

3

Use built-in CSS classes for dynamic content

Chromium's header/footer renderer supports special classes: 'pageNumber' (current page), 'totalPages' (total page count), 'date' (current date), 'title' (document title from <title> tag), and 'url' (source URL). Place <span class='pageNumber'></span> anywhere in your template.

4

Set margins to accommodate headers and footers

Headers and footers render inside the page margin area. Set "margin": { "top": "80px", "bottom": "60px" } to create enough space. If margins are too small, headers and footers overlap with body content.

5

Style your header and footer HTML

Apply inline styles directly in the template strings — external stylesheets and <link> tags are not available inside header/footer templates. Use font-size, color, padding, text-align, and borders for layout.

Pro Tips

  • 💡 Header and footer templates must be self-contained HTML with inline styles — they cannot reference your main document's CSS classes or external stylesheets.
  • 💡 Include images in headers using base64 data URIs: <img src='data:image/png;base64,...' height='30'>. External image URLs may not load reliably in header/footer contexts.
  • 💡 Set different header/footer content for the first page by using CSS @page :first { margin-top: 0; } in your body HTML and conditionally hiding the header on page one with JavaScript.
  • 💡 If page numbers show '0 of 0', check that displayHeaderFooter is true and that your top/bottom margins are large enough for the header/footer to render visibly.

Privacy & Security

All processing happens directly in your browser. Your files are never uploaded to any server — they remain on your device throughout the entire process. SublimePDF uses WebAssembly technology for fast, secure, client-side processing.

Works Everywhere

This tool works on any modern browser — Chrome, Firefox, Safari, or Edge — on desktop, tablet, or mobile. No software to install. PDF is an open ISO standard supported by all major platforms.

How to HTML To PDF Headers Footers — FAQ

Why are my headers and footers not showing?
Three common causes: 1) 'displayHeaderFooter' is not set to true, 2) top/bottom margins are too small (set at least 60px), 3) the template HTML contains syntax errors. Start with a minimal template to confirm it works, then add complexity.
Can I use different headers on odd and even pages?
Chromium's built-in header/footer templates don't support odd/even differentiation directly. You can use JavaScript in the header template to check the pageNumber class and conditionally show different content, or use CSS @page :left and @page :right in the body with CSS-based running headers.
How do I add a horizontal line below the header?
Include a border in your header template's inline style: <div style='border-bottom: 1px solid #ccc; padding-bottom: 5px; width: 100%;'>Header Text</div>.
Can I include the document title dynamically?
Yes. Set a <title> tag in your HTML document, then use <span class='title'></span> in your header or footer template. The renderer populates it with the document's title automatically.

Ready to get started?

Use SublimePDF's free tools right now.

Open Tool