Height header

Feb 15, 2012 at 5:05 PM

If I insert a header and generate a docx, the header takes space in the top of the document. The header has a certain height. To much. How can I make it less height?

Feb 16, 2012 at 3:55 PM
Edited Feb 16, 2012 at 4:19 PM

I discover header & footer height values were 'hard coded' (at 720 twips or ~ .5") into Writer > Word2007 > Document.php

I have some code that allows you to set header & footer values instead. Let me know if this works.

I will link this possible solution to a ticket: http://phpword.codeplex.com/workitem/35

In Section > Settings.php, add variables for header & footer values above __construct function


// add header and foot height variables
	private $_headerHeight;
	private $_footerHeight;


And in Section > Settings.php, modify __construct function with default header & footer values


public function __construct() {
		$this->_orientation = null;
		$this->_marginTop = 1418;
		$this->_marginLeft = 1418;
		$this->_marginRight	= 1418;
		$this->_marginBottom = 1134;
		$this->_pageSizeW = $this->_defaultPageSizeW;
		$this->_pageSizeH = $this->_defaultPageSizeH;
		$this->_borderTopSize = null;
		$this->_borderTopColor = null;
		$this->_borderLeftSize = null;
		$this->_borderLeftColor = null;
		$this->_borderRightSize = null;
		$this->_borderRightColor = null;
		$this->_borderBottomSize = null;
		$this->_borderBottomColor = null;
		$this->_headerHeight = 720; // set default header and footer to 720 twips (.5 inches)
		$this->_footerHeight = 720;


And in Section > Settings.php, add getter & setter functions for header & footer below __construct function


	 * Get Header Height
	 * @return int
	public function getHeaderHeight() {
		return $this->_headerHeight;

	 * Set Header Height
	 * @param int $pValue
	public function setHeaderHeight($pValue = '') {
		$this->_headerHeight = $pValue;
		return $this;

	 * Get Footer Height
	 * @return int
	public function getFooterHeight() {
		return $this->_footerHeight;

	 * Set Footer Height
	 * @param int $pValue
	public function setFooterHeight($pValue = '') {
		$this->_footerHeight = $pValue;
		return $this;


In Writer > Word2007 > Document.php


private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) {
		$_settings = $section->getSettings();
		$_header = $section->getHeader();
		$_footer = $section->getFooter();
		$pgSzW = $_settings->getPageSizeW();
		$pgSzH = $_settings->getPageSizeH();
		$orientation = $_settings->getOrientation();

		$marginTop = $_settings->getMarginTop();
		$marginLeft = $_settings->getMarginLeft();
		$marginRight = $_settings->getMarginRight();
		$marginBottom = $_settings->getMarginBottom();

// add ability to configure header and footer height
		$headerHeight = $_settings->getHeaderHeight();
		$footerHeight = $_settings->getFooterHeight();

		$borders = $_settings->getBorderSize();


			if(!is_null($_header)) {
				$rId = $_header->getRelationId();
					$objWriter->writeAttribute('w:type', 'default');
					$objWriter->writeAttribute('r:id', 'rId'.$rId);

			if(!is_null($_footer)) {
				$rId = $_footer->getRelationId();
					$objWriter->writeAttribute('w:type', 'default');
					$objWriter->writeAttribute('r:id', 'rId'.$rId);

				$objWriter->writeAttribute('w:w', $pgSzW);
				$objWriter->writeAttribute('w:h', $pgSzH);

				if(!is_null($orientation) && strtolower($orientation) != 'portrait') {
					$objWriter->writeAttribute('w:orient', $orientation);


				$objWriter->writeAttribute('w:top', $marginTop);
				$objWriter->writeAttribute('w:right', $marginRight);
				$objWriter->writeAttribute('w:bottom', $marginBottom);
				$objWriter->writeAttribute('w:left', $marginLeft);
// add ability to configure height of header and footer
				$objWriter->writeAttribute('w:header', $headerHeight);
				$objWriter->writeAttribute('w:footer', $footerHeight);
				//$objWriter->writeAttribute('w:header', '720'); //units of measurement: twips. 720 twips = .5 inches
				//$objWriter->writeAttribute('w:footer', '720');

				$objWriter->writeAttribute('w:gutter', '0');

			if(!is_null($borders[0]) || !is_null($borders[1]) || !is_null($borders[2]) || !is_null($borders[3])) {
				$borderColor = $_settings->getBorderColor();

					$objWriter->writeAttribute('w:offsetFrom', 'page');

					if(!is_null($borders[0])) {
							$objWriter->writeAttribute('w:val', 'single');
							$objWriter->writeAttribute('w:sz', $borders[0]);
							$objWriter->writeAttribute('w:space', '24');
							$objWriter->writeAttribute('w:color', $borderColor[0]);

					if(!is_null($borders[1])) {
							$objWriter->writeAttribute('w:val', 'single');
							$objWriter->writeAttribute('w:sz', $borders[1]);
							$objWriter->writeAttribute('w:space', '24');
							$objWriter->writeAttribute('w:color', $borderColor[1]);

					if(!is_null($borders[2])) {
							$objWriter->writeAttribute('w:val', 'single');
							$objWriter->writeAttribute('w:sz', $borders[2]);
							$objWriter->writeAttribute('w:space', '24');
							$objWriter->writeAttribute('w:color', $borderColor[2]);

					if(!is_null($borders[3])) {
							$objWriter->writeAttribute('w:val', 'single');
							$objWriter->writeAttribute('w:sz', $borders[3]);
							$objWriter->writeAttribute('w:space', '24');
							$objWriter->writeAttribute('w:color', $borderColor[3]);

				$objWriter->writeAttribute('w:space', '720');



And last, use new header & footer feature like this. This makes a very small 'badge' for printing.


$section = $PHPWord->createSection(array('orientation'=>null, 'marginLeft'=>210, 'marginRight'=>200, 'marginTop'=>620, 'marginBottom'=>0,
                                      'headerHeight'=>0, 'footerHeight'=>0, 'pageSizeW'=>3038.4, 'pageSizeH'=>4847.04));
Feb 16, 2012 at 6:16 PM

Thanks a lot!

I'm not used to make/change classes, so I did change the hard coded value 720 to 0 for test purpose. And did work, almost: the header had less height, but it could be more: Word puts a return in the header, I don't know why. Do you also know how to change that?

How is the procedure to put the code in a next patch/release PHPWord? I'm afraid my client will download in the future a new version of PHPWord and if I make changes in the code of PHPWord, the application won't work after installing the new version of PHPWord...

Feb 16, 2012 at 6:27 PM

I don't know about 'return' in header? I did not have that problem. My modified header had no height as specified.

How to get changes into PHPWord? raw_venture_ad is the coordinator for the project.

I know that sometimes ideas/patches posted in the discussion board make it into releases, and I hope that is what will happen here.There have been several reasonable patches posted that have NOT yet been added, so I think maybe the project is not very active right now. I wish I had more time to help patch it, as I think it is a good project.

As for 'client' upgrading PHPWord without your patches, you need to get stick out and smack client hand. Or at least explain danger of doing it without talking to you first.

It is not difficult to modify PHPWord files. Open with text editor and look at code. You will see where to put the changes above. What is the worst that can happen? Good luck!

Feb 18, 2012 at 2:14 PM

I have include your code in the php's, for the first time I worked with classes. And succesfull, it generate's a good docx!

(I work with NetBeans. I do programma since 1986, most Basic, since 2004 with php, but I always make function, never classes.)

The 'return in the header' is a effect after adding a watermark in the header. I did search several classes why he do that, could not found it. But it's not a big problem, so I do not search any further.

I have a other problem, a big one. My client has told my it's a must have: add colums in a page. I will start a new discussion for the problem, today of tomorrow. I'm bussy with searching, maybe I'm a fast learning person and can I add some code to the classes of PHPWord myself :-)


Feb 18, 2012 at 4:52 PM

Hello Jillelain,

I did manage it (the cols), see new discussion.

Jul 27, 2012 at 5:31 AM

works like a charm!

thank you!