chimos
chimos

Reputation: 664

DataTables PDF export (pdfmake): how to prevent breaking rows in page breaks?

I am trying to customize the PDF-Export with DataTables (which uses pdfmake). All my custom styles are working except dontBreakRows, it doesn't seem to make any difference, so table rows contents are being splited in page breaks, instead of keeping them together on the next page when they don't fit in the previous, which is what I had expected.

My pdfmake version is 0.1.27 (last one, on the date), DataTables 1.10.12, Buttons 1.2.1

This is my code:

(...)
$.extend( true, {}, buttonCommon, {
    'extend': 'pdf',
    'text': 'PDF A4',
    'orientation': 'landscape',
    'pageSize': 'A4',
    'message': 'Date '+todays,
    'customize': function (doc) {
      doc.content.splice(1, 0, {
        margin: \{0, -32, 0, 8\},
        alignment: 'right',
        image: 'data:image/png;base64,(...)'
      });
      //These styles are working:
      doc.defaultStyle.fontSize = 10;
      doc.styles.title.fontSize = 12;
      doc.styles.tableHeader.fontSize = 11;
      doc.styles.tableFooter.fontSize = 11;
      doc.styles.tableHeader.alignment = 'left';
      doc.styles.title.bold = true;
      doc.styles.tableHeader.bold = true;
      doc.styles.tableHeader.color = '#ffffff';
      doc.styles.tableHeader.fillColor = '#666666';
      doc.styles.tableBodyOdd.fillColor = '#ffffff';
      doc.styles.tableBodyEven.fillColor = '#e9e9e9';
      doc.styles.tableHeader.noWrap = true;

      //It doesn't work:
      doc.styles.tableBodyOdd.dontBreakRows = true;
      doc.styles.tableBodyEven.dontBreakRows = true;
      doc.styles.tableBodyOdd.pageBreak = 'before';
      doc.styles.tableBodyEven.pageBreak = 'before';

      //It doesn't work neither:
      doc.defaultStyle = 
        {
          dontBreakRows: true
        }              
    }

}),
(...)

What am I doing wrong? Thank you!

Upvotes: 3

Views: 3259

Answers (2)

Marcos Lima
Marcos Lima

Reputation: 1

You are using bodyOdd and bodyEven, so you need to use

doc.styles.tableBodyOdd.noWrap = true;
doc.styles.tableBodyEven.noWrap = true;

Upvotes: 0

TOS
TOS

Reputation: 71

I had exactly the same problem. The styles are not working.

For me this worked (depending on the table's position in content):

doc.content[1].table.dontBreakRows = true; 

If you figured out a better way, just let me know

Upvotes: 6

Related Questions