From 324e15a3beaca85e0200d06d4b116e80709112e9 Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Tue, 18 Mar 2014 11:06:13 -0400 Subject: [PATCH 1/5] Font-style addition: bgColor - Meant to replace fgColor, can now use any hex color instead of the 15 constants FGCOLOR_xyz in Font.php - Preserve spaces after text fixed (Base.php line 78) Signed-off-by: Julien Carignan --- Classes/PHPWord/Style/Font.php | 30 +++++++++++++++++++++++- Classes/PHPWord/Writer/Word2007/Base.php | 11 +++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Classes/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php index f26f392468..90186597ae 100755 --- a/Classes/PHPWord/Style/Font.php +++ b/Classes/PHPWord/Style/Font.php @@ -149,7 +149,13 @@ class PHPWord_Style_Font * @var string */ private $_fgColor = null; - + + /** + * Background color + * + * @var string + */ + private $_bgColor = null; /** * Text line height * @@ -466,6 +472,28 @@ public function setFgColor($pValue = null) $this->_fgColor = $pValue; 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/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index e9249120e5..bdb95441f6 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -75,6 +75,7 @@ protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWor $objWriter->startElement('w:t'); $objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text + $strText = str_replace(" ", " ", $strText);// line above only preserves spaces before text, this one keeps spaces after text $objWriter->writeRaw($strText); $objWriter->endElement(); @@ -395,6 +396,7 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P $color = $style->getColor(); $size = $style->getSize(); $fgColor = $style->getFgColor(); + $bgColor = $style->getBgColor(); $strikethrough = $style->getStrikethrough(); $underline = $style->getUnderline(); $superscript = $style->getSuperScript(); @@ -464,6 +466,15 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P $objWriter->writeAttribute('w:val', $fgColor); $objWriter->endElement(); } + + // Background-Color + if (!is_null($bgColor)) { + $objWriter->startElement('w:shd'); + $objWriter->writeAttribute('w:val', "clear"); + $objWriter->writeAttribute('w:color', "auto"); + $objWriter->writeAttribute('w:fill', $bgColor); + $objWriter->endElement(); + } // Superscript/subscript if ($superscript || $subscript) { From bcdb9c0ccf3a1b20a82c24119f6bf0bc78477446 Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Tue, 18 Mar 2014 11:32:03 -0400 Subject: [PATCH 2/5] When saving as "php://output", we want its content-length. Tmp file should go into sys_get_temp_dir() instead of "./" Signed-off-by: Julien Carignan --- Classes/PHPWord/Writer/Word2007.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Classes/PHPWord/Writer/Word2007.php b/Classes/PHPWord/Writer/Word2007.php index 5089036372..3462c0a8dc 100755 --- a/Classes/PHPWord/Writer/Word2007.php +++ b/Classes/PHPWord/Writer/Word2007.php @@ -70,7 +70,7 @@ public function save($pFilename = null) // 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_');// temp files should go to system temp directory (if a user cancels a download, the file stays) if ($pFilename == '') { $pFilename = $originalFilename; } @@ -193,6 +193,7 @@ public function save($pFilename = null) // If a temporary file was used, copy it to the correct file stream if ($originalFilename != $pFilename) { + header('Content-Length: '.filesize($pFilename));// if php://output, we want to know the total file size when downloading if (copy($pFilename, $originalFilename) === false) { throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename."); } From 3b1013ed3100406c625b87e9b03d5ef057e7b583 Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Tue, 18 Mar 2014 12:01:12 -0400 Subject: [PATCH 3/5] Added height rules for table row, mostly for removing the added space after a table - Code mostly come from this discussion: https://phpword.codeplex.com/discussions/440933 - Adapted and works for 0.8.1 - Usage: $table->addRow(5000, $rowStyle, "exact"); Signed-off-by: Julien Carignan --- Classes/PHPWord/Section/Table.php | 4 ++-- Classes/PHPWord/Section/Table/Row.php | 20 +++++++++++++++++++- Classes/PHPWord/Writer/Word2007/Base.php | 6 ++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Classes/PHPWord/Section/Table.php b/Classes/PHPWord/Section/Table.php index f5a8a79900..e35d267d54 100755 --- a/Classes/PHPWord/Section/Table.php +++ b/Classes/PHPWord/Section/Table.php @@ -100,9 +100,9 @@ public function __construct($insideOf, $pCount, $style = null) * * @param int $height */ - public function addRow($height = null, $style = null) + public function addRow($height = null, $style = null, $hRules = null) { - $row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style); + $row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style, $hRules); $this->_rows[] = $row; return $row; } diff --git a/Classes/PHPWord/Section/Table/Row.php b/Classes/PHPWord/Section/Table/Row.php index dd8ea65c12..d4219075eb 100644 --- a/Classes/PHPWord/Section/Table/Row.php +++ b/Classes/PHPWord/Section/Table/Row.php @@ -37,6 +37,13 @@ class PHPWord_Section_Table_Row * @var int */ private $_height = null; + + /** + * Row heightRules + * + * @var array + */ + private $_heightRules = array(); /** * Row style @@ -75,11 +82,12 @@ class PHPWord_Section_Table_Row * @param int $height * @param mixed $style */ - public function __construct($insideOf, $pCount, $height = null, $style = null) + public function __construct($insideOf, $pCount, $height = null, $style = null, $hRules = null) { $this->_insideOf = $insideOf; $this->_pCount = $pCount; $this->_height = $height; + $this->_heightRules = $hRules; $this->_style = new PHPWord_Style_Row(); if (!is_null($style)) { @@ -138,4 +146,14 @@ public function getHeight() { return $this->_height; } + + /** + * Get all row height rules + * + * @return array + */ + public function getHeightRules() + { + return $this->_heightRules; + } } diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index bdb95441f6..90037211fb 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -568,6 +568,7 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo for ($i = 0; $i < $_cRows; $i++) { $row = $_rows[$i]; $height = $row->getHeight(); + $heightRules = $row->getHeightRules(); $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); $cantSplit = $rowStyle->getCantSplit(); @@ -578,6 +579,11 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo $objWriter->startElement('w:trPr'); if (!is_null($height)) { $objWriter->startElement('w:trHeight'); + if(!is_null($heightRules)) { + $objWriter->startAttribute('w:hRule'); + $objWriter->text($heightRules); + $objWriter->endAttribute(); + } $objWriter->writeAttribute('w:val', $height); $objWriter->endElement(); } From 5dd7b47fad22ea58ceb22e2b3c3e5dd9790b6206 Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Tue, 18 Mar 2014 12:13:21 -0400 Subject: [PATCH 4/5] remove first line break on each page Signed-off-by: Julien Carignan --- Classes/PHPWord/_staticDocParts/settings.xml | 88 +++++++++----------- 1 file changed, 41 insertions(+), 47 deletions(-) diff --git a/Classes/PHPWord/_staticDocParts/settings.xml b/Classes/PHPWord/_staticDocParts/settings.xml index 5eb2289137..894df55147 100644 --- a/Classes/PHPWord/_staticDocParts/settings.xml +++ b/Classes/PHPWord/_staticDocParts/settings.xml @@ -1,48 +1,42 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 27c5b04b395d4698ed22f075d85428502954354b Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Tue, 18 Mar 2014 15:30:28 -0400 Subject: [PATCH 5/5] added samples associated with pull request #145 Signed-off-by: Julien Carignan --- samples/Sample_20_BGColor.php | 31 ++++++++++++++++++++ samples/Sample_21_TableRowRules.php | 44 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 samples/Sample_20_BGColor.php create mode 100644 samples/Sample_21_TableRowRules.php diff --git a/samples/Sample_20_BGColor.php b/samples/Sample_20_BGColor.php new file mode 100644 index 0000000000..75d9a68586 --- /dev/null +++ b/samples/Sample_20_BGColor.php @@ -0,0 +1,31 @@ +'); +require_once '../Classes/PHPWord.php'; + +// New Word Document +echo date('H:i:s') , ' Create new PHPWord object' , EOL; +$PHPWord = new PHPWord(); +$section = $PHPWord->createSection(); + +$section->addText("This is some text highlighted using fgColor (limited to 15 colors)", array("fgColor" => 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")); +$section->addText(" Can highlight space before and after words ", 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; + $objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer); + $objWriter->save("{$name}.{$extension}"); + rename("{$name}.{$extension}", "results/{$name}.{$extension}"); +} + + +// Done +echo date('H:i:s'), " Done writing file(s)", EOL; +echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL; diff --git a/samples/Sample_21_TableRowRules.php b/samples/Sample_21_TableRowRules.php new file mode 100644 index 0000000000..83d03e0c05 --- /dev/null +++ b/samples/Sample_21_TableRowRules.php @@ -0,0 +1,44 @@ +'); +require_once '../Classes/PHPWord.php'; + +// New Word Document +echo date('H:i:s') , ' Create new PHPWord object' , EOL; +$PHPWord = new PHPWord(); +$section = $PHPWord->createSection(); + +$section->addText("By default, a table row adds a textbreak after its content (notice the red border), even if the row height is <= height of the content"); + +$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 row rule \"exact\", we get rid of the textbreak!"); + +$table2 = $section->addTable(array("cellMargin"=> 0, "cellMarginRight"=> 0, "cellMarginBottom"=> 0, "cellMarginLeft"=> 0)); +$table2->addRow(3750, null, "exact"); +$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, null, 'exact');"); + +// 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; + $objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer); + $objWriter->save("{$name}.{$extension}"); + rename("{$name}.{$extension}", "results/{$name}.{$extension}"); +} + + +// Done +echo date('H:i:s'), " Done writing file(s)", EOL; +echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;