Skip to content

added "bgColor" fontStyle, "exactHeight" tableRowStyle and minor modifs for php://output #168

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

Merged
merged 8 commits into from
Mar 29, 2014
Merged
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
34 changes: 34 additions & 0 deletions Classes/PHPWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions Classes/PHPWord/Style/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion Classes/PHPWord/Writer/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.");
}
Expand Down
16 changes: 16 additions & 0 deletions Classes/PHPWord/Writer/Word2007/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -563,13 +573,19 @@ 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');

if (!is_null($height) || !is_null($tblHeader) || !is_null($cantSplit)) {
$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();
}
Expand Down
30 changes: 30 additions & 0 deletions samples/Sample_20_BGColor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
// Init
error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
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;
46 changes: 46 additions & 0 deletions samples/Sample_21_TableRowRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
// Init
error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
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;