Angular中pdfmake设置表格widths报错:Cannot set '_minWidth' of undefined
解决pdfmake设置表格widths时出现
TypeError: Cannot set property '_minWidth' of undefined的问题 问题根源
你遇到的这个错误,核心原因是表格的列数和widths数组的长度不匹配。仔细看你的代码就能发现:
- 表格
body的第一行有5个单元格:['col1', 'col2', 'col3', 'col4', 'col5'] - 但你设置的
widths数组始终只有4个元素(比如['auto', 'auto', 'auto', 'auto'])
pdfmake在计算表格布局时,会遍历widths数组给每一列分配宽度,当列数多于widths的长度时,就会找不到对应的列对象,从而抛出_minWidth未定义的错误。
解决方案
只需要让widths数组的长度和表格的列数保持一致即可。你的表格是5列,所以widths需要设置5个值:
比如用自适应宽度:
widths: ['auto', 'auto', 'auto', 'auto', 'auto'],
或者根据需求自定义混合宽度:
widths: [80, '*', '*', 100, 50], // 5个值对应5列,*表示自适应剩余空间
修正后的完整文档定义示例
var docDefinition = { content: [ {text: 'Section A: Facilities and Ambience', style: 'sectionStyling'}, { style: 'tableExample', table: { widths: ['auto', 'auto', 'auto', 'auto', 'auto'], // 改为5个元素,匹配列数 body: [ ['col1', 'col2', 'col3', 'col4', 'col5'], ['some text', '1', '1', '100%', '-'], ['some text', '1', '1', '100%', '-'], ['some text', '1', '1', '100%', '-'], ['some text', '1', '1', '100%', '-'], ['some text', '1', '1', '100%', '-'], ['some text', '1', '1', '100%', '-'], ['some text', '1', '1', '100%', '-'], ['some text', '1', '1', '100%', '-'] ] } } ], background: function () { return [ { image: 'watermark', width: 200, alignment : 'center', margin: [0, 325, 0, 0] } ]; }, images: { watermark: 'data:image/base64....' }, styles: { sectionStyling: { fontSize: 18, color: 'blue', bold: true, alignment: 'right', margin: [0, 190, 0, 80] }, subheader: { fontSize: 14 }, superMargin: { margin: [20, 0, 40, 0], fontSize: 15 } }, defaultStyle: { alignment: 'justify' } };
额外说明
当你移除widths配置时,pdfmake会自动给每一列分配默认宽度,所以不会报错。只要保证widths数组长度和表格列数一致,不管是用auto、固定数值还是*(自适应剩余空间),都能正常生成PDF。
内容的提问来源于stack exchange,提问作者Murlidhar Fichadia
相关产品推荐
相关产品推荐

