Skip to content

Commit ef99fac

Browse files
ytilottiProgi1984
andauthored
Add support table row height when importing HTML (#2350)
* Add support table row height when importing HTML * fix space * fix phpcsfixer * Add test table row height * Fix xpath test table row height * Fix attribute first test table row height * Fix $cValue Co-authored-by: Progi1984 <[email protected]> * Fix css * Fix test on pt Co-authored-by: Progi1984 <[email protected]>
1 parent 60734dd commit ef99fac

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,11 @@ protected static function parseRow($node, $element, &$styles)
444444
$rowStyles['tblHeader'] = true;
445445
}
446446

447-
return $element->addRow(null, $rowStyles);
447+
// set cell height to control row heights
448+
$height = $rowStyles['height'] ?? null;
449+
unset($rowStyles['height']); // would not apply
450+
451+
return $element->addRow($height, $rowStyles);
448452
}
449453

450454
/**
@@ -808,6 +812,11 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
808812
$styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::AUTO;
809813
}
810814

815+
break;
816+
case 'height':
817+
$styles['height'] = Converter::cssToTwip($value);
818+
$styles['exactHeight'] = true;
819+
811820
break;
812821
case 'border':
813822
case 'border-top':

tests/PhpWordTests/Shared/HtmlTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,58 @@ public function testParseTableAndCellWidth(): void
464464
self::assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
465465
}
466466

467+
/**
468+
* Parse heights in rows, which also allows for controlling column height.
469+
*/
470+
public function testParseTableRowHeight(): void
471+
{
472+
$phpWord = new PhpWord();
473+
$section = $phpWord->addSection([
474+
'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE,
475+
]);
476+
477+
$html = <<<HTML
478+
<table>
479+
<tr style="height: 100px;">
480+
<td>100px</td>
481+
</tr>
482+
<tr style="height: 200pt;">
483+
<td>200pt</td>
484+
</tr>
485+
<tr>
486+
<td>
487+
<table>
488+
<tr style="height: 300px;">
489+
<td>300px</td>
490+
</tr>
491+
</table>
492+
</td>
493+
</tr>
494+
</table>
495+
HTML;
496+
497+
Html::addHtml($section, $html);
498+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
499+
500+
// <tr style="height: 100; ... 100px = 1500 twips (100 / 96 * 1440)
501+
$xpath = '/w:document/w:body/w:tbl/w:tr/w:trPr/w:trHeight';
502+
self::assertTrue($doc->elementExists($xpath));
503+
self::assertEquals(1500, $doc->getElement($xpath)->getAttribute('w:val'));
504+
self::assertEquals('exact', $doc->getElement($xpath)->getAttribute('w:hRule'));
505+
506+
// <tr style="height: 200pt; ... 200pt = 4000 twips (200 / 72 * 1440)
507+
$xpath = '/w:document/w:body/w:tbl/w:tr[2]/w:trPr/w:trHeight';
508+
self::assertTrue($doc->elementExists($xpath));
509+
self::assertEquals(4000, $doc->getElement($xpath)->getAttribute('w:val'));
510+
self::assertEquals('exact', $doc->getElement($xpath)->getAttribute('w:hRule'));
511+
512+
// <tr style="width: 300; .. 300px = 4500 twips (300 / 72 * 1440)
513+
$xpath = '/w:document/w:body/w:tbl/w:tr[3]/w:tc/w:tbl/w:tr/w:trPr/w:trHeight';
514+
self::assertTrue($doc->elementExists($xpath));
515+
self::assertEquals(4500, $doc->getElement($xpath)->getAttribute('w:val'));
516+
self::assertEquals('exact', $doc->getElement($xpath)->getAttribute('w:hRule'));
517+
}
518+
467519
/**
468520
* Test parsing table (attribute border).
469521
*/

0 commit comments

Comments
 (0)