我正在尝试用JavaScript创建pdf使用pdfmake。我的问题是我有数组,但不能循环数组。
PDF格式
function printPDF() {
var activities = [
['Work', 9],
['Eat', 1],
['Commute', 2],
['Play Game', 1],
['Sleep', 7]
];
var dd = {
content: [{
text: 'Teslim Formu',
style: 'header'
},
{
style: 'tableExample',
table: {
body: [
[{
text: 'Toplam Adet',
style: 'subheader'
}, {
text: 'Etiket Değeri',
style: 'subheader'
}, {
text: 'Toplam Ağırlık',
style: 'subheader'
}, {
text: 'Sigorta Değeri',
style: 'subheader'
}, {
text: 'Mağaza Adı',
style: 'subheader'
}, {
text: 'Mağaza Yetkilisi',
style: 'subheader'
}],
["a", "b", "c", "d", "f", "g", "f"]
]
}
},
{
text: 'Ürün Bilgisi',
style: 'margin-header'
},
{
style: 'tableExample',
table: {
body: [
[{
text: 'Adet',
style: 'subheader'
}, {
text: 'Barkod No',
style: 'subheader'
}],
activities.flatMap((item) => {
return [item[0], item[1]]
})
]
}
},
],
styles: {
header: {
fontSize: 14,
bold: true,
margin: [0, 0, 0, 10]
},
'margin-header': {
fontSize: 14,
bold: true,
margin: [0, 20, 0, 10]
},
subheader: {
fontSize: 12,
bold: true,
},
tableHeader: {
bold: true,
fontSize: 13,
color: 'black'
}
},
defaultStyle: {
// alignment: 'justify'
}
}
pdfMake.createPdf(dd).open();
}<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.59/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.59/vfs_fonts.js"></script>
<button class="convertPdf" onclick="printPDF()">PDF</button>
这是我的密码。

我正试着做这样的事。但是它不允许我插入超过一个数组。它允许如果我的活动数组只是' Work ',9,但是如果我添加的数组多于工作9,它会给我带来错误:“无法读取未定义的属性(读取'_calcWidth')”。
发布于 2022-06-13 10:02:16
你身体的第一个数组有6个元素,第二个数组有7个元素('a','b‘.)。
如[医]pdfmake操场所示,表主体的数组不能有不同的大小:
table: {
body: [
["A", "B", "C"],
["D", "E"], (error: pdfmake expected 3 elems here)
],
}作为一种解决办法,您可以向较小的数组中添加一个空字符串,以平衡:
table: {
body: [
["A", "B", "C"],
["C", "D", ""],
],
}https://stackoverflow.com/questions/69675611
复制相似问题