Skip to content

Azamat8405 supportpadding cell #1

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

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "azamat8405/phpword",
"name": "phpoffice/phpword",
"description": "PHPWord - A pure PHP library for reading and writing word processing documents (OOXML, ODF, RTF, HTML, PDF)",
"keywords": [
"PHP", "PHPOffice", "office", "PHPWord", "word", "template", "template processor", "reader", "writer",
Expand Down
1 change: 1 addition & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- Writer ODText: Support for ListItemRun by [@Progi1984](https://github.com/Progi1984) fixing [#2159](https://github.com/PHPOffice/PHPWord/issues/2159), [#2620](https://github.com/PHPOffice/PHPWord/issues/2620) in [#2669](https://github.com/PHPOffice/PHPWord/pull/2669)
- Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675)
- Writer Word2007: Support for padding in Table Cell by [@Azamat8405](https://github.com/Azamat8405) in [#2697](https://github.com/PHPOffice/PHPWord/pull/2697)
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#2660](https://github.com/PHPOffice/PHPWord/pull/2660)
- Autoload : Allow to use PHPWord without Composer fixing [#2543](https://github.com/PHPOffice/PHPWord/issues/2543), [#2552](https://github.com/PHPOffice/PHPWord/issues/2552), [#2716](https://github.com/PHPOffice/PHPWord/issues/2716), [#2717](https://github.com/PHPOffice/PHPWord/issues/2717) in [#2722](https://github.com/PHPOffice/PHPWord/pull/2722)

Expand Down
52 changes: 52 additions & 0 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,58 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
$styles['spaceAfter'] = Converter::cssToTwip($value);

break;

case 'padding':
$valueTop = $valueRight = $valueBottom = $valueLeft = null;
$cValue = preg_replace('# +#', ' ', trim($value));
$paddingArr = explode(' ', $cValue);
$countParams = count($paddingArr);
if ($countParams == 1) {
$valueTop = $valueRight = $valueBottom = $valueLeft = $paddingArr[0];
} elseif ($countParams == 2) {
$valueTop = $valueBottom = $paddingArr[0];
$valueRight = $valueLeft = $paddingArr[1];
} elseif ($countParams == 3) {
$valueTop = $paddingArr[0];
$valueRight = $valueLeft = $paddingArr[1];
$valueBottom = $paddingArr[2];
} elseif ($countParams == 4) {
$valueTop = $paddingArr[0];
$valueRight = $paddingArr[1];
$valueBottom = $paddingArr[2];
$valueLeft = $paddingArr[3];
}
if ($valueTop !== null) {
$styles['paddingTop'] = Converter::cssToTwip($valueTop);
}
if ($valueRight !== null) {
$styles['paddingRight'] = Converter::cssToTwip($valueRight);
}
if ($valueBottom !== null) {
$styles['paddingBottom'] = Converter::cssToTwip($valueBottom);
}
if ($valueLeft !== null) {
$styles['paddingLeft'] = Converter::cssToTwip($valueLeft);
}

break;
case 'padding-top':
$styles['paddingTop'] = Converter::cssToTwip($value);

break;
case 'padding-right':
$styles['paddingRight'] = Converter::cssToTwip($value);

break;
case 'padding-bottom':
$styles['paddingBottom'] = Converter::cssToTwip($value);

break;
case 'padding-left':
$styles['paddingLeft'] = Converter::cssToTwip($value);

break;

case 'border-color':
self::mapBorderColor($styles, $value);

Expand Down
100 changes: 100 additions & 0 deletions src/PhpWord/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ class Cell extends Border
*/
private $vAlign;

/**
* @var null|int
*/
private $paddingTop;

/**
* @var null|int
*/
private $paddingBottom;

/**
* @var null|int
*/
private $paddingLeft;

/**
* @var null|int
*/
private $paddingRight;

/**
* Text Direction.
*
Expand Down Expand Up @@ -357,4 +377,84 @@ public function getNoWrap(): bool
{
return $this->noWrap;
}

/**
* Get style padding-top.
*/
public function getPaddingTop(): ?int
{
return $this->paddingTop;
}

/**
* Set style padding-top.
*
* @return $this
*/
public function setPaddingTop(int $value): self
{
$this->paddingTop = $value;

return $this;
}

/**
* Get style padding-bottom.
*/
public function getPaddingBottom(): ?int
{
return $this->paddingBottom;
}

/**
* Set style padding-bottom.
*
* @return $this
*/
public function setPaddingBottom(int $value): self
{
$this->paddingBottom = $value;

return $this;
}

/**
* Get style padding-left.
*/
public function getPaddingLeft(): ?int
{
return $this->paddingLeft;
}

/**
* Set style padding-left.
*
* @return $this
*/
public function setPaddingLeft(int $value): self
{
$this->paddingLeft = $value;

return $this;
}

/**
* Get style padding-right.
*/
public function getPaddingRight(): ?int
{
return $this->paddingRight;
}

/**
* Set style padding-right.
*
* @return $this
*/
public function setPaddingRight(int $value): self
{
$this->paddingRight = $value;

return $this;
}
}
34 changes: 34 additions & 0 deletions src/PhpWord/Writer/Word2007/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@ public function write(): void
$xmlWriter->endElement(); // w:tcW
}

$paddingTop = $style->getPaddingTop();
$paddingLeft = $style->getPaddingLeft();
$paddingBottom = $style->getPaddingBottom();
$paddingRight = $style->getPaddingRight();

if ($paddingTop !== null || $paddingLeft !== null || $paddingBottom !== null || $paddingRight !== null) {
$xmlWriter->startElement('w:tcMar');
if ($paddingTop !== null) {
$xmlWriter->startElement('w:top');
$xmlWriter->writeAttribute('w:w', $paddingTop);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:top
}
if ($paddingLeft !== null) {
$xmlWriter->startElement('w:start');
$xmlWriter->writeAttribute('w:w', $paddingLeft);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:start
}
if ($paddingBottom !== null) {
$xmlWriter->startElement('w:bottom');
$xmlWriter->writeAttribute('w:w', $paddingBottom);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:bottom
}
if ($paddingRight !== null) {
$xmlWriter->startElement('w:end');
$xmlWriter->writeAttribute('w:w', $paddingRight);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:end
}
$xmlWriter->endElement(); // w:tcMar
}

// Text direction
$textDir = $style->getTextDirection();
$xmlWriter->writeElementIf(null !== $textDir, 'w:textDirection', 'w:val', $textDir);
Expand Down
61 changes: 61 additions & 0 deletions tests/PhpWordTests/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Converter;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
Expand Down Expand Up @@ -270,6 +271,66 @@ public function testParseLineHeight(): void
self::assertEquals(LineSpacingRule::AUTO, $doc->getElementAttribute('/w:document/w:body/w:p[5]/w:pPr/w:spacing', 'w:lineRule'));
}

public function testParseCellPaddingStyle(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();

$top = 10;
$right = 11;
$bottom = 12;
$left = 13;

$testValTop = Converter::pixelToTwip($top);
$testValRight = Converter::pixelToTwip($right);
$testValBottom = Converter::pixelToTwip($bottom);
$testValLeft = Converter::pixelToTwip($left);

$html = '<table>
<tbody>
<tr>
<td style="padding:' . $top . 'px ' . $right . 'px ' . $bottom . 'px ' . $left . 'px;">full</td>
<td style="padding:' . $top . 'px 0px ' . $bottom . 'px ' . $left . 'px;padding-right:' . $right . 'px;">mix</td>
<td style="padding-top:' . $top . 'px;">top</td>
<td style="padding-bottom:' . $bottom . 'px;">bottom</td>
<td style="padding-left:' . $left . 'px;">left</td>
</tr>
</tbody>
</table>';
Html::addHtml($section, $html);

$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:top';
self::assertTrue($doc->elementExists($path));
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:bottom';
self::assertTrue($doc->elementExists($path));
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:end';
self::assertTrue($doc->elementExists($path));
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:start';
self::assertTrue($doc->elementExists($path));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc[2]/w:tcPr/w:tcMar/w:end';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValRight, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc[3]/w:tcPr/w:tcMar/w:top';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValTop, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc[4]/w:tcPr/w:tcMar/w:bottom';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValBottom, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc[5]/w:tcPr/w:tcMar/w:start';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValLeft, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
}

/**
* Test text-indent style.
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/PhpWordTests/Style/CellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,25 @@ public function testBorderSize(): void
$object->setStyleValue('borderSize', $value);
self::assertEquals($expected, $object->getBorderSize());
}

/**
* Test cell padding.
*/
public function testPadding(): void
{
$object = new Cell();
$methods = [
'paddingTop' => 10,
'paddingBottom' => 20,
'paddingLeft' => 30,
'paddingRight' => 40,
];

foreach ($methods as $methodName => $methodValue) {
$object->setStyleValue($methodName, $methodValue);
$getterName = 'get' . ucfirst($methodName);

self::assertEquals($methodValue, $object->$getterName());
}
}
}
Loading
Loading