Search code examples
node.jspdfmake

pdfMake content bugs when I try to push multiple tables in content


I'm creating a document with pdfMake. In my document, I have three tables: the first show the three best equipments, the second shows the three worst equipments and the third shows all the equipments. It happens that the first table prints ok, but when I try to render the next tables, it does not work as expected (see this image). The code is this:

        const tableHeader = [
        setTableHeader("Código"),
        setTableHeader("OEE"),
        setTableHeader("Disponibilidade"),
        setTableHeader("Performance"),
        setTableHeader("Qualidade"),
    ]
    const equipmentsTable = [
        tableHeader,
        ...sortedEquipsInfos.map((equip)=>[
        equip.eECode,
        equip.oee.toLocaleString('pt-BR', {maximumFractionDigits: 2}) + '%',
        equip.availability.toLocaleString('pt-BR', {maximumFractionDigits: 2}) + '%',
        equip.performance.toLocaleString('pt-BR', {maximumFractionDigits: 2}) + '%',
        equip.quality.toLocaleString('pt-BR', {maximumFractionDigits: 2}) + '%',
    ])];
    ...
    const contentAux = [];
    contentAux.push({
            table: {
                body: [
                    equipmentsTable[0], //header
                    equipmentsTable[equipmentsTable.length - 3],
                    equipmentsTable[equipmentsTable.length - 2],
                    equipmentsTable[equipmentsTable.length - 1],
                ]
                
            }
       });
     contentAux.push({
            table: {
                body: [
                    equipmentsTable[0],
                    equipmentsTable[1],
                    equipmentsTable[2],
                    equipmentsTable[3],
                ]
                
            }
        })
      contentAux.push(
        {
            table: {
                body: equipmentsTable
            },
         }
       )
      ...
      var pdfDoc = printer.createPdfKitDocument({

       ...
      content: contentAux

If I coment the first table, the second works fine. If I coment the first and the second, the third works. Any idea of what is happening?


Solution

  • This happens due to a known bug (or limitation) in pdfmake.

    You cannot reuse the same object reference (equipmentsTable in your case) in the document definition. See https://github.com/bpampuch/pdfmake/issues/465

    A possible workaround is to copy the objects (in all 3 cases):

    ...
    const equipmentsTableCopy1 = JSON.parse(JSON.stringify(equipmentsTable));
    ...
    [
        equipmentsTableCopy1[0],
        equipmentsTableCopy1[1],
        equipmentsTableCopy1[2],
        equipmentsTableCopy1[3],
    ]
    ...