You need to enable JavaScript to run this app. 如何使用jspdf autotable移除多个表格之间的间距
优惠活动
大模型
产品
解决方案
定价
更多

如何使用jspdf autotable移除多个表格之间的间距

问题解决办法

你设置的margin: {top:0, bottom:0}是表格相对于PDF页面边缘的边距,不是多个连续表格之间的间距,所以不会生效。按以下步骤修改即可:

  • 每次渲染完一个表格后,jsPDF Autotable会将当前表格的最终Y轴坐标存储在doc.lastAutoTable.finalY属性中,下一个表格指定startY为该值,就可以紧贴上个表格底部渲染。
  • 如还有视觉上的微小空隙,可以调整全局单元格内边距cellPadding,或给startY做±1以内的微调抵消线宽带来的偏移。

修改后的代码示例:

const doc = new jsPDF()
// 第一个表格如果要贴页面顶部可以手动指定startY: 0
//@ts-ignore
doc.autoTable({
    theme: 'plain',
    startY: 0,
    margin: {top: 0, bottom: 0},
    // 可选:统一设置单元格内边距避免默认padding产生空隙
    styles: { cellPadding: 1 },
    head: [[ 'Logo mecprog placeholder', {content: 'ORDEM DE SERVIÇO', styles: {fontSize: 20, lineWidth: 1}}, 'logo cliente placeholder']],
    body: [['',{content:  ordemDeServico.cod, styles: {lineWidth: 1}}, '']]
})
//@ts-ignore
doc.autoTable({
    theme: 'plain',
    // 取上个表格的最终Y坐标作为当前表格的起始位置
    startY: doc.lastAutoTable.finalY,
    margin: {top: 0, bottom: 0},
    styles: { cellPadding: 1 },
    head: [[ {content:  'Cliente', styles: {lineWidth: 1}},{content:  'Data Progamada', styles: {lineWidth: 1}}, {content:  'Data Executada', styles: {lineWidth: 1}}]],
    body: [[{content:  ordemDeServico.cliente.nomeFantasia, styles: {lineWidth: 1}}, {content:  ordemDeServico.dataProgramada, styles: {lineWidth: 1}},{content: ordemDeServico.dataExecutada, styles: {lineWidth: 1}}]]
})
//@ts-ignore
doc.autoTable({
    theme: 'plain',
    startY: doc.lastAutoTable.finalY,
    margin: {top: 0, bottom: 0},
    styles: { cellPadding: 1 },
    head: [[{content:  'Equipamento', styles: {lineWidth: 1}} , {content:  'Km/Hora Execução', styles: {lineWidth: 1}}, {content:  'Modelo', styles: {lineWidth: 1}}]],
    body: [[{content:  ordemDeServico.equipamento.equipamento, styles: {lineWidth: 1}},{content: this.check(ordemDeServico.quilometroExecucao), styles: {lineWidth: 1}},{content: this.check(ordemDeServico.equipamento.modelo), styles: {lineWidth: 1}}]]
})
//@ts-ignore
doc.autoTable({
  theme: 'plain',
  startY: doc.lastAutoTable.finalY,
  margin: {top: 0, bottom: 0},
  styles: { cellPadding: 1 },
  head: [[{content:  'Equipamento', styles: {lineWidth: 1}} , {content:  'Km/Hora Execução', styles: {lineWidth: 1}}, {content:  'Modelo', styles: {lineWidth: 1}}]],
  body: [[{content:  ordemDeServico.equipamento.equipamento, styles: {lineWidth: 1}},{content: this.check(ordemDeServico.quilometroExecucao), styles: {lineWidth: 1}},{content: this.check(ordemDeServico.equipamento.modelo), styles: {lineWidth: 1}}]]
})

doc.save('table.pdf')

内容的提问来源于stack exchange,提问作者Pedro Lucas De Moura

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 07:54:02