页眉
在许多场景下,需要添加页眉。bkhtmltopdf 基于 Chromium,支持自定义页眉模板。
页眉模板
模板中可使用 Chromium 预定义变量:
变量 | 描述 |
---|---|
pageNumber | 当前页码 |
totalPages | 总页数 |
示例模板
<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>
使用方式
在 API 请求中,通过 pdf 对象配置 headerTemplate
和 displayHeaderFooter: 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;top: 0;text-align: center;color: white\\"><span class=\\"pageNumber\\"></span> of <span class=\\"totalPages\\"></span></div>","displayHeaderFooter":true}}' \
--output bkhtmltopdf.pdf