Skip to main content

Header

In many scenarios, adding a header is required. bkhtmltopdf, based on Chromium, supports custom header templates.

Header Template

Chromium predefined variables can be used in the template:

VariableDescription
pageNumberCurrent page number
totalPagesTotal number of pages

Example Template


<div style="width: 100%;font-size: 20px;position: absolute;top: 0;text-align: center;color: white">
<span class="pageNumber"></span> of <span class="totalPages"></span>
</div>

Usage

Configure headerTemplate and displayHeaderFooter: true in the pdf object within your API request. See the HTML to PDF documentation for details.

curl 'http://localhost:8080/html-to-pdf' \
-H 'Content-Type: application/json' \
--data-raw $'{"html":"<html><body style=\'background:darkgreen;color:white;\'><h1>Hi, bkhtmltopdf.</h1></body></html>","pdf":{"headerTemplate":"<div style=\\"width: 100%;font-size: 20px;position: absolute;top: 0;text-align: center;color: white\\"><span class=\\"pageNumber\\"></span> of <span class=\\"totalPages\\"></span></div>","displayHeaderFooter":true}}' \
--output bkhtmltopdf.pdf

Header example