image in header problem

Jul 24, 2012 at 12:38 PM

I did a huge research on docx baking, and this script is the only one quite promising.

yet, everything is satisfactory except header part. 

has anyone else deal with this problem: inserting image to top header (on every page) ?

I tried many xml and php tricks but no positive result...

Jul 25, 2012 at 5:37 AM

That's how I did:
        $header = $section->createHeader();
        $fontStyleTitle = array('size' => 11);
        $paragraphStyleTitle = array('spaceBefore' => 0);
        $styleTable = array('borderSize'=>0);
        $table = $header->addTable($styleTable);
        $table->addRow();
        $logo = 'absolute/path/to/image.png';
        $table->addCell(1000)->addImage($logo, array('align' => 'left'));
        $table->addCell(1000);
        $table->addCell(1000)->addText('Report number :', $fontStyleTitle, array_merge($paragraphStyleTitle, array('align' => 'right')));
        $header->addText('MY HEADER TITLE', $fontStyleTitle, $paragraphStyleTitle);

So, from a cell, use the method addImage

Jul 25, 2012 at 7:33 AM

Thanks for your input. But I already tried such combinations, and my problem remains..

After generating the docx file, if i open it with Libre office (which is mainly used at my workplace) the image is Not in the header. If i open it in word 2007 i get error about corrupted file and only after recovery (or smth) word opens it in compatibility mode, although the image is in header. 

So, your solution kind of works, but not in Libre office case. I guess i'm stuck at this point with no possible solution, due to crapy free office.

Jul 25, 2012 at 7:42 AM

what is your code?

Jul 25, 2012 at 7:51 AM

pretty much the same as you offered.

I even found a newer version of phpword at github: https://github.com/PHPOffice/PHPWord

there is even an example of picture in a header (which does not work for me):

 

<?php
require_once '../PHPWord.php';

// New Word Document
$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWord->createSection();

// Add header
$header = $section->createHeader();
$table = $header->addTable();
$table->addRow();
$table->addCell(4500)->addText('This is the header.');
$table->addCell(4500)->addImage('../logo.jpg', array('width'=>50, 'height'=>50, 'align'=>'right'));

// Add footer
$footer = $section->createFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align'=>'center'));

// Write some text
$section->addTextBreak();
$section->addText('Some text...');

// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('HeaderFooter.docx');
?>

Aug 29, 2012 at 3:52 PM

try to input the ABSOLUTE path to the file!

$table->addCell(4500)->addImage('/path/to/images/logo.jpg', array('width'=>50, 'height'=>50, 'align'=>'right'));

Aug 30, 2012 at 6:37 AM

I tried, but did not work.

I have altered the Base.php, changing the XML code for image processing. and now images work everywhere and on both MS word and libre office.

 

Thanks for trying :)

 

Although i now have a new problem :D 

the <br /> tag gives double lines (margin-bottom:10pt as i looked inside the docx via ms word). i wonder is there a way to start a new line without such a huge space between lines?

Nov 12, 2012 at 2:56 PM

Could you tell me what have you altered in Base.php ? I have the same problem :)

Nov 13, 2012 at 1:00 PM

I altered pretty much, but when talking about images, i used instead of 'pict' other type. i got an example by extracting the docx file and analizing xml (the part image). and i done it right after generating the file and again, after editing it (to look good) and re-saving it in Libre office. then i saw that different image xml standard was used, which works on both libre and Ms offices.

here is the code:

protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $image) {
		$rId = $image->getRelationId();		
		$style = $image->getStyle();
		$width = $style->getWidth();
		$height = $style->getHeight();
		$align = $style->getAlign();
		$position = $style->getPosition();

		$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;position:'.$position);
						
						$objWriter->startElement('v:imagedata');
							$objWriter->writeAttribute('r:id', 'rId'.$rId);
							$objWriter->writeAttribute('o:title', '');
						$objWriter->endElement();
					$objWriter->endElement();
					
				$objWriter->endElement();*/
				
			
			
				$objWriter->startElement('w:drawing');
					$objWriter->startElement('wp:inline');
						$objWriter->writeAttribute('distT', 0);
						$objWriter->writeAttribute('distB', 0);
						$objWriter->writeAttribute('distL', 0);
						$objWriter->writeAttribute('distR', 0);
						
						
						
						// new start //
						$objWriter->startElement('wp:extent');
							$objWriter->writeAttribute('cx', $width*9525);
							$objWriter->writeAttribute('cy', $height*9525);
						$objWriter->endElement();
						
						$objWriter->startElement('wp:effectExtent');
							$objWriter->writeAttribute('l', 0); // 19050 // untested
							$objWriter->writeAttribute('t', 0);
							$objWriter->writeAttribute('r', 0);
							$objWriter->writeAttribute('b', 0);
						$objWriter->endElement();
						
						$objWriter->startElement('wp:docPr');
							$objWriter->writeAttribute('id', $rId);
							$objWriter->writeAttribute('name', 'Picture '.$rId);
						$objWriter->endElement();
						
						$objWriter->startElement('wp:cNvGraphicFramePr');
							$objWriter->startElement('a:graphicFrameLocks');
								$objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
								$objWriter->writeAttribute('noChangeAspect', 1);
							$objWriter->endElement();
						$objWriter->endElement();
						// new end //
						
						
						
						
						$objWriter->startElement('a:graphic');
							$objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
							
							$objWriter->startElement('a:graphicData');
								$objWriter->writeAttribute('uri', 'http://schemas.openxmlformats.org/drawingml/2006/picture');
							
								$objWriter->startElement('pic:pic');
									$objWriter->writeAttribute('xmlns:pic', 'http://schemas.openxmlformats.org/drawingml/2006/picture');
									
									
									// new start //
									$objWriter->startElement('pic:nvPicPr');									
										$objWriter->startElement('pic:cNvPr');
											$objWriter->writeAttribute('id', $rId); // 0  // untested
											$objWriter->writeAttribute('name', 'Picture 1');
										$objWriter->endElement();										
										$objWriter->startElement('pic:cNvPicPr');
											$objWriter->startElement('a:picLocks');
												$objWriter->writeAttribute('noChangeAspect', 1);
												$objWriter->writeAttribute('noChangeArrowheads', 1);
											$objWriter->endElement();											
										$objWriter->endElement();
									$objWriter->endElement();	
									// new end //							
									
									
									
									$objWriter->startElement('pic:blipFill');
										$objWriter->startElement('a:blip');
											$objWriter->writeAttribute('r:embed', 'rId'.$rId);
											
											// new start //
											$objWriter->startElement('a:srcRect');
											$objWriter->endElement();
											$objWriter->startElement('a:stretch');
												$objWriter->startElement('a:fillRect');
												$objWriter->endElement();
											$objWriter->endElement();	
											// new end //
																					
										$objWriter->endElement();
									$objWriter->endElement();
									
									$objWriter->startElement('pic:spPr');
										$objWriter->writeAttribute('bwMode', 'auto');
										
										$objWriter->startElement('a:xfrm');
											$objWriter->startElement('a:off');
												$objWriter->writeAttribute('x', 0);
												$objWriter->writeAttribute('y', 0);
											$objWriter->endElement();
											
											$objWriter->startElement('a:ext');
												$objWriter->writeAttribute('cx', $width*9525);
												$objWriter->writeAttribute('cy', $height*9525);
											$objWriter->endElement();											
										$objWriter->endElement();
										
										// new start //
										$objWriter->startElement('a:prstGeom');
											$objWriter->writeAttribute('prst', 'rect');
											$objWriter->startElement('a:avLst');
											$objWriter->endElement();
										$objWriter->endElement();
										
										$objWriter->startElement('a:noFill');
										$objWriter->endElement();	
																			
										$objWriter->startElement('a:ln');
											$objWriter->writeAttribute('w', 9525);
											$objWriter->startElement('a:noFill');
											$objWriter->endElement();
											$objWriter->startElement('a:miter');
												$objWriter->writeAttribute('lim', 800000);
											$objWriter->endElement();
											$objWriter->startElement('a:headEnd');
											$objWriter->endElement();
											$objWriter->startElement('a:tailEnd');
											$objWriter->endElement();
										$objWriter->endElement();										
										// new end //
										
										
										
									$objWriter->endElement();
								$objWriter->endElement();
							$objWriter->endElement();
							
						$objWriter->endElement();
						
					$objWriter->endElement();
				$objWriter->endElement();
			
			$objWriter->endElement();
			
		$objWriter->endElement();
	}