Blog

Back to blog posts

Print header, footer, page numbers to PDF with wkhtmltopdf

Published Aug 21, 2018

Intro

Adding a header, footer, and page numbers to your PDFs with wkhtmltopdf is rather straight forward. We provide a list of advanced options here. Wkhtmltopdf is a popular open source PDF rendering engine. The techniques described below will work for wkhtmltopdf as well as Api2Pdf. Api2Pdf is a web service that helps you convert html to pdf and runs on AWS Lambda.

Add Header or Footer to PDF with wkhtmltopdf

The advanced options you can primarily use are footerLeft, footerCenter, footerRight, headerLeft, headerCenter, headerRight — all of which take in standard string formats.

Page Numbers in wkhtmltopdf

To add page numbers to your headers and footers, you need to use the [page] and [topage] variables. Most commonly, you will set a footerRight with the text “Page [page] of [topage]”

Example

The following is an example payload of how you would add a footer with page numbers in wkhtmltopdf using the Api2Pdf.com service.

 

{
"html": "<p>Hello World</p>",
"inlinePdf": true,
"fileName": "test.pdf",
"options": {
"footerRight": "Page [page] of [topage]"
}
}

 

You can learn more about Api2Pdf by visiting its documentation here.