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

Asked

Viewed 224 times

1

I need to take a JSON that returns from Mongodb and through a foreach generate a table and create a PDF, but I’m not able to adapt the JSON to the pdfMake structure, follows JSON excerpt:

{
    "_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 took the data from the Jsonplaceholder site and saved it on Mongodb, and now I can’t adapt it to the pdfMake structure. I need you to return all keys, including the "_id", because I want to print all 5 in the table. Thanks in advance.

  • Leonardo asks the question of preencherPDF tb.

1 answer

2


Taking advantage of the structure of your another question, if I understood the format could do so:

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
                }
            }
        ]
    }
}
  • Dude, you made a little mistake here with the _id structure, but I think it’s quiet to solve, I don’t know how to thank you...

  • True, the _id vem formato Objectid, editei a resposta @Leonardoebert.

  • Thank you very much Lucas, I was already in this problem for two days, and I didn’t know what else to do.

  • 1

    It worked 100%, I can’t thank you enough...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.