跳到主要内容

页脚

在许多场景下,需要添加页脚。bkhtmltopdf 基于 Chromium,支持自定义页脚模板。

页脚模板

模板中可使用 Chromium 预定义变量:

变量描述
pageNumber当前页码
totalPages总页数

示例模板


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

使用方式

在 API 请求中,通过 pdf 对象配置 footerTemplatedisplayHeaderFooter: true。详见 HTML to PDF 文档。

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;bottom: 0;text-align: center;color: white\\"><span class=\\"pageNumber\\"></span> of <span class=\\"totalPages\\"></span></div>","displayHeaderFooter":true}}' \
--output bkhtmltopdf.pdf

Header example