Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Short answer: wkhtmltopdf can print document-wide page numbers and show the current chapter and section in headers or footers with substitution tokens such as [page], [topage], [section] and [subsection]. It does not automatically restart the counter at every heading. For chapter-local numbering, render each chapter separately, then merge the PDFs.

What wkhtmltopdf can number

wkhtmltopdf converts one or more HTML documents into PDF files. Its header and footer text supports these substitutions:

Token Value Typical use
[page] Current page in the rendered object Page number
[frompage] First page number Range labels
[topage] Last page number “Page 4 of 18”
[section] Current section heading Chapter name in a header
[subsection] Current subsection heading Section name in a header
[sitepage] and [sitepages] Site-level page counters Multi-document jobs

These tokens expose context and counters; they do not define a new numbering scope. In particular, the documented interface does not provide an automatic “restart at every <h1>” switch.

Add global page numbers with a text footer

For one continuous PDF, reserve enough bottom margin for the footer and use the current-page and total-page tokens:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Epson EcoTank ET-2800 Wireless Color All-in-One Supertank Printer - Black
  • INNOVATIVE CARTRIDGE-FREE PRINTING — No more dealing with lots of tiny ink cartridges; With this wireless document and photo printer each ink bottle set is equivalent to about 90 individual cartridges²
  • LESS FREQUENT INK REPLACEMENT — Replacement ink bottles don't have to be changed nearly as often as ink cartridges¹; When you choose this combination printer, scanner and copier you can print up to 4,500 pages black/7,500 color³
  • COLOR PRINTING — Up to 2 years of ink in the box4 (and with every replacement ink set) for fewer out-of-ink frustrations
  • ZERO CARTRIDGE WASTE — By using an Epson EcoTank printer you can help reduce the amount of cartridge waste ending up in landfills
  • HOME PRINTER DESIGNED FOR RELIABILITY — The Epson EcoTank ET-2800 All-in-One Supertank Color Printer creates vivid, detailed prints and documents thanks to Micro Piezo Heat-Free Technology; Fire off 10 ISO pages per minute1 to easily finish large jobs
wkhtmltopdf 
  --margin-bottom 18mm 
  --footer-center "Page [page] of [topage]" 
  input.html output.pdf

The equivalent header form is:

wkhtmltopdf 
  --margin-top 16mm 
  --header-right "Page [page] of [topage]" 
  input.html output.pdf

Use --header-spacing and --footer-spacing to control the gap between body content and the header or footer. If text overlaps the document, increase the corresponding margin first.

Show chapter and section names

Use semantic headings in the source HTML: an <h1> for each chapter and <h2> for sections. Then place the substitutions in a header or footer:

wkhtmltopdf 
  --margin-top 16mm 
  --margin-bottom 18mm 
  --header-left "[section]" 
  --header-right "[subsection]" 
  --footer-center "Page [page] of [topage]" 
  book.html book.pdf

The exact heading text that wkhtmltopdf recognizes depends on the document structure and renderer version. Keep headings as real, visible HTML headings rather than text styled to look like headings.

Build a styled HTML header or footer

Plain strings are sufficient for simple numbering. For a logo, rule, multiple aligned fields or custom typography, use --header-html or --footer-html. wkhtmltopdf passes values to that HTML file as GET-style query parameters.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <style>
    body { margin: 0; font: 9pt Arial, sans-serif; }
    .row { display: flex; justify-content: space-between; border-bottom: 1px solid #999; }
  </style>
  <script>
    function subst() {
      const params = new URLSearchParams(location.search);
      for (const name of ["page", "topage", "section", "subsection"]) {
        document.querySelectorAll("." + name).forEach(el => {
          el.textContent = params.get(name) || "";
        });
      }
    }
  </script>
</head>
<body onload="subst()">
  <div class="row">
    <span class="section"></span>
    <span>Page <span class="page"></span> of <span class="topage"></span></span>
  </div>
</body>
</html>

Save this as header.html, then invoke it:

wkhtmltopdf 
  --margin-top 20mm 
  --header-html header.html 
  --header-spacing 3 
  book.html book.pdf

The documented classes include page, topage, section, subsection, title, doctitle, sitepage and sitepages. Populate only the fields you need. Test the header file with a representative multi-page document because a header that is too tall can be pushed outside the printable area.

Rank #2
Sale
Epson EcoTank Photo ET-8550 Wireless Wide-Format All-in-One Tank Printer
  • CARTRIDGE-FREE PRINTING — Print lab-quality photos, graphics and creative projects; Get vibrant colors and sharp text with Epson's high-accuracy printhead and Claria ET Premium 6-color inks
  • INK BOTTLES — Save on photos1 and creative projects with affordable in-house printing; All-in-one printer allows you to print 4" x 6" photos for about 4 cents each vs. 40 cents with traditional ink cartridges1
  • LESS FREQUENT INK REPLACEMENT — Replacement ink bottles don't have to be changed nearly as often as ink cartridges¹; Printer, scanner and copier lets you print up to 6,200 color pages³
  • PRINT FOR LONGER — Up to 2 years of ink in the box² (and with every replacement ink set) for fewer out-of-ink frustrations with this wireless printer
  • ZERO CARTRIDGE WASTE — Epson EcoTank printer helps reduce the amount of cartridge waste ending up in landfills; Cartridge-free printer uses high-yield ink bottles; Each replacement ink bottle set is equivalent to about 100 individual ink cartridges⁴

Restart numbering for every chapter

A single continuous wkhtmltopdf object has global (or site-level) counters. Therefore a command such as --footer-center "Chapter [section] — Page [page] of [topage]" labels chapters but does not reset page 1 at each heading.

Reliable chapter-local workflow

  1. Split the source into one HTML file per chapter, keeping each chapter’s headings and assets self-contained.
  2. Render each file as a separate PDF. Each render starts its own [page] and [topage] sequence.
  3. Merge the chapter PDFs with your PDF-merging tool, preserving their order and bookmarks if your merger supports them.
wkhtmltopdf 
  --margin-bottom 18mm 
  --footer-center "Chapter 2 — Page [page] of [topage]" 
  chapter-2.html chapter-2.pdf

wkhtmltopdf 
  --margin-bottom 18mm 
  --footer-center "Chapter 3 — Page [page] of [topage]" 
  chapter-3.html chapter-3.pdf

# Merge chapter-2.pdf and chapter-3.pdf with your chosen PDF-merging tool.

This is a multi-render pipeline, not an automatic heading feature. It is the practical choice when every chapter must display “Page 1 of N”. It also lets you vary a chapter label or footer independently.

Continuous numbering with an offset

If chapters are rendered separately but must display continuous numbers, use the library’s pageOffset setting (or its equivalent in your language binding). The offset adds a fixed value to page numbers in headers, footers and the table of contents. It does not reset numbering per chapter. For example, a chapter rendered after a 12-page introduction can use an offset of 12 so its first printed page is 13.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Outlines and a table of contents

Semantic headings also drive navigation. Enable bookmarks with --outline and limit nesting with --outline-depth:

wkhtmltopdf 
  --outline 
  --outline-depth 2 
  book.html book.pdf

To generate a table of contents, use a toc object. The headings become entries, while --dump-outline writes the generated outline XML and --xsl-style-sheet lets you customize the TOC transformation. A typical multi-object command is:

Rank #3
HP Smart Tank 5000 Wireless All-in-One Ink Tank Printer, Scanner, Copier with 2 Years of Ink Included, Best-for-Home, Cartridge-Free, Refillable and AI-Enabled. (5D1B6A)
  • SET IT UP ONCE AND PRINT WITH CONFIDENCE. No complicated maintenance. Just easy, reliable printing you can count on.
  • INK FOR YEARS. NOT MONTHS. Up to 2 years of ink included. Get thousands of pages of cartridge-free printing. More pages, less hassle
  • KEEPS PRINTING WELL AFTER COMPETITORS HAVE QUIT. No complex maintenance. Sharper text, richer colors.[2] Only with HP Smart Tank
  • PREMIUM SUPPORT - Strong technical expertise to solve issues faster
  • THE LAST PRINTER YOU'LL EVER NEED. Enjoy years of refillable, cartridge-free printing.
wkhtmltopdf 
  --outline 
  --outline-depth 2 
  toc 
  chapter-1.html chapter-2.html 
  book.pdf

Check the output after changing heading levels: a deeper outline can make navigation useful but can also produce an unwieldy TOC.

Margins, layout and numbering troubleshooting

Footer is missing or clipped

  • Increase --margin-bottom (or --margin-top for a header).
  • Reduce --footer-spacing or --header-spacing if the reserved area is too tall.
  • Make sure the input is not using CSS that hides print headers or changes the page size unexpectedly.

Tokens appear literally

Pass the token in the wkhtmltopdf option, not inside ordinary body HTML. In a shell, quote the entire string: --footer-center "Page [page] of [topage]". In a programmatic wrapper, provide the same option through that wrapper’s command or option map.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Section names are blank or wrong

  • Use real <h1> and <h2> elements in document order.
  • Confirm that the heading is rendered in the same object; an iframe or separately loaded fragment may not define the current context.
  • Try a plain-text header before debugging a styled template, then inspect the query parameters received by the template.

Chapter pages do not restart at one

This is expected in a single continuous render. Split the chapters, render each separately and merge the PDFs. Do not substitute pageOffset; it only adds a constant.

Header HTML has no values

Ensure the template runs its substitution function on load and reads the query string. Use the documented class names exactly, including lowercase spelling. Keep JavaScript small and avoid dependencies that require network access.

Page totals change between runs

Page totals depend on fonts, CSS, image dimensions, paper size, margins and content loaded at render time. Pin those inputs, wait for dynamic content before capture, and use the same wkhtmltopdf build for every chapter.

Rank #4
NDYIN Portable Printers Wireless for Travel, N80 Bluetooth Thermal Printer
  • Wireless Bluetooth Printer: Portable thermal printer compatible with iPhone, Android phones, iPad and tablet computers via Bluetooth. For smartphones, please download the "Nada Print" App. You can also connect to laptops and computers for printing using a USB-C cable. (Note: Laptops and computers can only be connected via USB and require the installation of a driver first. Bluetooth connection is not supported.)
  • No-ink printing: Only supports US Letter and A4 size thermal paper.(Doesn't support regular paper) The no-ink portable thermal printer uses direct thermal technology, requiring no ink, toner or ribbons, making it environmentally friendly, cost-effective and time-saving. The thermal printer package comes with a roll of US Letter thermal printing paper. Note: When installing the paper, remember to switch the paper size switch on APP
  • Clear Print: NDYIN N80 portable thermal printer adopts high-definition printing technology, with a 203DPI resolution to provide you with clear printing results. This mobile printer is compatible with roll paper, folded paper and tattoo transfer paper, supporting printing from your mobile phone PDF, Word, pictures and web pages anytime and anywhere. It is recommended to use our NDYIN thermal paper to achieve good printing quality
  • Portable wireless printer for travel: The thermal printer is equipped with a built-in 1500mAh rechargeable battery, which can print 160 sheets of 8.5" x 11" thermal paper after being fully charged. It weighs only 1.5 pounds and is compact in size. This ink-free portable printer can be easily carried in a backpack or briefcase! It is perfect for business travel, cars, small offices, construction sites, schools and homes. You can print documents, contracts, invoices and boarding passes anytime and anywhere
  • The N80 thermal printer has a wide range of uses. The package includes the N80 printer, a roll of US Letter paper(7m/roll), a user manual, a guide card, a type-C soft cable and a type C adapter. Note: The charging adapter is not included. Special thermal paper is required for use; ordinary paper cannot be used. This ink-free portable thermal printer is suitable for various scenarios such as home, school, travel, office, and outdoor, meeting the printing needs of different groups of people. This tattoo template printer is also compatible with tattoo transfer paper, making it an ideal choice for tattoo art
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance and reliability choices

  • One pass: fastest operationally and simplest for a global counter, but cannot reset at headings.
  • Multiple renders: required for chapter-local totals; parallelize independent chapter jobs only when your machine has enough CPU and memory, then merge in deterministic order.
  • Text template: fewer moving parts and easier to debug.
  • HTML template: better visual control, but depends on correct query-string handling and margin reservation.
  • Stable assets: local or reliably available fonts and images reduce layout drift and unexpected page-count changes.

Always inspect the first page, a page containing a long heading, a page break near a footer and the final page. A visually correct footer can still be wrong if the total-page value changed after late-loading content.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Or skip the browser setup

If your real goal is a clean screenshot or PDF of a web page rather than a locally authored book, ScreenshotNeo provides a single API call. It accepts cookie and consent banners like a visitor, removes more than 60 known consent platforms plus newsletter popups and chat widgets, and reports whether a response was clean and billed. Bot checks, CAPTCHAs, blank pages, timeouts, failed loads and cache hits cost nothing.

cURL

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Python

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)

Node.js

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

See the full parameter list and PDF options in the ScreenshotNeo documentation. The service also offers an MCP server with take_screenshot, get_page_info and capture_pdf tools for Claude, Cursor and other MCP clients. Every plan includes all features; 1,000 screenshots per month are free with no card, and paid plans start at $5 for 3,000. Create a free ScreenshotNeo account.

Frequently Asked Questions

Can I use [sitepage] instead of [page] for chapter resets?

No. Site-level counters change scope for multi-document jobs but do not automatically restart at each chapter heading. Render separate chapter objects when a local page 1 is required.

Which heading levels should I use for chapters and sections?

Use <h1> for chapters and <h2> for sections so headers, outlines and the TOC can follow the intended hierarchy.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does pageOffset change the total-page count?

No. It adds a fixed value to displayed page numbers; it is not a per-chapter reset and does not redefine the document’s page total.

Quick Recap

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.