Search code examples
javascriptpdfexportpdfmakepage-break

Pdfmake keep two rows together on page break when to page break on table


For the pdfmake, I have a table in which it uses two rows per record. Ex: enter image description here and when the table breaks it breaks by each row. However, I want it to break after or before 2 rows in a table, which you keep the two rows together in one page.

Instead of : enter image description here

I want the two rows to be in the same page. Is there a way to make sure this happens? I've tried to use the pageBreakBefore function, however I'm not that intuitive with it and there haven't been any good examples online.

var dd = {
        content: [
    
        'The following table has nothing more than a body array',
        {
            style: 'tableExample',
            table: {
               dontBreakRows:true,
              //  headerRows:1,
                body: [
                    [{text:'Col', colSpan:3, fillColor:'grey'}, {},{}],
                    [{text:'Column 1',fillColor:'blue'}, {text:'Column 2',fillColor:'blue'}, {text:'Column 3',fillColor:'blue'}],
                    ['One value goes here', 'Another one here', 'OK?'],
                    [{text:'Description:\n'+'sdfgdhfjsfgvbnfhjfhjfghjgfjgfjfhjfghjgffghjgfasfsjgkfldshgfkld',colSpan:3}],
                                        ['One value goes here', 'Another one here', 'OK?'],
                    [{text:'Description:\n'+'sdfgdhfjsfgvbnfhjfhjfghjgfjgfjfhjfghjgffghjgfasfsjgkfldshgfkld',colSpan:3}]
,
                    ['One value goes here', 'Another one here', 'OK?'],
                    [{text:'Description:\n'+'sdfgdhfjsfgvbnfhjfhjfghjgfjgfjfhjfghjgffghjgfasfsjgkfldshgfkld',colSpan:3}]
,                   ['One value goes here', 'Another one here', 'OK?'],
                    [{text:'Description:\n'+'sdfgdhfjsfgvbnfhjfhjfghjgfjgfjfhjfghjgffghjgfasfsjgkfldshgfkld',colSpan:3}]
,                   ['One value goes here', 'Another one here', 'OK?'],
                    [{text:'Description:\n'+'sdfgdhfjsfgvbnfhjfhjfghjgfjgfjfhjfghjgffghjgfasfsjgkfldshgfkld',colSpan:3}]
,                   ['One value goes here', 'Another one here', 'OK?'],
                    [{text:'Description:\n'+'sdfgdhfjsfgvbnfhjfhjfghjgfjgfjfhjfghjgffghjgfasfsjgkfldshgfkld',colSpan:3}]
,


                ],
            }
        },
    ]
    
}

Solution

  • You can handle it like this:

    pageBreakBefore: function(currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) {
      //Here you can change the criteria of pageBreak according to your requirement
      if (currentNode.id === 'myid' && (currentNode.pageNumbers.length != 1 || currentNode.pageNumbers[0] != currentNode.pages)) {
        return true;
      }
      //Here you can change the criteria of pageBreak according to your requirement
      else if (currentNode.id === 'mySecondid' && currentNode.pageNumbers.length != 1) {
        return true;
      }
      return false;
    },