diff --git a/Classes/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php index 2aa6cbc6d3..66d8283213 100755 --- a/Classes/PHPWord/Style/Font.php +++ b/Classes/PHPWord/Style/Font.php @@ -149,6 +149,18 @@ class PHPWord_Style_Font * @var string */ private $_fgColor = null; + + /** + * Background color + * + * @var string + */ + private $_bgColor = null; + /** + * Text line height + * + * @var int + */ /** * Text line height @@ -468,6 +480,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/Style/Row.php b/Classes/PHPWord/Style/Row.php index da039d84b5..d779d3cc1d 100644 --- a/Classes/PHPWord/Style/Row.php +++ b/Classes/PHPWord/Style/Row.php @@ -45,6 +45,13 @@ class PHPWord_Style_Row */ private $_cantSplit = false; + /** + * Table row exact height + * + * @var bool + */ + private $_exactHeight = false; + /** * Create a new row style */ @@ -112,4 +119,29 @@ public function getCantSplit() { 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/Classes/PHPWord/Writer/Word2007.php b/Classes/PHPWord/Writer/Word2007.php index 16d66c666f..2589e9efd7 100755 --- a/Classes/PHPWord/Writer/Word2007.php +++ b/Classes/PHPWord/Writer/Word2007.php @@ -113,7 +113,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; } @@ -236,6 +236,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."); } diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index d829bf9299..1cf3bad394 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -398,6 +398,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(); @@ -467,6 +468,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) { @@ -563,6 +573,7 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); $cantSplit = $rowStyle->getCantSplit(); + $exactHeight = $rowStyle->getExactHeight(); $objWriter->startElement('w:tr'); @@ -570,6 +581,11 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo $objWriter->startElement('w:trPr'); if (!is_null($height)) { $objWriter->startElement('w:trHeight'); + if($exactHeight) { + $objWriter->startAttribute('w:hRule'); + $objWriter->text("exact"); + $objWriter->endAttribute(); + } $objWriter->writeAttribute('w:val', $height); $objWriter->endElement(); } diff --git a/samples/Sample_20_BGColor.php b/samples/Sample_20_BGColor.php new file mode 100644 index 0000000000..1122ba3d8a --- /dev/null +++ b/samples/Sample_20_BGColor.php @@ -0,0 +1,30 @@ +'); +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")); + + +// 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..1082f99f86 --- /dev/null +++ b/samples/Sample_21_TableRowRules.php @@ -0,0 +1,46 @@ +'); +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, 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"=>)); +$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; + $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;