From e61c40e71d8670d3334afeb6e2478d8ee8ef1325 Mon Sep 17 00:00:00 2001
From: Abubakkar Rangara <>
Date: Tue, 24 Jul 2018 13:59:16 +0100
Subject: [PATCH 1/4] Adding table layout to the generated HTML if element has
layout style. This is useful when using creating PDF from PHPWord (e.g. using
dompdf), otherwise the PDF does not contain any layout for table.
---
src/PhpWord/Writer/HTML/Element/Table.php | 4 +++-
tests/PhpWord/Writer/HTML/ElementTest.php | 19 +++++++++++++++++++
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php
index 844066f4..068f489a 100644
--- a/src/PhpWord/Writer/HTML/Element/Table.php
+++ b/src/PhpWord/Writer/HTML/Element/Table.php
@@ -39,7 +39,9 @@ class Table extends AbstractElement
$rows = $this->element->getRows();
$rowCount = count($rows);
if ($rowCount > 0) {
- $content .= '
' . PHP_EOL;
+ $tableStyle = $this->element->getStyle();
+ $tableLayout = $tableStyle === null ? '' : $tableStyle->getLayout();
+ $content .= ''. PHP_EOL;
for ($i = 0; $i < $rowCount; $i++) {
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
$rowStyle = $rows[$i]->getStyle();
diff --git a/tests/PhpWord/Writer/HTML/ElementTest.php b/tests/PhpWord/Writer/HTML/ElementTest.php
index 7a6397ef..1f286c5f 100644
--- a/tests/PhpWord/Writer/HTML/ElementTest.php
+++ b/tests/PhpWord/Writer/HTML/ElementTest.php
@@ -157,4 +157,23 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$this->assertTrue(strpos($content, $expected) !== false);
}
+
+ /**
+ * Tests writing table with layout
+ */
+ public function testWriteTableLayout()
+ {
+ $phpWord = new PhpWord();
+ $section = $phpWord->addSection();
+ $section->addTable();
+ $table = $section->addTable(array('layout' => 'fixed'));
+
+ $row1 = $table->addRow();
+ $row1->addCell()->addText('fixed layout table');
+
+ $dom = $this->getAsHTML($phpWord);
+ $xpath = new \DOMXPath($dom);
+
+ $this->assertEquals('table-layout: fixed', $xpath->query('/html/body/table')->item(0)->attributes->getNamedItem('style')->textContent);
+ }
}
From 4b9ae18d5aefee34bb631b96a752dfca6be56b4d Mon Sep 17 00:00:00 2001
From: Abubakkar Rangara <>
Date: Tue, 24 Jul 2018 14:23:23 +0100
Subject: [PATCH 2/4] Adding table layout to the generated HTML - fixed
php-cs-fixer error
---
src/PhpWord/Writer/HTML/Element/Table.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php
index 068f489a..50c5a777 100644
--- a/src/PhpWord/Writer/HTML/Element/Table.php
+++ b/src/PhpWord/Writer/HTML/Element/Table.php
@@ -41,7 +41,7 @@ class Table extends AbstractElement
if ($rowCount > 0) {
$tableStyle = $this->element->getStyle();
$tableLayout = $tableStyle === null ? '' : $tableStyle->getLayout();
- $content .= ''. PHP_EOL;
+ $content .= '' . PHP_EOL;
for ($i = 0; $i < $rowCount; $i++) {
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
$rowStyle = $rows[$i]->getStyle();
From 32fb85fc8e4ad5bc059574995344fd60dc950aaa Mon Sep 17 00:00:00 2001
From: Christopher ARZUR
Date: Fri, 16 Nov 2018 23:35:57 +0000
Subject: [PATCH 3/4] Added PHP 7.3 support for travis (#1495)
* Added PHP 7.3 support for travis
* mark php 7.3 as failable
---
.travis.yml | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index db77ff05..6fcdad43 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -10,11 +10,19 @@ php:
- 7.0
- 7.1
- 7.2
+ - 7.3
matrix:
include:
- php: 7.0
env: COVERAGE=1
+ - php: 5.3
+ env: COMPOSER_MEMORY_LIMIT=2G
+ exclude:
+ - php: 7.0
+ - php: 5.3
+ allow_failures:
+ - php: 7.3
cache:
directories:
@@ -32,7 +40,7 @@ before_install:
before_script:
## Deactivate xdebug if we don't do code coverage
- - if [ -z "$COVERAGE" ]; then phpenv config-rm xdebug.ini ; fi
+ - if [ -z "$COVERAGE" ]; then phpenv config-rm xdebug.ini || echo "xdebug not available" ; fi
## Composer
- composer self-update
- travis_wait composer install --prefer-source
From b50de97a41f29987901b1370b74cbf08c643e741 Mon Sep 17 00:00:00 2001
From: troosan
Date: Wed, 28 Nov 2018 22:54:57 +0100
Subject: [PATCH 4/4] support `auto` table layout too
---
CHANGELOG.md | 1 +
src/PhpWord/Writer/HTML/Element/Table.php | 26 ++++++++++++++++++++---
tests/PhpWord/Writer/HTML/ElementTest.php | 11 +++++++---
3 files changed, 32 insertions(+), 6 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index abf34834..7e2325fa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ v0.16.0 (xx xxx 2018)
### Fixed
- Fix regex in `cloneBlock` function @nicoder #1269
- HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436
+- Adding table layout to the generated HTML @aarangara #1441
v0.15.0 (14 Jul 2018)
----------------------
diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php
index 50c5a777..a5143d2b 100644
--- a/src/PhpWord/Writer/HTML/Element/Table.php
+++ b/src/PhpWord/Writer/HTML/Element/Table.php
@@ -39,9 +39,8 @@ class Table extends AbstractElement
$rows = $this->element->getRows();
$rowCount = count($rows);
if ($rowCount > 0) {
- $tableStyle = $this->element->getStyle();
- $tableLayout = $tableStyle === null ? '' : $tableStyle->getLayout();
- $content .= '' . PHP_EOL;
+ $content .= 'element->getStyle()) . '>' . PHP_EOL;
+
for ($i = 0; $i < $rowCount; $i++) {
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
$rowStyle = $rows[$i]->getStyle();
@@ -104,4 +103,25 @@ class Table extends AbstractElement
return $content;
}
+
+ /**
+ * Translates Table style in CSS equivalent
+ *
+ * @param \PhpOffice\PhpWord\Style\Table|null $tableStyle
+ * @return string
+ */
+ private function getTableStyle(\PhpOffice\PhpWord\Style\Table $tableStyle = null)
+ {
+ if ($tableStyle == null) {
+ return '';
+ }
+ $style = ' style="';
+ if ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED) {
+ $style .= 'table-layout: fixed;';
+ } elseif ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO) {
+ $style .= 'table-layout: auto;';
+ }
+
+ return $style . '"';
+ }
}
diff --git a/tests/PhpWord/Writer/HTML/ElementTest.php b/tests/PhpWord/Writer/HTML/ElementTest.php
index 1f286c5f..73c6ede9 100644
--- a/tests/PhpWord/Writer/HTML/ElementTest.php
+++ b/tests/PhpWord/Writer/HTML/ElementTest.php
@@ -166,14 +166,19 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addTable();
- $table = $section->addTable(array('layout' => 'fixed'));
- $row1 = $table->addRow();
+ $table1 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED));
+ $row1 = $table1->addRow();
$row1->addCell()->addText('fixed layout table');
+ $table2 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO));
+ $row2 = $table2->addRow();
+ $row2->addCell()->addText('auto layout table');
+
$dom = $this->getAsHTML($phpWord);
$xpath = new \DOMXPath($dom);
- $this->assertEquals('table-layout: fixed', $xpath->query('/html/body/table')->item(0)->attributes->getNamedItem('style')->textContent);
+ $this->assertEquals('table-layout: fixed;', $xpath->query('/html/body/table[1]')->item(0)->attributes->getNamedItem('style')->textContent);
+ $this->assertEquals('table-layout: auto;', $xpath->query('/html/body/table[2]')->item(0)->attributes->getNamedItem('style')->textContent);
}
}