From 9240376eb3fc9dc94a8556a27c3e43af2b74edf0 Mon Sep 17 00:00:00 2001 From: SRG Group Date: Tue, 23 Feb 2016 10:03:55 +0100 Subject: [PATCH 1/5] Adding setParagraphStyle to Textrun for indentation --- src/PhpWord/Element/TextRun.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index d4e90608dc..81fbe2cf59 100644 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -55,4 +55,26 @@ public function getParagraphStyle() { 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; + } } From 21b2224c7da4e61e53d1a8852686db90f981436a Mon Sep 17 00:00:00 2001 From: srggroup Date: Tue, 23 Feb 2016 12:29:09 +0100 Subject: [PATCH 2/5] Html Image support added --- src/PhpWord/Shared/Html.php | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 64bcab1eb1..3aa80cde00 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -128,6 +128,7 @@ protected static function parseNode($node, $element, $styles = array(), $data = 'ul' => array('List', null, null, $styles, $data, 3, null), 'ol' => array('List', null, null, $styles, $data, 7, null), 'li' => array('ListItem', $node, $element, $styles, $data, null, null), + 'img' => array('Image', $node, $element, $styles, $data, null, null), ); $newElement = null; @@ -374,4 +375,64 @@ private static function parseStyle($attribute, $styles) return $styles; } + + + + + /** + * Parse image node + * + * @param \DOMNode $node + * @param \PhpOffice\PhpWord\Element\AbstractContainer $element + * @param array &$styles + * @return \PhpOffice\PhpWord\Element\TextRun + * + **/ + private static function parseImage($node, $element, &$styles, $data) + { + $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; + } } From f52e29df540c121d1492d9fe4f43a0881f5973c2 Mon Sep 17 00:00:00 2001 From: srggroup Date: Sat, 5 Nov 2016 11:17:55 +0100 Subject: [PATCH 3/5] Fix DocBlock of parseImage function --- src/PhpWord/Shared/Html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 3aa80cde00..026c868ba4 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -385,7 +385,7 @@ private static function parseStyle($attribute, $styles) * @param \DOMNode $node * @param \PhpOffice\PhpWord\Element\AbstractContainer $element * @param array &$styles - * @return \PhpOffice\PhpWord\Element\TextRun + * @return \PhpOffice\PhpWord\Element\Image * **/ private static function parseImage($node, $element, &$styles, $data) From 7acea1293d7588b4083cb1a6a528d86f19bdc4e5 Mon Sep 17 00:00:00 2001 From: srggroup Date: Sat, 5 Nov 2016 12:11:34 +0100 Subject: [PATCH 4/5] Make test for TextRun --- tests/PhpWord/Element/TextRunTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/PhpWord/Element/TextRunTest.php b/tests/PhpWord/Element/TextRunTest.php index bdfa4bf53c..dd8df975f4 100644 --- a/tests/PhpWord/Element/TextRunTest.php +++ b/tests/PhpWord/Element/TextRunTest.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\SimpleType\Jc; /** * Test class for PhpOffice\PhpWord\Element\TextRun @@ -152,4 +153,16 @@ public function testCreateFootnote() $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element); $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()); + } } From 5955fd895507c908ccc9a905bf57795463eb3884 Mon Sep 17 00:00:00 2001 From: troosan Date: Wed, 20 Dec 2017 23:31:15 +0100 Subject: [PATCH 5/5] fix formatting, add tests & update changelog --- CHANGELOG.md | 3 +++ src/PhpWord/Element/TextRun.php | 8 ++++---- src/PhpWord/Shared/Html.php | 6 ++---- tests/PhpWord/Element/ListItemRunTest.php | 4 ++-- tests/PhpWord/Element/TextRunTest.php | 24 +++++++++++++++++++---- tests/PhpWord/Shared/HtmlTest.php | 19 ++++++++++++++++++ 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51f9243484..ae1618a2ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ This is the last version to support PHP 5.3 - Implement PageBreak for odt writer @cookiekiller #863 #824 - Allow to force an update of all fields on opening a document - @troosan #951 - 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 - 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 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 error on Empty Dropdown Entry - @ComputerTinker #592 ### Deprecated - PhpWord->getProtection(), get it from the settings instead PhpWord->getSettings()->getDocumentProtection(); diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index 5aa0e2d7e5..6d9ae9f4a1 100644 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -43,7 +43,7 @@ class TextRun extends AbstractContainer */ public function __construct($paragraphStyle = null) { - $this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle); + $this->paragraphStyle = $this->setParagraphStyle($paragraphStyle); } /** @@ -55,7 +55,7 @@ public function getParagraphStyle() { return $this->paragraphStyle; } - + /** * Set Paragraph style * @@ -65,12 +65,12 @@ public function getParagraphStyle() public function setParagraphStyle($style = null) { if (is_array($style)) { - $this->paragraphStyle = new Paragraph; + $this->paragraphStyle = new Paragraph(); $this->paragraphStyle->setStyleByArray($style); } elseif ($style instanceof Paragraph) { $this->paragraphStyle = $style; } elseif (null === $style) { - $this->paragraphStyle = new Paragraph; + $this->paragraphStyle = new Paragraph(); } else { $this->paragraphStyle = $style; } diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 514708d1de..8310e515c2 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -136,7 +136,7 @@ protected static function parseNode($node, $element, $styles = array(), $data = 'ul' => array('List', null, null, $styles, $data, 3, null), 'ol' => array('List', null, null, $styles, $data, 7, null), 'li' => array('ListItem', $node, $element, $styles, $data, null, null), - 'img' => array('Image', $node, $element, $styles, $data, null, null), + 'img' => array('Image', $node, $element, $styles, null, null, null), 'br' => array('LineBreak', null, $element, $styles, null, null, null), ); @@ -512,12 +512,10 @@ private static function parseStyle($attribute, $styles) * * @param \DOMNode $node * @param \PhpOffice\PhpWord\Element\AbstractContainer $element - * @param array &$styles - * @param array $data * * @return \PhpOffice\PhpWord\Element\Image **/ - private static function parseImage($node, $element, &$styles, $data) + private static function parseImage($node, $element) { $style = array(); foreach ($node->attributes as $attribute) { diff --git a/tests/PhpWord/Element/ListItemRunTest.php b/tests/PhpWord/Element/ListItemRunTest.php index 999756ba0e..84beec02e4 100644 --- a/tests/PhpWord/Element/ListItemRunTest.php +++ b/tests/PhpWord/Element/ListItemRunTest.php @@ -27,13 +27,13 @@ class ListItemRunTest extends \PHPUnit\Framework\TestCase /** * New instance */ - public function testConstructNull() + public function testConstruct() { $oListItemRun = new ListItemRun(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun); $this->assertCount(0, $oListItemRun->getElements()); - $this->assertNull($oListItemRun->getParagraphStyle()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oListItemRun->getParagraphStyle()); } /** diff --git a/tests/PhpWord/Element/TextRunTest.php b/tests/PhpWord/Element/TextRunTest.php index 35228fb77c..59b8b89fdb 100644 --- a/tests/PhpWord/Element/TextRunTest.php +++ b/tests/PhpWord/Element/TextRunTest.php @@ -19,6 +19,7 @@ use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\SimpleType\Jc; +use PhpOffice\PhpWord\Style\Paragraph; /** * Test class for PhpOffice\PhpWord\Element\TextRun @@ -30,13 +31,13 @@ class TextRunTest extends \PHPUnit\Framework\TestCase /** * New instance */ - public function testConstructNull() + public function testConstruct() { $oTextRun = new TextRun(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun); $this->assertCount(0, $oTextRun->getElements()); - $this->assertNull($oTextRun->getParagraphStyle()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle()); } /** @@ -63,6 +64,21 @@ public function testConstructArray() $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 */ @@ -153,7 +169,7 @@ public function testCreateFootnote() $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element); $this->assertCount(1, $oTextRun->getElements()); } - + /** * Get paragraph style */ @@ -161,7 +177,7 @@ 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()); } diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index bfe24c58a9..d168c09e9b 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -252,4 +252,23 @@ public function testParseLineBreak() $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); } + + public function testParseImage() + { + $src = __DIR__ . '/../_files/images/firefox.png'; + + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + $html = '

'; + 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')); + } }