-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Using table tag in HTML Reader produces no output #324
Comments
Hello |
@hregis l do, in src/PhpWord/Shared/Html.php parseChildNodes(): private static function parseChildNodes($node, $element, $styles, $data)
{
if ($node->nodeName != 'li') {
$cNodes = $node->childNodes;
if (count($cNodes) > 0) {
foreach ($cNodes as $cNode) {
if ($element instanceof AbstractContainer) {
self::parseNode($cNode, $element, $styles, $data);
}
}
}
}
} Change this to: private static function parseChildNodes($node, $element, $styles, $data)
{
if ($node->nodeName != 'li') {
$cNodes = $node->childNodes;
if (count($cNodes) > 0) {
foreach ($cNodes as $cNode) {
// if ($element instanceof AbstractContainer) {
self::parseNode($cNode, $element, $styles, $data);
// }
}
}
}
} Also could this issue please be labelled as a Bug, it's definitely not a Question. |
@EK1771 thank you, but i have this error with develop branch: |
I had the same issue where tables were not being parsed from HTML to DOM. The problem is that HTML elements @EK1771 solution removes the check against Abstract containers, but also causes every element to be checked for children, even when some are not containers. There are a couple of steps to fix this.
/PhpWord/Shared/Html.php:parseNode()
This works for me, hope it helps others. I'm sure there is a more elegent solution that could be incorporated in the Develop branch. It needs to be exapanded to deal with Mark |
Any news? The bug still occours |
by using \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html) we can interpret html to word. Can we set alignment options for this output (such as align right/left/both) ? |
@mogilvie are you able to share your complete and working Html class //node mapping table
'table' => array('Table', $node, $element, $styles, null, 'addTable', true),
'thead' => array('Table', $node, $element, $styles, null, 'skipThead', true),
'tbody' => array('Table', $node, $element, $styles, null, 'skipTbody', true),
'tr' => array('Table', $node, $element, $styles, null, 'addRow', true),
'td' => array('Table', $node, $element, $styles, null, 'addCell', true),
'th' => array('Table', $node, $element, $styles, null, 'addCell', true),
/**
* Parse child nodes.
*
* @param \DOMNode $node
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
* @param array $styles
* @param array $data
* @return void
*/
private static function parseChildNodes($node, $element, $styles, $data)
{
if ('li' != $node->nodeName) {
$cNodes = $node->childNodes;
if (count($cNodes) > 0) {
foreach ($cNodes as $cNode) {
// Added to get tables to work
$htmlContainers = array(
'thead',
'tbody',
'tr',
'td',
'th',
);
if (in_array($cNode->nodeName, $htmlContainers)) {
self::parseNode($cNode, $element, $styles, $data);
}
if ($element instanceof AbstractContainer) {
self::parseNode($cNode, $element, $styles, $data);
}
}
}
}
}
private static function parseTable($node, $element, &$styles, $argument1)
{
switch ($argument1) {
case 'addTable':
$styles['paragraph'] = self::parseInlineStyle($node, $styles['paragraph']);
$newElement = $element->addTable('table', array('width' => 90));
break;
case 'skipThead':
case 'skipTbody':
$newElement = $element;
break;
case 'addRow':
$newElement = $element->addRow();
break;
case 'addCell':
$newElement = $element->addCell(1750);
break;
} Sample: $html .= '<table><thead><tr><th>Header of column 1</th><th>Header of column 2</th></tr></thead>';
$html .= '<tbody><tr><td>Row 1 for column 1</td><td>Row 1 for column 2</td></tr></tbody></table>'; |
I'll post the full class tonight, but in the meantime, is the error caused by the switch statement not having any code to be executed for case: 'skipThead'? |
@mogilvie I doubt that - it should fall through to the 'skipTbody' case because there's no break statement. I don't fully understand how this works but I assumed that thead would need to be handled in the same way as tbody. |
@garethellis36 Agree re skipThead. Is the parseTable function returning the $newElement? It's cut off from the sample html class. |
Html.php uploaded as a text file. It was working as of Jan 2015. |
Thank you, this is very very helpful. The embedded table is printing like charm now. I am still stuck with an embedded nested list, if you can help. There are two issues, 1. List item not printing if Thank you |
This issue still occurs... The @garethellis36 version dont work for me (PHPWord version 0.13). So I work a little improving the HTML class.
More details see: http://stackoverflow.com/questions/29275140/html-reader-from-phpword-doest-work-with-tables/40600565#40600565 |
How to cellspacing and cellpadding on table on Html table? It's not working :( |
Sample Code:
Expected Output:
Table with one cell containing the word "test".
Actual Output:
Blank
From stepping through the code quickly, the issue seems to be caused by the following if condition in parseChildNodes():
Commenting out the if condition then allows for the sample code above to produce the expected output.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: