Skip to content

Commit 3e6745f

Browse files
adambalint-srgtroosan
authored andcommitted
HTML image support & TextRun paragraph style (#934)
* Adding setParagraphStyle to Textrun for indentation * Html Image support added * fix formatting, add tests & update changelog
1 parent 200c2f1 commit 3e6745f

File tree

6 files changed

+136
-5
lines changed

6 files changed

+136
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ This is the last version to support PHP 5.3
2424
- Implement PageBreak for odt writer @cookiekiller #863 #824
2525
- Allow to force an update of all fields on opening a document - @troosan #951
2626
- Allow adding a CheckBox in a TextRun - @irond #727
27+
- Add support for HTML img tag - @srggroup #934
28+
- Add support for password protection for docx - @mariahaubner #1019
2729

2830
### Fixed
2931
- Loosen dependency to Zend
@@ -43,6 +45,7 @@ This is the last version to support PHP 5.3
4345
- Fix incorrect image size between windows and mac - @bskrtich #874
4446
- Fix adding HTML table to document - @mogilvie @arivanbastos #324
4547
- Fix parsing on/off values (w:val="true|false|1|0|on|off") - @troosan #1221 #1219
48+
- Fix error on Empty Dropdown Entry - @ComputerTinker #592
4649

4750
### Deprecated
4851
- PhpWord->getProtection(), get it from the settings instead PhpWord->getSettings()->getDocumentProtection();

src/PhpWord/Element/TextRun.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TextRun extends AbstractContainer
4343
*/
4444
public function __construct($paragraphStyle = null)
4545
{
46-
$this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);
46+
$this->paragraphStyle = $this->setParagraphStyle($paragraphStyle);
4747
}
4848

4949
/**
@@ -55,4 +55,26 @@ public function getParagraphStyle()
5555
{
5656
return $this->paragraphStyle;
5757
}
58+
59+
/**
60+
* Set Paragraph style
61+
*
62+
* @param string|array|\PhpOffice\PhpWord\Style\Paragraph $style
63+
* @return string|\PhpOffice\PhpWord\Style\Paragraph
64+
*/
65+
public function setParagraphStyle($style = null)
66+
{
67+
if (is_array($style)) {
68+
$this->paragraphStyle = new Paragraph();
69+
$this->paragraphStyle->setStyleByArray($style);
70+
} elseif ($style instanceof Paragraph) {
71+
$this->paragraphStyle = $style;
72+
} elseif (null === $style) {
73+
$this->paragraphStyle = new Paragraph();
74+
} else {
75+
$this->paragraphStyle = $style;
76+
}
77+
78+
return $this->paragraphStyle;
79+
}
5880
}

src/PhpWord/Shared/Html.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ protected static function parseNode($node, $element, $styles = array(), $data =
136136
'ul' => array('List', null, null, $styles, $data, 3, null),
137137
'ol' => array('List', null, null, $styles, $data, 7, null),
138138
'li' => array('ListItem', $node, $element, $styles, $data, null, null),
139+
'img' => array('Image', $node, $element, $styles, null, null, null),
139140
'br' => array('LineBreak', null, $element, $styles, null, null, null),
140141
);
141142

@@ -506,6 +507,63 @@ private static function parseStyle($attribute, $styles)
506507
return $styles;
507508
}
508509

510+
/**
511+
* Parse image node
512+
*
513+
* @param \DOMNode $node
514+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
515+
*
516+
* @return \PhpOffice\PhpWord\Element\Image
517+
**/
518+
private static function parseImage($node, $element)
519+
{
520+
$style = array();
521+
foreach ($node->attributes as $attribute) {
522+
switch ($attribute->name) {
523+
case 'src':
524+
$src = $attribute->value;
525+
break;
526+
case 'width':
527+
$width = $attribute->value;
528+
$style['width'] = $width;
529+
break;
530+
case 'height':
531+
$height = $attribute->value;
532+
$style['height'] = $height;
533+
break;
534+
case 'style':
535+
$styleattr = explode(';', $attribute->value);
536+
foreach ($styleattr as $attr) {
537+
if (strpos($attr, ':')) {
538+
list($k, $v) = explode(':', $attr);
539+
switch ($k) {
540+
case 'float':
541+
if (trim($v) == 'right') {
542+
$style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_RIGHT;
543+
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE;
544+
$style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
545+
$style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
546+
$style['overlap'] = true;
547+
}
548+
if (trim($v) == 'left') {
549+
$style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_LEFT;
550+
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE;
551+
$style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
552+
$style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
553+
$style['overlap'] = true;
554+
}
555+
break;
556+
}
557+
}
558+
}
559+
break;
560+
}
561+
}
562+
$newElement = $element->addImage($src, $style);
563+
564+
return $newElement;
565+
}
566+
509567
/**
510568
* Transforms a CSS border style into a word border style
511569
*

tests/PhpWord/Element/ListItemRunTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class ListItemRunTest extends \PHPUnit\Framework\TestCase
2727
/**
2828
* New instance
2929
*/
30-
public function testConstructNull()
30+
public function testConstruct()
3131
{
3232
$oListItemRun = new ListItemRun();
3333

3434
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun);
3535
$this->assertCount(0, $oListItemRun->getElements());
36-
$this->assertNull($oListItemRun->getParagraphStyle());
36+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oListItemRun->getParagraphStyle());
3737
}
3838

3939
/**

tests/PhpWord/Element/TextRunTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
namespace PhpOffice\PhpWord\Element;
1919

2020
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWord\SimpleType\Jc;
22+
use PhpOffice\PhpWord\Style\Paragraph;
2123

2224
/**
2325
* Test class for PhpOffice\PhpWord\Element\TextRun
@@ -29,13 +31,13 @@ class TextRunTest extends \PHPUnit\Framework\TestCase
2931
/**
3032
* New instance
3133
*/
32-
public function testConstructNull()
34+
public function testConstruct()
3335
{
3436
$oTextRun = new TextRun();
3537

3638
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
3739
$this->assertCount(0, $oTextRun->getElements());
38-
$this->assertNull($oTextRun->getParagraphStyle());
40+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
3941
}
4042

4143
/**
@@ -62,6 +64,21 @@ public function testConstructArray()
6264
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
6365
}
6466

67+
/**
68+
* New instance with object
69+
*/
70+
public function testConstructObject()
71+
{
72+
$oParagraphStyle = new Paragraph();
73+
$oParagraphStyle->setAlignment(Jc::BOTH);
74+
$oTextRun = new TextRun($oParagraphStyle);
75+
76+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
77+
$this->assertCount(0, $oTextRun->getElements());
78+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
79+
$this->assertEquals(Jc::BOTH, $oTextRun->getParagraphStyle()->getAlignment());
80+
}
81+
6582
/**
6683
* Add text
6784
*/
@@ -152,4 +169,16 @@ public function testCreateFootnote()
152169
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element);
153170
$this->assertCount(1, $oTextRun->getElements());
154171
}
172+
173+
/**
174+
* Get paragraph style
175+
*/
176+
public function testParagraph()
177+
{
178+
$oText = new TextRun('paragraphStyle');
179+
$this->assertEquals('paragraphStyle', $oText->getParagraphStyle());
180+
181+
$oText->setParagraphStyle(array('alignment' => Jc::CENTER, 'spaceAfter' => 100));
182+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oText->getParagraphStyle());
183+
}
155184
}

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,23 @@ public function testParseLineBreak()
252252
$this->assertEquals('This is some text', $doc->getElement('/w:document/w:body/w:p/w:r[1]/w:t')->nodeValue);
253253
$this->assertEquals('with a linebreak.', $doc->getElement('/w:document/w:body/w:p/w:r[2]/w:t')->nodeValue);
254254
}
255+
256+
public function testParseImage()
257+
{
258+
$src = __DIR__ . '/../_files/images/firefox.png';
259+
260+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
261+
$section = $phpWord->addSection();
262+
$html = '<p><img src="' . $src . '" width="150" height="200" style="float: right;"/><img src="' . $src . '" style="float: left;"/></p>';
263+
Html::addHtml($section, $html);
264+
265+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
266+
267+
$baseXpath = '/w:document/w:body/w:p/w:r';
268+
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
269+
$this->assertStringMatchesFormat('%Swidth:150pt%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
270+
$this->assertStringMatchesFormat('%Sheight:200pt%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
271+
$this->assertStringMatchesFormat('%Smso-position-horizontal:right%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
272+
$this->assertStringMatchesFormat('%Smso-position-horizontal:left%S', $doc->getElementAttribute($baseXpath . '[2]/w:pict/v:shape', 'style'));
273+
}
255274
}

0 commit comments

Comments
 (0)