Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lvc2202 opened this issue Jan 18, 2018 · 22 comments
Milestone

Comments

@lvc2202
Copy link

lvc2202 commented Jan 18, 2018

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.

@lvc2202
Copy link
Author

lvc2202 commented Jan 22, 2018

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

@lvc2202
Copy link
Author

lvc2202 commented Jan 22, 2018

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

@troosan
Copy link
Contributor

troosan commented Jan 22, 2018

might be fixed by #380

@lvc2202
Copy link
Author

lvc2202 commented Jan 23, 2018

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

@lvc2202
Copy link
Author

lvc2202 commented Jan 23, 2018

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

@lvc2202
Copy link
Author

lvc2202 commented Jan 23, 2018

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

@lvc2202
Copy link
Author

lvc2202 commented Jan 23, 2018

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 ?

@lvc2202
Copy link
Author

lvc2202 commented Jan 23, 2018

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.

@lvc2202
Copy link
Author

lvc2202 commented Jan 25, 2018

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

@lvc2202
Copy link
Author

lvc2202 commented Jan 25, 2018

img
This is the screenshot of the problem

@lvc2202
Copy link
Author

lvc2202 commented Jan 29, 2018

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.

@troosan
Copy link
Contributor

troosan commented Feb 2, 2018

@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.

@lvc2202
Copy link
Author

lvc2202 commented Feb 5, 2018

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

@lvc2202
Copy link
Author

lvc2202 commented Feb 5, 2018

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);
        }
    }

`

@troosan
Copy link
Contributor

troosan commented Feb 5, 2018

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>

@lvc2202
Copy link
Author

lvc2202 commented Feb 6, 2018

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>

@lvc2202
Copy link
Author

lvc2202 commented Feb 6, 2018

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>

@troosan troosan mentioned this issue Feb 6, 2018
2 tasks
@lvc2202
Copy link
Author

lvc2202 commented Feb 7, 2018

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>

@troosan
Copy link
Contributor

troosan commented Feb 8, 2018

@lvc2202 try now, should be ok

@lvc2202
Copy link
Author

lvc2202 commented Feb 8, 2018

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

@samimussbach
Copy link

You could find them in #1273

@lvc2202
Copy link
Author

lvc2202 commented Feb 8, 2018

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

@lvc2202 lvc2202 closed this as completed Feb 8, 2018
troosan added a commit that referenced this issue Feb 9, 2018
@troosan troosan added this to the v0.15.0 milestone Feb 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants