Skip to content

HTML image support & TextRun paragraph style #934

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 6 commits into from
Dec 20, 2017
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
24 changes: 23 additions & 1 deletion src/PhpWord/Element/TextRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
}
58 changes: 58 additions & 0 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +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, null, null, null),
'br' => array('LineBreak', null, $element, $styles, null, null, null),
);

Expand Down Expand Up @@ -506,6 +507,63 @@ private static function parseStyle($attribute, $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
*
Expand Down
4 changes: 2 additions & 2 deletions tests/PhpWord/Element/ListItemRunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
33 changes: 31 additions & 2 deletions tests/PhpWord/Element/TextRunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\Paragraph;

/**
* Test class for PhpOffice\PhpWord\Element\TextRun
Expand All @@ -29,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());
}

/**
Expand All @@ -62,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
*/
Expand Down Expand Up @@ -152,4 +169,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());
}
}
19 changes: 19 additions & 0 deletions tests/PhpWord/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<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'));
}
}