Line breaks after image.

Oct 28, 2010 at 6: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.

--

Clement

Oct 28, 2010 at 8:11 PM
Edited Oct 28, 2010 at 8: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->addImage('myimage.jpg');
$statusCellTextRun->addText('my text');

See this thread for a related discussion:

http://phpword.codeplex.com/Thread/View.aspx?ThreadId=210852

Sep 14, 2011 at 7:31 PM

do you have any solution?

Sep 14, 2011 at 8: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 11:09 PM
Edited Dec 8, 2011 at 11:09 PM

same problem here

Jan 3, 2012 at 11: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 2:56 PM
Edited Jan 11, 2012 at 3: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');
		$image->setRelationId($rID);
		
		$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;
	
	$objWriter->startElement('w:p');
	
	
	if($SpIsObject) {
		$this->_writeParagraphStyle($objWriter, $styleParagraph);
	} elseif(!$SpIsObject && !is_null($styleParagraph)) {
		$objWriter->startElement('w:pPr');
			$objWriter->startElement('w:pStyle');
				$objWriter->writeAttribute('w:val', $styleParagraph);
			$objWriter->endElement();
		$objWriter->endElement();
	}
	
	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);
			}
		}
	}
	
	$objWriter->endElement();
}

 

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) {
			$objWriter->startElement('w:p');
		
			if(!is_null($align)) {
				$objWriter->startElement('w:pPr');
					$objWriter->startElement('w:jc');
						$objWriter->writeAttribute('w:val', $align);
					$objWriter->endElement();
				$objWriter->endElement();
			}
		}
		
			$objWriter->startElement('w:r');
			
				$objWriter->startElement('w:pict');
					
					$objWriter->startElement('v:shape');
						$objWriter->writeAttribute('type', '#_x0000_t75');
						$objWriter->writeAttribute('style', 'width:'.$width.'px;height:'.$height.'px');
						
						$objWriter->startElement('v:imagedata');
							$objWriter->writeAttribute('r:id', 'rId'.$rId);
							$objWriter->writeAttribute('o:title', '');
						$objWriter->endElement();
					$objWriter->endElement();
					
				$objWriter->endElement();
				
			$objWriter->endElement();
			
		if(!$withoutP) {
			$objWriter->endElement();
		}
	}

 

You should then be able to use

 

$textRun->addImage($my_image);

 

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 8: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?

Thanks!

 

Aug 20, 2012 at 9:48 AM

Good, NathanGaskin, thanks very much!!!

Aug 20, 2014 at 8:56 AM
Hi
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 !!!