Page numbers in HTML to PDF
You cannot write a page number in your HTML, because the page does not know how many pages there will be. Pagination happens in the print engine. The number comes from a footer template, and the template needs somewhere to live.
Who actually counts the pages
Chromium lays the document out, decides where each page breaks, and then draws a header and a footer per page into the margins it was given. The footer template is HTML with inline styles, and Chromium substitutes five placeholders before drawing it:
| Class | Filled with |
|---|---|
pageNumber | The current page number |
totalPages | The number of pages in the finished document |
title | The document title |
date | The render date |
url | The document URL |
They are elements, not variables: <span class="pageNumber"></span> is replaced by the number at draw time.
The call, with a margin
The footer is drawn in the bottom margin, so a request with no margin has nowhere to put it. Set margin and the footer appears inside it.
curl -X POST https://webcapture.io/v1/convert \
-H "Authorization: Bearer wc_your_key" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/handbook",
"margin": "18mm",
"displayHeaderFooter": true,
"footerTemplate": "<div style=\"width:100%;font-size:9px;color:#666;text-align:center;\">Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></div>"}'
displayHeaderFooter must be true and at least one template must be present; the pair without a template is a 422, because the flag alone would print empty bands. Templates are capped at 10,000 characters.
The three things that go wrong
- No font size. Templates inherit nothing from your page. A template without
font-sizedraws at the engine's default, which reads as hairline grey at print resolution. Set it explicitly, as the example does. - No width. The template's block is as wide as its content unless you set
width: 100%. A centred footer then centres inside a box that is already the size of the text, which looks left-aligned. - No margin. The footer occupies the bottom margin. With a margin of zero it has no room and is not drawn at all, which looks like the template was ignored.
The margin field is one value applied to all four sides, 100 or less, with an optional mm, cm, in or px unit. A single generous value is usually the right shape for a numbered document.
What the free tools expose
The anonymous tool pages do not carry header and footer fields: their forms are field allowlists over the same endpoint, and templates are keyed-surface parameters. If you need numbered pages, the same request with a key costs one of the 50 free renders a month.
For a visible diagonal mark instead of a footer band, see the watermark stamp. For the full parameter table, the API reference.
Frequently asked questions
Can page numbers start at a number other than 1?
Not through a template placeholder. The engine counts from the first page of the render. To start at another number, put the offset in the document flow with a counter and use the header or footer for the running total instead, or render the range you want as its own document.
Can I have a different footer on the first page?
Not per page. The template applies to every page, which is why the examples keep the mark minimal: a horizontal rule or a two-line footer looks wrong when it repeats on a cover. Put cover-page-specific content in the HTML itself.
Do header and footer templates work for PNG captures?
No. They are a print feature: they are drawn in paper margins during PDF pagination. An image capture has no margins and no pages, so the fields are refused for image formats.
Related
Render your first page in seconds
Mint a key, POST a URL, and the finished document comes back in the same response; pass ?wait=false for a job ticket with poll and download URLs. The first 50 renders a month are free, and there is no credit card. Not ready for a key? The free URL to PDF tool renders public pages without one.