如何使用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
相关产品推荐
相关产品推荐

