DocRaptor HTML TO PDF API

How to create a PDF with Page Numbers

Adding page numbers to PDF documents is difficult to impossible with open-source HTML to PDF tools, but it's really easy with DocRaptor's API!

Because we're designed as a document generation tool, our CSS is aware of your document—including page numbers. To add page numbers with a really simple footer, just add this CSS:

@page {
  @bottom {
    content: "Page " counter(page, upper-alpha) " of " counter(pages, decimal);
  }
}

You can also reset the page count like so:

#page4 {
  counter-reset: page 1
}

To see all that in action, use this example HTML code:

Page 1
Page 2
Page 3
Page 4, with reset page counter

Finally, try out these full code samples in a variety of languages that will actually generate a PDF with page numbers:

Select Your Language

Using DocRaptor's API

Below are the basics of using DocRaptor's HTTP API. It can easily be used in any programming language, but if we don't have a native agent for your language, please let us know! That'll help us write the best examples and agents.

The API key shown here, YOUR_API_KEY_HERE, actually works! Use it for testing without creating an account. The only requirement is test mode must be used (test parameter set to true).

To make documents, send a POST request to this URL:

https://YOUR_API_KEY_HERE@api.docraptor.com/docs

Send JSON-encoded API parameters (you'll need to set an HTTP header for Content-Type as application/json). These are the only required parameters:

{
  "type": "pdf",
  "document_content": "Hello World!"
}

Generating the Document with cURL

Here's a completely functional code sample for this tutorial:

cat <<-'DOCUMENT_OPTIONS' > docraptor_parameters.json
  {
    "test": true,
    "document_type": "pdf",
    "document_content": "<div style=\"page-break-after: always;\">Page 1</div><div style=\"page-break-after: always;\">Page 2</div><div style=\"page-break-after: always;\">Page 3</div><div id=\"page4\" style=\"page-break-after: always;\">Page 4, with reset page counter</div><style>@page {@bottom {content: \"Page \" counter(page, upper-alpha) \" of \" counter(pages, decimal);}}#page4 {counter-reset: page 1}</style>"
  }
DOCUMENT_OPTIONS

curl https://YOUR_API_KEY_HERE@api.docraptor.com/docs \
  --fail --silent --show-error \
  --header "Content-Type:application/json" \
  --data @docraptor_parameters.json \
  > page-numbers.pdf

Downloads

Here are all the files you need to follow this tutorial:

Related Tutorials & Documentation

See the complete PDF tutorial list or review the documentation for additional info, such as generating asynchronous documents.

Ready to get started? Try DocRaptor for free with unlimited test documents.