PDFMake - make PDF from dynamical content (an array)

╄→尐↘猪︶ㄣ 提交于 2019-12-12 05:14:40

问题


I'm having trouble creating PDF file, where I can put a data from array dynamically, with for loop for example. Length of the array can be different every time so there is no way I'll just write for one size of array.

There is example what I'm able to do:

var stuff = [
        'aaaaaaaaaaaaaaaaaaa',
        'bbbbbbbbbbbbbbbbbbb',
        'ccccccccccccccccccc',
        'ddddddddddddddddddd'
    ]
var dd = {
    content: 
    [
        {text: stuff[0], pageBreak: 'after'},
        {text: stuff[1], pageBreak: 'after'},
        {text: stuff[2], pageBreak: 'after'},
        {text: stuff[3], pageBreak: 'after'}
    ]
}

But I'd love to do something like that:

var stuff = [
        'aaaaaaaaaaaaaaaaaaa',
        'bbbbbbbbbbbbbbbbbbb',
        'ccccccccccccccccccc',
        'ddddddddddddddddddd'
    ]

var dd = {
    content: 
    [
        for(var i=0;i<stuff.length;i++){
          {text: stuff[i], pageBreak: 'after'}        
        }
    ]
}

I know that the second piece of code makes no sence but it shows what I want to do.

Is there any way how to do achieve that?


回答1:


You could use Array#map, like this

var stuff = [
    'aaaaaaaaaaaaaaaaaaa',
    'bbbbbbbbbbbbbbbbbbb',
    'ccccccccccccccccccc',
    'ddddddddddddddddddd'
];

var dd  = {
    content: stuff.map(function(item) {
        return { text: item, pageBreak: 'after'}
    })
};


来源:https://stackoverflow.com/questions/40318654/pdfmake-make-pdf-from-dynamical-content-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!