最近接到一个需求,要把html生成pdf,生成月报周报。
平时写html很顺手,把html转成pdf,原来就以为是在浏览器里ctrl+p,然后选择保存成pdf就能搞定的,
实际上会遇到这样几个问题(我把解决方案一并写在这里了)
thead {
display: table-header-group; /* ✅ 关键,让表头在跨页时重复 */
}
tfoot {
display: table-footer-group; /* 跨页表尾重复 */
}
tbody {
display: table-row-group; /* 默认即可 */
}
/* 可选:避免行被拆开 */
tr {
page-break-inside: avoid;
}
@bottom-right {
content: "Page " counter(page) " of " counter(pages);
font-size: 14px;
font-weight: 500;
color: #18181b;
margin-right: 20px;
margin-bottom: 16px;
position: relative;
z-index: 1000;
}
@page {
size: A4;
margin: 0; // 左右边距不设置,使用css里的
margin-top: 15mm; //上下边距保持一点没关系
margin-bottom: 15mm;
}
curl \
--request POST 'http://localhost:3005/forms/chromium/convert/html?marginLeft=0cm&marginRight=0cm' \
--form files=@dist/index.html \
-o dist/index.pdf