diff --git a/CHANGELOG.md b/CHANGELOG.md index 25689dbc..2ea77c02 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,11 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ### Features - Image: Get image dimensions without EXIF extension - @andrew-kzoo GH-184 -- Table: Add tblGrid element for Libre/Open Office table sizing - @gianis6 GH-183 +- Table: Add `tblGrid` element for Libre/Open Office table sizing - @gianis6 GH-183 - Footnote: Ability to insert textbreak in footnote `$footnote->addTextBreak()` - @ivanlanin - Footnote: Ability to style footnote reference mark by using `FootnoteReference` style - @ivanlanin +- Font: Add `bgColor` to font style to define background using HEX color - @jcarignan GH-168 +- Table: Add `exactHeight` to row style to define whether row height should be exact or atLeast - @jcarignan GH-168 ### Bugfixes diff --git a/docs/elements.rst b/docs/elements.rst index f7ce0319..d8b50e30 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -67,6 +67,7 @@ Available font styles: - ``strikethrough`` Strikethrough, *true* or *false* - ``color`` Font color, e.g. *FF0000* - ``fgColor`` Font highlight color, e.g. *yellow*, *green*, *blue* +- ``bgColor`` Font background color, e.g. *FF0000* Paragraph style ^^^^^^^^^^^^^^^ @@ -201,27 +202,28 @@ Table, row, and cell styles Table styles: -- ``$width`` Table width in percent -- ``$bgColor`` Background color, e.g. '9966CC' -- ``$border(Top|Right|Bottom|Left)Size`` Border size in twips -- ``$border(Top|Right|Bottom|Left)Color`` Border color, e.g. '9966CC' -- ``$cellMargin(Top|Right|Bottom|Left)`` Cell margin in twips +- ``width`` Table width in percent +- ``bgColor`` Background color, e.g. '9966CC' +- ``border(Top|Right|Bottom|Left)Size`` Border size in twips +- ``border(Top|Right|Bottom|Left)Color`` Border color, e.g. '9966CC' +- ``cellMargin(Top|Right|Bottom|Left)`` Cell margin in twips Row styles: - ``tblHeader`` Repeat table row on every new page, *true* or *false* - ``cantSplit`` Table row cannot break across pages, *true* or *false* +- ``exactHeight`` Row height is exact or at least Cell styles: -- ``$width`` Cell width in twips -- ``$valign`` Vertical alignment, *top*, *center*, *both*, *bottom* -- ``$textDirection`` Direction of text -- ``$bgColor`` Background color, e.g. '9966CC' -- ``$border(Top|Right|Bottom|Left)Size`` Border size in twips -- ``$border(Top|Right|Bottom|Left)Color`` Border color, e.g. '9966CC' -- ``$gridSpan`` Number of columns spanned -- ``$vMerge`` *restart* or *continue* +- ``width`` Cell width in twips +- ``valign`` Vertical alignment, *top*, *center*, *both*, *bottom* +- ``textDirection`` Direction of text +- ``bgColor`` Background color, e.g. '9966CC' +- ``border(Top|Right|Bottom|Left)Size`` Border size in twips +- ``border(Top|Right|Bottom|Left)Color`` Border color, e.g. '9966CC' +- ``gridSpan`` Number of columns spanned +- ``vMerge`` *restart* or *continue* Cell span ~~~~~~~~~ diff --git a/samples/Sample_20_BGColor.php b/samples/Sample_20_BGColor.php new file mode 100644 index 00000000..46a232f7 --- /dev/null +++ b/samples/Sample_20_BGColor.php @@ -0,0 +1,23 @@ +createSection(); + +$section->addText("This is some text highlighted using fgColor (limited to 15 colors) ", array("fgColor" => \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW)); +$section->addText("This one uses bgColor and is using hex value (0xfbbb10)", array("bgColor" => "fbbb10")); +$section->addText("Compatible with font colors", array("color"=>"0000ff", "bgColor" => "fbbb10")); + +// Save file +$name = basename(__FILE__, '.php'); +$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf'); +foreach ($writers as $writer => $extension) { + echo date('H:i:s'), " Write to {$writer} format", \EOL; + $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer); + $xmlWriter->save("{$name}.{$extension}"); + rename("{$name}.{$extension}", "results/{$name}.{$extension}"); +} + +include_once 'Sample_Footer.php'; diff --git a/samples/Sample_21_TableRowRules.php b/samples/Sample_21_TableRowRules.php new file mode 100644 index 00000000..c02ac2ba --- /dev/null +++ b/samples/Sample_21_TableRowRules.php @@ -0,0 +1,40 @@ +createSection(); + +$section->addText("By default, when you insert an image, it adds a textbreak after its content."); +$section->addText("If we want a simple border around an image, we wrap the image inside a table->row->cell"); +$section->addText("On the image with the red border, even if we set the row height to the height of the image, the textbreak is still there:"); + +$table1 = $section->addTable(array("cellMargin" => 0, "cellMarginRight" => 0, "cellMarginBottom" => 0, "cellMarginLeft" => 0)); +$table1->addRow(3750); +$cell1 = $table1->addCell(null, array("valign" => "top", "borderSize" => 30, "borderColor" => "ff0000")); +$cell1->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center")); + +$section->addTextBreak(); +$section->addText("But if we set the rowStyle 'exactHeight' to true, the real row height is used, removing the textbreak:"); + +$table2 = $section->addTable(array("cellMargin" => 0, "cellMarginRight" => 0, "cellMarginBottom" => 0, "cellMarginLeft" => 0)); +$table2->addRow(3750, array("exactHeight" => true)); +$cell2 = $table2->addCell(null, array("valign" => "top", "borderSize" => 30, "borderColor" => "00ff00")); +$cell2->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center")); + +$section->addTextBreak(); +$section->addText("In this example, image is 250px height. Rows are calculated in twips, and 1px = 15twips."); +$section->addText("So: $"."table2->addRow(3750, array('exactHeight'=>true));"); + +// Save file +$name = basename(__FILE__, '.php'); +$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf'); +foreach ($writers as $writer => $extension) { + echo date('H:i:s'), " Write to {$writer} format", \EOL; + $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer); + $xmlWriter->save("{$name}.{$extension}"); + rename("{$name}.{$extension}", "results/{$name}.{$extension}"); +} + +include_once 'Sample_Footer.php'; diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 3ab855d6..3d235343 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -135,6 +135,18 @@ class Font */ private $_fgColor = null; + /** + * Background color + * + * @var string + */ + private $_bgColor = null; + /** + * Text line height + * + * @var int + */ + /** * Text line height * @@ -454,6 +466,28 @@ class Font return $this; } + /** + * Get background color + * + * @return string + */ + public function getBgColor() + { + return $this->_bgColor; + } + + /** + * Set background color + * + * @param string $pValue + * @return PHPWord_Style_Font + */ + public function setBgColor($pValue = null) + { + $this->_bgColor = $pValue; + return $this; + } + /** * Get style type * diff --git a/src/PhpWord/Style/Row.php b/src/PhpWord/Style/Row.php index 1ecb044b..74b4d966 100644 --- a/src/PhpWord/Style/Row.php +++ b/src/PhpWord/Style/Row.php @@ -28,6 +28,13 @@ class Row */ private $_cantSplit = false; + /** + * Table row exact height + * + * @var bool + */ + private $_exactHeight = false; + /** * Create a new row style */ @@ -95,4 +102,29 @@ class Row { return $this->_cantSplit; } + + /** + * Set exactHeight + * + * @param bool $pValue + * @return PHPWord_Style_Row + */ + public function setExactHeight($pValue = false) + { + if (!is_bool($pValue)) { + $pValue = false; + } + $this->_exactHeight = $pValue; + return $this; + } + + /** + * Get exactHeight + * + * @return boolean + */ + public function getExactHeight() + { + return $this->_exactHeight; + } } diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index 98d0bcd1..fe275838 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -99,7 +99,7 @@ class ODText implements IWriter // If $pFilename is php://output or php://stdout, make it a temporary file... $originalFilename = $pFilename; if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - $pFilename = @tempnam('./', 'phppttmp'); + $pFilename = @tempnam(sys_get_temp_dir(), 'phpword_'); if ($pFilename == '') { $pFilename = $originalFilename; } diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 970d91ad..017c89c1 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -93,7 +93,7 @@ class RTF implements IWriter // If $pFilename is php://output or php://stdout, make it a temporary file... $originalFilename = $pFilename; if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - $pFilename = @tempnam('./', 'phppttmp'); + $pFilename = @tempnam(sys_get_temp_dir(), 'phpword_'); if ($pFilename == '') { $pFilename = $originalFilename; } diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 1652ceab..5b4f9f85 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -110,7 +110,7 @@ class Word2007 implements IWriter // If $pFilename is php://output or php://stdout, make it a temporary file... $originalFilename = $pFilename; if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - $pFilename = @tempnam('./', 'phppttmp'); + $pFilename = @tempnam(sys_get_temp_dir(), 'phpword_'); if ($pFilename == '') { $pFilename = $originalFilename; } diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 7234aed9..58e3eda1 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -413,6 +413,7 @@ class Base extends WriterPart $color = $style->getColor(); $size = $style->getSize(); $fgColor = $style->getFgColor(); + $bgColor = $style->getBgColor(); $strikethrough = $style->getStrikethrough(); $underline = $style->getUnderline(); $superscript = $style->getSuperScript(); @@ -483,6 +484,15 @@ class Base extends WriterPart $xmlWriter->endElement(); } + // Background-Color + if (!is_null($bgColor)) { + $xmlWriter->startElement('w:shd'); + $xmlWriter->writeAttribute('w:val', "clear"); + $xmlWriter->writeAttribute('w:color', "auto"); + $xmlWriter->writeAttribute('w:fill', $bgColor); + $xmlWriter->endElement(); + } + // Superscript/subscript if ($superscript || $subscript) { $xmlWriter->startElement('w:vertAlign'); @@ -572,8 +582,10 @@ class Base extends WriterPart $xmlWriter->startElement('w:tblGrid'); foreach ($cellWidths as $width) { $xmlWriter->startElement('w:gridCol'); - $xmlWriter->writeAttribute('w:w', $width); - $xmlWriter->writeAttribute('w:type', 'dxa'); + if (!is_null($width)) { + $xmlWriter->writeAttribute('w:w', $width); + $xmlWriter->writeAttribute('w:type', 'dxa'); + } $xmlWriter->endElement(); } $xmlWriter->endElement(); // w:tblGrid @@ -581,7 +593,7 @@ class Base extends WriterPart // Table style $tblStyle = $table->getStyle(); $tblWidth = $table->getWidth(); - if ($tblStyle instanceof PhpOffice\PhpWord\Style\Table) { + if ($tblStyle instanceof \PhpOffice\PhpWord\Style\Table) { $this->_writeTableStyle($xmlWriter, $tblStyle, false); } else { if (!empty($tblStyle)) { @@ -606,6 +618,7 @@ class Base extends WriterPart $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); $cantSplit = $rowStyle->getCantSplit(); + $exactHeight = $rowStyle->getExactHeight(); $xmlWriter->startElement('w:tr'); @@ -614,6 +627,7 @@ class Base extends WriterPart if (!is_null($height)) { $xmlWriter->startElement('w:trHeight'); $xmlWriter->writeAttribute('w:val', $height); + $xmlWriter->writeAttribute('w:hRule', ($exactHeight ? 'exact' : 'atLeast')); $xmlWriter->endElement(); } if ($tblHeader) { diff --git a/tests/PhpWord/Tests/Style/FontTest.php b/tests/PhpWord/Tests/Style/FontTest.php index 482bfe91..c5e0664b 100644 --- a/tests/PhpWord/Tests/Style/FontTest.php +++ b/tests/PhpWord/Tests/Style/FontTest.php @@ -55,6 +55,7 @@ class FontTest extends \PHPUnit_Framework_TestCase 'strikethrough' => false, 'color' => PhpWord::DEFAULT_FONT_COLOR, 'fgColor' => null, + 'bgColor' => null, 'hint' => PhpWord::DEFAULT_FONT_CONTENT_TYPE, ); foreach ($attributes as $key => $default) { @@ -83,7 +84,8 @@ class FontTest extends \PHPUnit_Framework_TestCase 'underline' => Font::UNDERLINE_HEAVY, 'strikethrough' => true, 'color' => '999999', - 'fgColor' => '999999', + 'fgColor' => Font::FGCOLOR_YELLOW, + 'bgColor' => 'FFFF00', 'hint' => 'eastAsia', ); $object->setArrayStyle($attributes); diff --git a/tests/PhpWord/Tests/Style/RowTest.php b/tests/PhpWord/Tests/Style/RowTest.php index daf27efb..fefb7b0a 100644 --- a/tests/PhpWord/Tests/Style/RowTest.php +++ b/tests/PhpWord/Tests/Style/RowTest.php @@ -29,6 +29,7 @@ class RowTest extends \PHPUnit_Framework_TestCase $properties = array( 'tblHeader' => true, 'cantSplit' => false, + 'exactHeight' => true, ); foreach ($properties as $key => $value) { // set/get @@ -56,6 +57,7 @@ class RowTest extends \PHPUnit_Framework_TestCase $properties = array( 'tblHeader' => 'a', 'cantSplit' => 'b', + 'exactHeight' => 'c', ); foreach ($properties as $key => $value) { $set = "set{$key}"; diff --git a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php index 68baeb2b..c5a430e4 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php @@ -206,6 +206,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $styles['superScript'] = true; $styles['color'] = 'FF0000'; $styles['fgColor'] = 'yellow'; + $styles['bgColor'] = 'FFFF00'; $styles['hint'] = 'eastAsia'; $section = $phpWord->createSection();