HTML image support & TextRun paragraph style (#934)
* Adding setParagraphStyle to Textrun for indentation * Html Image support added * fix formatting, add tests & update changelog
This commit is contained in:
parent
200c2f1eb0
commit
3e6745f146
|
|
@ -24,6 +24,8 @@ This is the last version to support PHP 5.3
|
||||||
- Implement PageBreak for odt writer @cookiekiller #863 #824
|
- Implement PageBreak for odt writer @cookiekiller #863 #824
|
||||||
- Allow to force an update of all fields on opening a document - @troosan #951
|
- Allow to force an update of all fields on opening a document - @troosan #951
|
||||||
- Allow adding a CheckBox in a TextRun - @irond #727
|
- Allow adding a CheckBox in a TextRun - @irond #727
|
||||||
|
- Add support for HTML img tag - @srggroup #934
|
||||||
|
- Add support for password protection for docx - @mariahaubner #1019
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- Loosen dependency to Zend
|
- Loosen dependency to Zend
|
||||||
|
|
@ -43,6 +45,7 @@ This is the last version to support PHP 5.3
|
||||||
- Fix incorrect image size between windows and mac - @bskrtich #874
|
- Fix incorrect image size between windows and mac - @bskrtich #874
|
||||||
- Fix adding HTML table to document - @mogilvie @arivanbastos #324
|
- Fix adding HTML table to document - @mogilvie @arivanbastos #324
|
||||||
- Fix parsing on/off values (w:val="true|false|1|0|on|off") - @troosan #1221 #1219
|
- Fix parsing on/off values (w:val="true|false|1|0|on|off") - @troosan #1221 #1219
|
||||||
|
- Fix error on Empty Dropdown Entry - @ComputerTinker #592
|
||||||
|
|
||||||
### Deprecated
|
### Deprecated
|
||||||
- PhpWord->getProtection(), get it from the settings instead PhpWord->getSettings()->getDocumentProtection();
|
- PhpWord->getProtection(), get it from the settings instead PhpWord->getSettings()->getDocumentProtection();
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ class TextRun extends AbstractContainer
|
||||||
*/
|
*/
|
||||||
public function __construct($paragraphStyle = null)
|
public function __construct($paragraphStyle = null)
|
||||||
{
|
{
|
||||||
$this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);
|
$this->paragraphStyle = $this->setParagraphStyle($paragraphStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -55,4 +55,26 @@ class TextRun extends AbstractContainer
|
||||||
{
|
{
|
||||||
return $this->paragraphStyle;
|
return $this->paragraphStyle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Paragraph style
|
||||||
|
*
|
||||||
|
* @param string|array|\PhpOffice\PhpWord\Style\Paragraph $style
|
||||||
|
* @return string|\PhpOffice\PhpWord\Style\Paragraph
|
||||||
|
*/
|
||||||
|
public function setParagraphStyle($style = null)
|
||||||
|
{
|
||||||
|
if (is_array($style)) {
|
||||||
|
$this->paragraphStyle = new Paragraph();
|
||||||
|
$this->paragraphStyle->setStyleByArray($style);
|
||||||
|
} elseif ($style instanceof Paragraph) {
|
||||||
|
$this->paragraphStyle = $style;
|
||||||
|
} elseif (null === $style) {
|
||||||
|
$this->paragraphStyle = new Paragraph();
|
||||||
|
} else {
|
||||||
|
$this->paragraphStyle = $style;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->paragraphStyle;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,6 +136,7 @@ class Html
|
||||||
'ul' => array('List', null, null, $styles, $data, 3, null),
|
'ul' => array('List', null, null, $styles, $data, 3, null),
|
||||||
'ol' => array('List', null, null, $styles, $data, 7, null),
|
'ol' => array('List', null, null, $styles, $data, 7, null),
|
||||||
'li' => array('ListItem', $node, $element, $styles, $data, null, null),
|
'li' => array('ListItem', $node, $element, $styles, $data, null, null),
|
||||||
|
'img' => array('Image', $node, $element, $styles, null, null, null),
|
||||||
'br' => array('LineBreak', null, $element, $styles, null, null, null),
|
'br' => array('LineBreak', null, $element, $styles, null, null, null),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -506,6 +507,63 @@ class Html
|
||||||
return $styles;
|
return $styles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse image node
|
||||||
|
*
|
||||||
|
* @param \DOMNode $node
|
||||||
|
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
|
||||||
|
*
|
||||||
|
* @return \PhpOffice\PhpWord\Element\Image
|
||||||
|
**/
|
||||||
|
private static function parseImage($node, $element)
|
||||||
|
{
|
||||||
|
$style = array();
|
||||||
|
foreach ($node->attributes as $attribute) {
|
||||||
|
switch ($attribute->name) {
|
||||||
|
case 'src':
|
||||||
|
$src = $attribute->value;
|
||||||
|
break;
|
||||||
|
case 'width':
|
||||||
|
$width = $attribute->value;
|
||||||
|
$style['width'] = $width;
|
||||||
|
break;
|
||||||
|
case 'height':
|
||||||
|
$height = $attribute->value;
|
||||||
|
$style['height'] = $height;
|
||||||
|
break;
|
||||||
|
case 'style':
|
||||||
|
$styleattr = explode(';', $attribute->value);
|
||||||
|
foreach ($styleattr as $attr) {
|
||||||
|
if (strpos($attr, ':')) {
|
||||||
|
list($k, $v) = explode(':', $attr);
|
||||||
|
switch ($k) {
|
||||||
|
case 'float':
|
||||||
|
if (trim($v) == 'right') {
|
||||||
|
$style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_RIGHT;
|
||||||
|
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE;
|
||||||
|
$style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
|
||||||
|
$style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
|
||||||
|
$style['overlap'] = true;
|
||||||
|
}
|
||||||
|
if (trim($v) == 'left') {
|
||||||
|
$style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_LEFT;
|
||||||
|
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE;
|
||||||
|
$style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
|
||||||
|
$style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
|
||||||
|
$style['overlap'] = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$newElement = $element->addImage($src, $style);
|
||||||
|
|
||||||
|
return $newElement;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms a CSS border style into a word border style
|
* Transforms a CSS border style into a word border style
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,13 @@ class ListItemRunTest extends \PHPUnit\Framework\TestCase
|
||||||
/**
|
/**
|
||||||
* New instance
|
* New instance
|
||||||
*/
|
*/
|
||||||
public function testConstructNull()
|
public function testConstruct()
|
||||||
{
|
{
|
||||||
$oListItemRun = new ListItemRun();
|
$oListItemRun = new ListItemRun();
|
||||||
|
|
||||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun);
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun);
|
||||||
$this->assertCount(0, $oListItemRun->getElements());
|
$this->assertCount(0, $oListItemRun->getElements());
|
||||||
$this->assertNull($oListItemRun->getParagraphStyle());
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oListItemRun->getParagraphStyle());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@
|
||||||
namespace PhpOffice\PhpWord\Element;
|
namespace PhpOffice\PhpWord\Element;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\SimpleType\Jc;
|
||||||
|
use PhpOffice\PhpWord\Style\Paragraph;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test class for PhpOffice\PhpWord\Element\TextRun
|
* Test class for PhpOffice\PhpWord\Element\TextRun
|
||||||
|
|
@ -29,13 +31,13 @@ class TextRunTest extends \PHPUnit\Framework\TestCase
|
||||||
/**
|
/**
|
||||||
* New instance
|
* New instance
|
||||||
*/
|
*/
|
||||||
public function testConstructNull()
|
public function testConstruct()
|
||||||
{
|
{
|
||||||
$oTextRun = new TextRun();
|
$oTextRun = new TextRun();
|
||||||
|
|
||||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
|
||||||
$this->assertCount(0, $oTextRun->getElements());
|
$this->assertCount(0, $oTextRun->getElements());
|
||||||
$this->assertNull($oTextRun->getParagraphStyle());
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -62,6 +64,21 @@ class TextRunTest extends \PHPUnit\Framework\TestCase
|
||||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* New instance with object
|
||||||
|
*/
|
||||||
|
public function testConstructObject()
|
||||||
|
{
|
||||||
|
$oParagraphStyle = new Paragraph();
|
||||||
|
$oParagraphStyle->setAlignment(Jc::BOTH);
|
||||||
|
$oTextRun = new TextRun($oParagraphStyle);
|
||||||
|
|
||||||
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
|
||||||
|
$this->assertCount(0, $oTextRun->getElements());
|
||||||
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
|
||||||
|
$this->assertEquals(Jc::BOTH, $oTextRun->getParagraphStyle()->getAlignment());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add text
|
* Add text
|
||||||
*/
|
*/
|
||||||
|
|
@ -152,4 +169,16 @@ class TextRunTest extends \PHPUnit\Framework\TestCase
|
||||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element);
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element);
|
||||||
$this->assertCount(1, $oTextRun->getElements());
|
$this->assertCount(1, $oTextRun->getElements());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get paragraph style
|
||||||
|
*/
|
||||||
|
public function testParagraph()
|
||||||
|
{
|
||||||
|
$oText = new TextRun('paragraphStyle');
|
||||||
|
$this->assertEquals('paragraphStyle', $oText->getParagraphStyle());
|
||||||
|
|
||||||
|
$oText->setParagraphStyle(array('alignment' => Jc::CENTER, 'spaceAfter' => 100));
|
||||||
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oText->getParagraphStyle());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -252,4 +252,23 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
|
||||||
$this->assertEquals('This is some text', $doc->getElement('/w:document/w:body/w:p/w:r[1]/w:t')->nodeValue);
|
$this->assertEquals('This is some text', $doc->getElement('/w:document/w:body/w:p/w:r[1]/w:t')->nodeValue);
|
||||||
$this->assertEquals('with a linebreak.', $doc->getElement('/w:document/w:body/w:p/w:r[2]/w:t')->nodeValue);
|
$this->assertEquals('with a linebreak.', $doc->getElement('/w:document/w:body/w:p/w:r[2]/w:t')->nodeValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testParseImage()
|
||||||
|
{
|
||||||
|
$src = __DIR__ . '/../_files/images/firefox.png';
|
||||||
|
|
||||||
|
$phpWord = new \PhpOffice\PhpWord\PhpWord();
|
||||||
|
$section = $phpWord->addSection();
|
||||||
|
$html = '<p><img src="' . $src . '" width="150" height="200" style="float: right;"/><img src="' . $src . '" style="float: left;"/></p>';
|
||||||
|
Html::addHtml($section, $html);
|
||||||
|
|
||||||
|
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
|
||||||
|
|
||||||
|
$baseXpath = '/w:document/w:body/w:p/w:r';
|
||||||
|
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
|
||||||
|
$this->assertStringMatchesFormat('%Swidth:150pt%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
|
||||||
|
$this->assertStringMatchesFormat('%Sheight:200pt%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
|
||||||
|
$this->assertStringMatchesFormat('%Smso-position-horizontal:right%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
|
||||||
|
$this->assertStringMatchesFormat('%Smso-position-horizontal:left%S', $doc->getElementAttribute($baseXpath . '[2]/w:pict/v:shape', 'style'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue