Skip to content

Commit 133b727

Browse files
committed
table->setStretch() optionally avoids the table to stretch to the page width (only for word output)
1 parent 38cb04d commit 133b727

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/PhpWord/Style/Table.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Table extends Border
2828
const WIDTH_AUTO = 'auto'; // Automatically determined width
2929
const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit)
3030
const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip)
31+
const STRETCH_AUTO = 'autofit'; // Automatically stretch the table to fit the page width
32+
const STRETCH_FIXED = 'fixed'; // Do not stretch the table to fit the page width
3133

3234
/**
3335
* Is this a first row style?
@@ -121,6 +123,11 @@ class Table extends Border
121123
*/
122124
private $unit = self::WIDTH_AUTO;
123125

126+
/**
127+
* @var string Stretch the table to the page width
128+
*/
129+
private $stretch = self::STRETCH_AUTO;
130+
124131
/**
125132
* Create new table style
126133
*
@@ -563,6 +570,32 @@ public function setUnit($value = null)
563570
return $this;
564571
}
565572

573+
/**
574+
* Get stretch
575+
*
576+
* @return string
577+
*/
578+
public function getStretch()
579+
{
580+
return $this->stretch;
581+
}
582+
583+
/**
584+
* Set stretch
585+
*
586+
* Stretch the table to the page width
587+
*
588+
* @param string $value
589+
* @return self
590+
*/
591+
public function setStretch($value = null)
592+
{
593+
$enum = array(self::STRETCH_AUTO, self::STRETCH_FIXED);
594+
$this->stretch = $this->setEnumVal($value, $enum, $this->stretch);
595+
596+
return $this;
597+
}
598+
566599
/**
567600
* Get table style only property by checking if it's a firstRow
568601
*

src/PhpWord/Writer/Word2007/Style/Table.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
7474
$styleWriter->write();
7575

7676
$this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit());
77+
$this->writeLayout($xmlWriter, $style->getStretch());
7778
$this->writeMargin($xmlWriter, $style);
7879
$this->writeBorder($xmlWriter, $style);
7980

@@ -104,6 +105,20 @@ private function writeWidth(XMLWriter $xmlWriter, $width, $unit)
104105
$xmlWriter->endElement(); // w:tblW
105106
}
106107

108+
/**
109+
* Enable/Disable automatic resizing of the table
110+
*
111+
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
112+
* @param string $layout autofit / fixed
113+
* @return void
114+
*/
115+
private function writeLayout(XMLWriter $xmlWriter, $stretch)
116+
{
117+
$xmlWriter->startElement('w:tblLayout');
118+
$xmlWriter->writeAttribute('w:type', $stretch);
119+
$xmlWriter->endElement(); // w:tblLayout
120+
}
121+
107122
/**
108123
* Write margin.
109124
*

0 commit comments

Comments
 (0)