Forum


Replies: 1   Views: 144
Html fragment in a docx file: avoid page breaks between heading and content and in tables
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 ralfjahr  · 22-08-2024 - 08:19

We are adding several sections with section headline, paragraphs and tables to word files. The data is prepared as HTML and then added to the docx file (premium license). Is there a way to avoid page breaks between the section headline, the paragraphs and (especially in) a table?

We tried "page-break-inside: avoid;" and "page-break-after: avoid;" (https://www.phpdocx.com/documentation/snippets/prevent-page-break-paragraph-html) but were not successful. Where do we need to position these css attributes? Any magic trick which we forgot? 

Thanks!

Posted by admin  · 22-08-2024 - 08:32

Hello,

Please note that the samples detailed on the documentation pages are fully tested and working:

page-break-inside and page-break-after styles are available in paragraphs (https://www.phpdocx.com/htmlapi-documentation/html-standard/insert-paragraph-text-Word-document-with-HTML) (and other elements that generate paragraph contents such as headings or list items).

A sample using page-break-inside and page-break-after styles in a table using inline styles:

$html = '
<table border="1" style="border-collapse: collapse" width="600">
    <tbody>
        <tr width="600">
            <td><p style="page-break-inside: avoid; page-break-after: avoid;">1_1</p></td>
            <td rowspan="3" colspan="2"><p style="page-break-inside: avoid; page-break-after: avoid;">1_2</p></td>
        </tr>
        <tr width="600">
            <td><p style="page-break-inside: avoid; page-break-after: avoid;">Some random text.</p></td>
        </tr>
        <tr width="600">
            <td>
                <ul>
                    <li style="page-break-inside: avoid; page-break-after: avoid;">One</li>
                    <li style="page-break-inside: avoid; page-break-after: avoid;">Two <b>and a half</b></li>
                </ul>
            </td>
        </tr>
        <tr width="600">
            <td><p style="page-break-inside: avoid; page-break-after: avoid;">3_2</p></td>
            <td><p style="page-break-inside: avoid; page-break-after: avoid;">3_3</p></td>
            <td><p style="page-break-inside: avoid; page-break-after: avoid;">3_3</p></td>
        </tr>
    </tbody>
</table>';
$docx->embedHTML($html);

The same sample using a style tag:

$html = '
<style>
    table p, table li {
        page-break-inside: avoid;
        page-break-after: avoid;
    }
</style>
<table border="1" style="border-collapse: collapse" width="600">
    <tbody>
        <tr width="600">
            <td><p>1_1</p></td>
            <td rowspan="3" colspan="2"><p>1_2</p></td>
        </tr>
        <tr width="600">
            <td><p>Some random text.</p></td>
        </tr>
        <tr width="600">
            <td>
                <ul>
                    <li>One</li>
                    <li>Two <b>and a half</b></li>
                </ul>
            </td>
        </tr>
        <tr width="600">
            <td><p>3_2</p></td>
            <td><p>3_3</p></td>
            <td><p>3_3</p></td>
        </tr>
    </tbody>
</table>';
$docx->embedHTML($html);

A sample using page-break-inside and page-break-after styles in paragraphs using inline styles:

$html = '
<p>A content (no page break style)</p>
<p style="page-break-inside: avoid; page-break-after: avoid;">Some text</p>
<ol>
    <li style="page-break-inside: avoid; page-break-after: avoid;">Item 1</li>
    <li style="page-break-inside: avoid; page-break-after: avoid;">Item 2</li>
</ol>
<p>More content (the page-break-after from the previous li tag is applied to this content)</p>
<p>End content (no page break style)</p>
';
$docx->embedHTML($html);

The same sample using a style tag:

$html = '
<style>
.avoidpb, ol > li {
    page-break-inside: avoid; page-break-after: avoid;
}
</style>
<p>A content (no page break style)</p>
<p class="avoidpb">Some text</p>
<ol>
    <li>Item 1</li>
    <li>Item 2</li>
</ol>
<p>More content (the page-break-after from the previous li tag is applied to this content)</p>
<p>Other content (no page break style)</p>
';
$docx->embedHTML($html);

A similar sample using paragraphs and a h2 tag:

$html = '
<style>
h2, .avoidpb {
    page-break-inside: avoid; page-break-after: avoid;
}
</style>
<p>A content (no page break style)</p>
<h2>Heading content</h2>
<p class="avoidpb">Content 1</p>
<p class="avoidpb">Content 2</p>
<p class="avoidpb">Content 3</p>
<p>More content (the page-break-after from the previous li tag is applied to this content)</p>
<p>Other content (no page break style)</p>
';
$docx->embedHTML($html);

All these samples are fully tested and working correctly.

Please note the following about using keep styles:

For further support, please post or send to contact[at]phpdocx.com the simplest code that illustrates your issue and we'll check it.

Regards.