Replies: 2   Views: 846
Group items to prevent page breaks inside the group
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by RDI  · 17-09-2021 - 20:46


I have a bunch of tables that each have their own heading text right above it. This is all dynamically created from data within a database, so the table length can vary.

How can I group each heading and table together so that if 2 tables (each with their own heading) don't fit on one page the 2nd table AND it's header move onto their own additional page?

As it is right now the 2nd table moves onto a new page but the 2nd heading stays on the first page, after the 1st table.

$docx->addText(...); // Heading 1
$docx->addTable(...); // Table 1

$docx->addText(...); // Heading 2
$docx->addTable(...); // Table 2



Posted by admin  · 18-09-2021 - 08:21


You need to enable keepLines and keepNext options:

$docx->addText('Text content', array('keepLines' => true, 'keepNext' => true));

Please note that these options move the content to the same page of the next content, so this text will be moved if the whole table is in the next page. You can also use the addHeading method that applies those styles automatically.

If you want to keep a content and a whole table in the same page even if only some rows are not in the same page, please check the documentation available on (you need to apply keepNext and keepLines in all paragraphs of the table).


Posted by RDI  · 18-09-2021 - 18:25

Thank you! That's very helpful.