跳到主要内容

HTML to PDF

参数

名称类型必填说明默认值
idString请求唯一标识符UUID
htmlString要打印的 HTML 文本
pdfObjectPDF 参数
headerTemplateString页眉
footerTemplateString页脚
displayHeaderFooterBoolean只有为 true 时,页眉页脚 才会显示false
generateTaggedPdfBoolean生成带有标记的 PDF,
这对无障碍 PDF 非常有用
false
generateDocumentOutlineBoolean生成带有 大纲 的 PDF
同时 generateTaggedPdf 必须为 true
false
optionsObject打印选项
waitUntilStringload: 等待 load 事件
domcontentloaded: 等待 DOMContentLoaded 事件
manual: 等待用户打印指令
load
timeoutLong打印超时时间, 单位: 毫秒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" 时生效。