From 133b727f592eef4c47d099f543fcdbe45833bc42 Mon Sep 17 00:00:00 2001 From: ale rimoldi Date: Mon, 25 Jul 2016 10:11:52 +0200 Subject: [PATCH] table->setStretch() optionally avoids the table to stretch to the page width (only for word output) --- src/PhpWord/Style/Table.php | 33 +++++++++++++++++++++ src/PhpWord/Writer/Word2007/Style/Table.php | 15 ++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index 24f5066777..4b411cdcf0 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -28,6 +28,8 @@ class Table extends Border const WIDTH_AUTO = 'auto'; // Automatically determined width const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit) const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip) + const STRETCH_AUTO = 'autofit'; // Automatically stretch the table to fit the page width + const STRETCH_FIXED = 'fixed'; // Do not stretch the table to fit the page width /** * Is this a first row style? @@ -121,6 +123,11 @@ class Table extends Border */ private $unit = self::WIDTH_AUTO; + /** + * @var string Stretch the table to the page width + */ + private $stretch = self::STRETCH_AUTO; + /** * Create new table style * @@ -563,6 +570,32 @@ public function setUnit($value = null) return $this; } + /** + * Get stretch + * + * @return string + */ + public function getStretch() + { + return $this->stretch; + } + + /** + * Set stretch + * + * Stretch the table to the page width + * + * @param string $value + * @return self + */ + public function setStretch($value = null) + { + $enum = array(self::STRETCH_AUTO, self::STRETCH_FIXED); + $this->stretch = $this->setEnumVal($value, $enum, $this->stretch); + + return $this; + } + /** * Get table style only property by checking if it's a firstRow * diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index 8bbad107c3..b3686f9091 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -74,6 +74,7 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style) $styleWriter->write(); $this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit()); + $this->writeLayout($xmlWriter, $style->getStretch()); $this->writeMargin($xmlWriter, $style); $this->writeBorder($xmlWriter, $style); @@ -104,6 +105,20 @@ private function writeWidth(XMLWriter $xmlWriter, $width, $unit) $xmlWriter->endElement(); // w:tblW } + /** + * Enable/Disable automatic resizing of the table + * + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param string $layout autofit / fixed + * @return void + */ + private function writeLayout(XMLWriter $xmlWriter, $stretch) + { + $xmlWriter->startElement('w:tblLayout'); + $xmlWriter->writeAttribute('w:type', $stretch); + $xmlWriter->endElement(); // w:tblLayout + } + /** * Write margin. *