- Comment header('Content-Length:'): HTTP header should not be in class that can be called via CLI
- Incorporate temporary location to ODT and RTF
- Update documentation
- Add unit tests
This commit is contained in:
Ivan Lanin 2014-03-29 22:53:34 +07:00
commit 01f3f4071e
13 changed files with 173 additions and 21 deletions

View File

@ -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

View File

@ -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
~~~~~~~~~

View File

@ -0,0 +1,23 @@
<?php
include_once 'Sample_Header.php';
// New Word document
echo date('H:i:s'), " Create new PhpWord object", \EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->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';

View File

@ -0,0 +1,40 @@
<?php
include_once 'Sample_Header.php';
// New Word document
echo date('H:i:s'), " Create new PhpWord object", \EOL;
$phpWord = new \PhpOffice\PhpWord\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" => 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';

View File

@ -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
*

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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');
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) {

View File

@ -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);

View File

@ -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}";

View File

@ -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();