Line breaks after image.

Oct 28, 2010 at 5:17 PM

Hello, I've to generate a page with many badge as possible (they are 7,2cm by  10,3cm), and I used (in a foreach loop) :

$imageStyle = array('width'=>272, 'height'=>387);

$section->addImage("logo/".$item['id'].".jpg", $imageStyle);

Then, I generate the docx.

But after each image there is a line break... So I've 1 badge per page :(

Is there a possibility to bypass these line breaks ?

Thank you.



Oct 28, 2010 at 7:11 PM
Edited Oct 28, 2010 at 7:12 PM

I wish for this feature too. To complicate matters, I need to insert an image and add text after it (without a line break after the image) inside a table cell.

I tried to use the 'createTextRun' function to see if I could add an image without a trailing line break, but it didn't work.

See NON-working code below:

$statusCell = $table->addCell(1300, $cellStyle);
$statusCellTextRun = $statusCell->createTextRun();
$statusCellTextRun->addText('my text');

See this thread for a related discussion:

Sep 14, 2011 at 6:31 PM

do you have any solution?

Sep 14, 2011 at 7:38 PM

Sorry but I haven't found any solution, I have used another solution for my badges (in generating directly a PDF).

Good luck !

Dec 8, 2011 at 10:09 PM
Edited Dec 8, 2011 at 10:09 PM

same problem here

Jan 3, 2012 at 10:25 PM

I got around the problem by using a table with cells - ie image in the first cell and then text in the next cell. Not the prettiest but worked.

Jan 11, 2012 at 1:56 PM
Edited Jan 11, 2012 at 2:50 PM

I've got a working solution. It's pretty much untested so use it with caution. If you can add to this in any way then please do!

Add the following function to /Section/TextRun.php


public function addImage($src, $style = null) {
	$image = new PHPWord_Section_Image($src, $style);
	if(!is_null($image->getSource())) {
		$rID = PHPWord_Media::addSectionMediaElement($src, 'image');
		$this->_elementCollection[] = $image;
		return $image;
	} else {
		trigger_error('Source does not exist or unsupported image type.');

Then modify the _writeTextRun function in /Writer/Word2007/Base.php as follows


protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun) {
	$elements = $textrun->getElements();
	$styleParagraph = $textrun->getParagraphStyle();
	$SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;
	if($SpIsObject) {
		$this->_writeParagraphStyle($objWriter, $styleParagraph);
	} elseif(!$SpIsObject && !is_null($styleParagraph)) {
				$objWriter->writeAttribute('w:val', $styleParagraph);
	if(count($elements) > 0) {
		foreach($elements as $element) {
			if($element instanceof PHPWord_Section_Text) {
				$this->_writeText($objWriter, $element, true);
			} elseif($element instanceof PHPWord_Section_Link) {
				$this->_writeLink($objWriter, $element, true);
			else if($element instanceof PHPWord_Section_Image) {
				$this->_writeImage($objWriter, $element,true);


Finally, update the _writeImage function in the same class with the following:


	protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $image, $withoutP = false) {
		$rId = $image->getRelationId();
		$style = $image->getStyle();
		$width = $style->getWidth();
		$height = $style->getHeight();
		$align = $style->getAlign();
		if(!$withoutP) {
			if(!is_null($align)) {
						$objWriter->writeAttribute('w:val', $align);
						$objWriter->writeAttribute('type', '#_x0000_t75');
						$objWriter->writeAttribute('style', 'width:'.$width.'px;height:'.$height.'px');
							$objWriter->writeAttribute('r:id', 'rId'.$rId);
							$objWriter->writeAttribute('o:title', '');
		if(!$withoutP) {


You should then be able to use




To add an image into a text run without any trailing line breaks.

Like I said, this is pretty much untested and I don't know what it breaks - so use at your own risk!

Mar 21, 2012 at 7:59 AM

Thank you Nathan.

I've followed your instructions and got it working :).

Still... I want to set the image do. I tried using the v:shapetype element and played around with all of the v:shape & v:imagedata attributes with no success.

Does anyone know how to change the image layout?



Aug 20, 2012 at 8:48 AM

Good, NathanGaskin, thanks very much!!!

Aug 20, 2014 at 7:56 AM
The above solution works fine for section. How can I use it for header as when I use it in header, the doc gets corrupt. Please help !!!