Skip to content

Commit 3e182f0

Browse files
OlisaevAGOlisaevAG
authored andcommitted
Support for padding in a table cell
1 parent feadceb commit 3e182f0

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,53 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
803803
$styles['spaceAfter'] = Converter::cssToTwip($value);
804804

805805
break;
806+
807+
case 'padding':
808+
$valueTop = $valueRight = $valueBottom = $valueLeft = null;
809+
$cValue = preg_replace('# +#', ' ', trim($value));
810+
$paddingArr = explode(' ', $cValue);
811+
$countParams = count($paddingArr);
812+
if ($countParams == 1) {
813+
$valueTop = $valueRight = $valueBottom = $valueLeft = $paddingArr[0];
814+
} elseif ($countParams == 2) {
815+
$valueTop = $valueBottom = $paddingArr[0];
816+
$valueRight = $valueLeft = $paddingArr[1];
817+
} elseif ($countParams == 3) {
818+
$valueTop = $paddingArr[0];
819+
$valueRight = $valueLeft = $paddingArr[1];
820+
$valueBottom = $paddingArr[2];
821+
} elseif ($countParams == 4) {
822+
$valueTop = $paddingArr[0];
823+
$valueRight = $paddingArr[1];
824+
$valueBottom = $paddingArr[2];
825+
$valueLeft = $paddingArr[3];
826+
}
827+
if ($valueTop !== null) {
828+
$styles['paddingTop'] = Converter::cssToTwip($valueTop);
829+
}
830+
if ($valueRight !== null) {
831+
$styles['paddingRight'] = Converter::cssToTwip($valueRight);
832+
}
833+
if ($valueBottom !== null) {
834+
$styles['paddingBottom'] = Converter::cssToTwip($valueBottom);
835+
}
836+
if ($valueLeft !== null) {
837+
$styles['paddingLeft'] = Converter::cssToTwip($valueLeft);
838+
}
839+
break;
840+
case 'padding-top':
841+
$styles['paddingTop'] = Converter::cssToTwip($value);
842+
break;
843+
case 'padding-right':
844+
$styles['paddingRight'] = Converter::cssToTwip($value);
845+
break;
846+
case 'padding-bottom':
847+
$styles['paddingBottom'] = Converter::cssToTwip($value);
848+
break;
849+
case 'padding-left':
850+
$styles['paddingLeft'] = Converter::cssToTwip($value);
851+
break;
852+
806853
case 'border-color':
807854
self::mapBorderColor($styles, $value);
808855

src/PhpWord/Style/Cell.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ class Cell extends Border
7373
*/
7474
private $vAlign;
7575

76+
/**
77+
* @var null|int
78+
*/
79+
private $paddingTop;
80+
81+
/**
82+
* @var null|int
83+
*/
84+
private $paddingBottom;
85+
86+
/**
87+
* @var null|int
88+
*/
89+
private $paddingLeft;
90+
91+
/**
92+
* @var null|int
93+
*/
94+
private $paddingRight;
95+
7696
/**
7797
* Text Direction.
7898
*
@@ -356,4 +376,86 @@ public function getNoWrap(): bool
356376
{
357377
return $this->noWrap;
358378
}
379+
380+
381+
/**
382+
* Get style padding-top.
383+
* @return mixed
384+
*/
385+
public function getPaddingTop()
386+
{
387+
return $this->paddingTop;
388+
}
389+
/**
390+
* Set style padding-top.
391+
* @param int $value
392+
*
393+
* @return $this
394+
*/
395+
public function setPaddingTop(int $value): Cell
396+
{
397+
$this->paddingTop = $value;
398+
return $this;
399+
}
400+
/**
401+
* Get style padding-bottom.
402+
* @return mixed
403+
*/
404+
public function getPaddingBottom()
405+
{
406+
return $this->paddingBottom;
407+
}
408+
/**
409+
* Set style padding-bottom.
410+
* @param int $value
411+
*
412+
* @return $this
413+
*/
414+
public function setPaddingBottom(int $value): Cell
415+
{
416+
$this->paddingBottom = $value;
417+
return $this;
418+
}
419+
/**
420+
* Get style padding-left.
421+
* @return mixed
422+
*/
423+
public function getPaddingLeft()
424+
{
425+
return $this->paddingLeft;
426+
}
427+
/**
428+
* Set style padding-left.
429+
* @param int $value
430+
*
431+
* @return $this
432+
*/
433+
public function setPaddingLeft(int $value): Cell
434+
{
435+
$this->paddingLeft = $value;
436+
437+
return $this;
438+
}
439+
440+
/**
441+
* Get style padding-right.
442+
* @return mixed
443+
*/
444+
public function getPaddingRight()
445+
{
446+
return $this->paddingRight;
447+
}
448+
449+
/**
450+
* Set style padding-right.
451+
* @param int $value
452+
*
453+
* @return $this
454+
*/
455+
public function setPaddingRight(int $value): Cell
456+
{
457+
$this->paddingRight = $value;
458+
459+
return $this;
460+
}
359461
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,40 @@ public function write(): void
5454
$xmlWriter->endElement(); // w:tcW
5555
}
5656

57+
$paddingTop = $style->getPaddingTop();
58+
$paddingLeft = $style->getPaddingLeft();
59+
$paddingBottom = $style->getPaddingBottom();
60+
$paddingRight = $style->getPaddingRight();
61+
62+
if($paddingTop !== null || $paddingLeft !== null || $paddingBottom !== null || $paddingRight !== null){
63+
$xmlWriter->startElement('w:tcMar');
64+
if($paddingTop !== null) {
65+
$xmlWriter->startElement('w:top');
66+
$xmlWriter->writeAttribute('w:w', $paddingTop);
67+
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
68+
$xmlWriter->endElement(); // w:top
69+
}
70+
if($paddingLeft !== null) {
71+
$xmlWriter->startElement('w:start');
72+
$xmlWriter->writeAttribute('w:w', $paddingLeft);
73+
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
74+
$xmlWriter->endElement(); // w:start
75+
}
76+
if($paddingBottom !== null) {
77+
$xmlWriter->startElement('w:bottom');
78+
$xmlWriter->writeAttribute('w:w', $paddingBottom);
79+
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
80+
$xmlWriter->endElement(); // w:bottom
81+
}
82+
if($paddingRight !== null){
83+
$xmlWriter->startElement('w:end');
84+
$xmlWriter->writeAttribute('w:w', $paddingRight);
85+
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
86+
$xmlWriter->endElement(); // w:end
87+
}
88+
$xmlWriter->endElement(); // w:tcMar
89+
}
90+
5791
// Text direction
5892
$textDir = $style->getTextDirection();
5993
$xmlWriter->writeElementIf(null !== $textDir, 'w:textDirection', 'w:val', $textDir);

0 commit comments

Comments
 (0)