Phpword: Whenever I use italic/bold/underline in a table, it will break into another line when generated into word document.

Created on 18 Jan 2018  Â·  22Comments  Â·  Source: PHPOffice/PHPWord

For example:
I used bold in table and it can become bold but it break into another line as well when generated into word document.
<table border="0" cellpadding="0" cellspacing="0" style="width:500px"><tbody><tr><td>co<strong>lumn</strong> 1</td><td>column 2</td></tr></tbody></table>
I want to use bold in table without breaking into another line.
Any idea how to fix it ? Please help me thank you.

Most helpful comment

You could find them in #1273

All 22 comments

I noticed if there has any tag in between td tag, then it will cause the tag in between td to break into another line. For example: <td>I want to <span>test<span/> it out</td>
it will become as below:
I want to
test
it out

So it's there any way to solve this problem ?

might be fixed by #380

Hi troosan, I just tried but it still the same.

I noticed it didn't go to two of that files which u show me the file in the link.

I noticed it goes to phpoffice/phpword/src/PhpWord/Element instead of going to phpoffice/phpword/src/PhpWord/Writer

Just now I accidentally tried putting a paragraph tag in the html
<table border="0" cellpadding="0" cellspacing="0" style="width:500px"><tbody><tr><td><p>co<strong>lumn</strong> 1</p></td><td><p>column 2</p></td></tr></tbody></table>

It won't break into next line even if I put bold/italic/underline and this is what I wanted.
So what should I do ?

Hi Troosan, is there any way to make the tag (span/bold/italic/underline) to prevent it from breaking into another line in td tag?
Only paragraph tag are allow to break into another line.

Pls help me because I need to fix this bug by this end of tmr. Thank you

img
This is the screenshot of the problem

Now I finally noticed the main problem is caused by Text.
For example : <strong>I want to </strong><em>test.</em>
This above recognize as Text.
Do u guys have any idea on how to fix this? Because I've been thinking, trying, looking and testing for almost 1 weeks.
Pls help me.

@lvc2202 The following fixes your particular case

In PhpWord/Shared/Html change the parseCell as follows

    private static function parseCell($node, $element, &$styles)
    {
        $cellStyles = self::recursiveParseStylesInHierarchy($node, $styles['cell']);

        $colspan = $node->getAttribute('colspan');
        if (!empty($colspan)) {
            $cellStyles['gridSpan'] = $colspan - 0;
        }
        $cell = $element->addCell(null, $cellStyles);

        return $cell->addTextRun();
    }

Basically, instead of returning a cell, directly add a TextRun in it and return that instead.

As I'm not sure this will not bring other issues, I'll not commit it yet, it needs more testing.

Oh I see. I just tried and it show this error "Cannot add Table in TextRun. ".

I found another way is by doing this but not sure will it have any issues in the future.
For now everything is working properly.
Btw, I was using ckeditor for insert html to generate word document.
`

    // Use an XPath query to get all td elements
    $xPath = new \DOMXPath($dom);
    $tdTags = $xPath->query('//td');

    foreach ($tdTags as $tdTag) {
        // Create a new p element and set attributes
        $p = $dom->createElement('p');

        // Move all children of td into p
        $child = $tdTag->firstChild;
        if($child->nodeName!="p" && $child->nodeName!="ol" && $child->nodeName!="ul" && $child->nodeName!="table"){
            while ($child) {
                $nextChild = $child->nextSibling;
                $p->insertBefore($child);
                $child = $nextChild;
            }

            // Move the p inside the td element
            $tdTag->appendChild($p);
        }
    }

`

Can you provide a sample? Do you have tables inside tables?
I tried the following which works fine with the change I proposed:

<table align="center" style="width: 50%; border: 6px #0000FF double;">
    <thead>
        <tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
            <th style="width: 50pt">header a</th>
            <th style="width: 50">header b</th>
            <th style="background-color: #FFFF00; border-width: 12px"><span style="background-color: #00FF00;">header c</span></th>
        </tr>
    </thead>
    <tbody>
        <tr><td style="border-style: dotted;">1</td><td colspan="2">This cell spans the 2 bellow</td></tr>
        <tr><td>This is <b>bold</b> text</td><td></td><td><img src="resources/_earth.jpg" width="50"/></td></tr>
    </tbody>
</table>

Sure why not. This sample has some other language but don't care about it.
The sample is also a bit long hope u don't mind it.
This one don't have table inside table but it encounter error("Cannot add Table in TextRun. ") when using your code.
But sometimes we will use tables inside tables.
<p>Antara berikut, yang manakan nilai tempat dan nilai bagi digit yang bergaris ditulis dengan betul?</p><p>Which of the following, does the place value and the value of the stripe digit be written correctly?</p><table border="0" cellpadding="0" cellspacing="0" style="width:500px"><tbody><tr><td style="text-align:center"> </td><td style="background-color:#aaaaaa; text-align:center">Nombor</td><td style="background-color:#aaaaaa; text-align:center">Nalai tempat</td><td style="background-color:#aaaaaa; text-align:center">Nilai digit</td></tr><tr><td style="text-align:center">A</td><td style="text-align:center">13 <u>8</u>00</td><td style="text-align:center">Ribu</td><td style="text-align:center">8 000</td></tr><tr><td style="text-align:center">B</td><td style="text-align:center">47 70<u>1</u></td><td style="text-align:center">Sa</td><td style="text-align:center">10</td></tr><tr><td style="text-align:center">C</td><td style="text-align:center">6<u>2</u> 772</td><td style="text-align:center">Ratus</td><td style="text-align:center">200</td></tr><tr><td style="text-align:center">D</td><td style="text-align:center; vertical-align:top">93 4<u>0</u>6</td><td style="text-align:center">Puluh</td><td style="text-align:center; vertical-align:top">0</td></tr></tbody></table><p> </p><p> </p><p>Rajah di bawah menunjukkan pernyataan tentang nombor <strong>X</strong>.</p><p><em>The diagram below shows a statement about the number X. </em></p><table border="1" cellpadding="0" cellspacing="0" style="height:182px; width:356px"><tbody><tr><td><table border="0" cellpadding="0" cellspacing="0" style="height:174px; width:440px"><tbody><tr><td style="width:5px">•</td><td>Kedua–dua digit di tempat ratus dan ribu dan ribu ialah <strong>5</strong>.</td></tr><tr><td> </td><td><em>Both digits in hundred and thousand and thousand are <strong>5</strong>. </em></td></tr><tr><td>•</td><td>Nilai digit di tempat puluh ialah <strong>0</strong>.</td></tr><tr><td> </td><td><em>The digit value at the forty point is <strong>0</strong>. </em></td></tr><tr><td>•</td><td>Nilai nombor <strong>X</strong> lebih daripada 30 000.</td></tr><tr><td> </td><td><em>Value <strong>X</strong> is more than 30 000. </em></td></tr></tbody></table><p> </p></td></tr></tbody></table><p>Antara nombor berikut, yang manakah mungkin nombor <strong>X</strong>?</p><p><em>Which of the following numbers does the number X?</em></p><table border="0" cellpadding="0" cellspacing="0" style="width:330px"><tbody><tr><td style="width:22px"><p style="text-align:center"><strong>A</strong></p></td><td><p>15 520</p></td><td style="width:22px"><p style="text-align:center"><strong>B</strong></p></td><td><p>33 550</p></td></tr><tr><td style="width:22px"><p style="text-align:center"><strong>C</strong></p></td><td><p>35 570</p></td><td style="width:22px"><p style="text-align:center"><strong>D</strong></p></td><td style="vertical-align:top"><p>45 500</p></td></tr></tbody></table><p> </p>

This is another sample I made which has tables in tables.

<table border="1" cellpadding="0" cellspacing="0" style="width:500px">
    <tbody>
        <tr>
            <td>Test 1</td>
            <td>Test 2</td>
        </tr>
        <tr>
            <td>
            <p>Test 3</p>

            <table border="0" cellpadding="0" cellspacing="0" style="width:500px">
                <tbody>
                    <tr>
                        <td>a</td>
                        <td>b</td>
                    </tr>
                    <tr>
                        <td>c</td>
                        <td>d</td>
                    </tr>
                    <tr>
                        <td>e</td>
                        <td>f</td>
                    </tr>
                </tbody>
            </table>

            <p>&nbsp;</p>
            </td>
            <td>Test 4</td>
        </tr>
        <tr>
            <td>Test 5</td>
            <td>Test 6</td>
        </tr>
    </tbody>
</table>

Hi Troosan, the above commits works nicely but on certain condition like this below will cause this error ("Cannot add ListItemRun in TextRun")
Sorry for keep bothering you.

<table border="0" cellpadding="0" cellspacing="0" style="width:500px">
    <tbody>
        <tr>
            <td>test page</td>
            <td>result 123</td>
        </tr>
        <tr>
            <td>1. ?&gt; for(;;){--$i;}</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>2. &lt;/td&gt;</td>
            <td>
            <ul>
                <li>&lt;ul&gt;</li>
                <li>&lt;/ul&gt;</li>
                <li>abcd</li>
            </ul>
            </td>
        </tr>
    </tbody>
</table>

@lvc2202 try now, should be ok

Hi troosan, where do I get the changes ?
Because I haven't saw you commit the changes yet.

You could find them in #1273

Awesome, it's working properly now.
Thanks guys ^_^

Was this page helpful?
0 / 5 - 0 ratings