How to adapt a JSON to create a table in pdfMake?

0

I need to get a JSON that returns from MongoDB and through a forEach generate a table and create a PDF, however I am not able to adapt the JSON to the structure of pdfMake, follows JSON:

{
    "_id" : ObjectId("5978e9f71277a5dae49db945"),
    "userNumId" : 1,
    "numId" : 1,
    "titulo" : "sunt aut facere repellat provnumIdent occaecati excepturi optio reprehenderit",
    "texto" : "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

This is just a JSON document, I got the data from the JSONPlaceholder site and saved it to MongoDB, and now I can not adapt to the pdfMake structure. I need to return all the keys, including "_id", because I want to print all 5 in the table. Thank you very much in advance.

    
asked by anonymous 27.07.2017 / 16:27

1 answer

1

Leveraging the structure of your other question , if I understood the format could do it this way:

function preencherPDF(conteudo) {

    let corpo = [
        ['Id Usuário', 'Id Post', 'Título', 'Texto']
    ];
    conteudo.forEach((item) => corpo.push([item._id.toString(), item.numId, item.titulo, item.texto]));

    let conteudoPDF = {
        content: [{
                text: 'Teste de PDF',
                style: 'header'
            },
            'Teste com pdfMake',
            {
                table: {
                    body: corpo
                }
            }
        ]
    }
}
    
27.07.2017 / 16:56