HTML to PDF
- API 地址: http://127.0.0.1:8080/html-to-pdf
- 请求方法:
POST
- Content-Type:
application/json
参数
名称 | 类型 | 必填 | 说明 | 默认值 |
---|---|---|---|---|
id | String | 否 | 请求唯一标识符 | UUID |
html | String | 是 | 要打印的 HTML 文本 | |
Object | 否 | PDF 参数 | ||
headerTemplate | String | 否 | 页眉 | |
footerTemplate | String | 否 | 页脚 | |
displayHeaderFooter | Boolean | 否 | 只有为 true 时,页眉 和 页脚 才会显示 | false |
generateTaggedPdf | Boolean | 否 | 生成带有标记的 PDF, 这对无障碍 PDF 非常有用 | false |
generateDocumentOutline | Boolean | 否 | 生成带有 大纲 的 PDF 同时 generateTaggedPdf 必须为 true | false |
options | Object | 否 | 打印选项 | |
waitUntil | String | 否 | load : 等待 load 事件domcontentloaded : 等待 DOMContentLoaded 事件manual: 等待用户打印指令 | load |
timeout | Long | 否 | 打印超时时间, 单位: 毫秒 | 15000 |
提示
所有字符串支持 UTF-8。复杂 HTML 建议内联 CSS/JS 以避免加载延迟。
请求体
{
"id": "43eaf69a-194a-4c0f-8e38-726f09eb24ba",
"html": "<html><body>Hi, bkhtmltopdf.</body></html>",
"pdf": {
"footerTemplate": "",
"headerTemplate": "",
"displayHeaderFooter": false,
"generateTaggedPdf": true,
"generateDocumentOutline": true
},
"options": {
"waitUntil": "load"
}
}
响应体
接口返回 PDF 文件流(Content-Type: application/pdf
),可直接在浏览器或 HTTP 客户端中显示或保存。
cURL
curl 'http://localhost:8080/html-to-pdf' \
-H 'Content-Type: application/json' \
--data-raw '{"id":"43eaf69a-194a-4c0f-8e38-726f09eb24ba","html":"<html><body>Hi, bkhtmltopdf.</body></html>","pdf":{"footerTemplate":"","headerTemplate":"","displayHeaderFooter":false,"generateTaggedPdf":true,"generateDocumentOutline":true},"options":{"waitUntil":"load"}}' \
--output bkhtmltopdf.pdf
运行后,bkhtmltopdf.pdf
将生成在当前目录。
manual
manual
模式允许自定义渲染完成时机,特别适合集成 Paged.js 等库。在 HTML
中添加脚本手动触发:
<script>
window.addEventListener('DOMContentLoaded', () => {
// 仅在 waitUntil 等于 manual 时生效
console.debug('print')
})
</script>
- 机制:服务等待
console.debug('print'
)日志。若
15` 秒内未收到,将超时抛出异常。 - 用途:确保 JS/CSS 分页处理完毕,避免打印不完整页面。
- 注意:仅在
waitUntil: "manual"
时生效。