This project is read-only.

styles for table - how to set background

Jul 31, 2012 at 12:39 PM
Edited Aug 1, 2012 at 12:42 PM

Hi, i am struggling with styling a table.

Is there a way to set table background and width (100%) ?


EDIT: i found the tag for setting background, in i added 'bgColor' => '666666', to 'td' field.

But the width problem still remains. how to set table width to fit the page width?

Aug 9, 2012 at 9:14 AM

Hi tesiadynamics

You can't currently set the width of the table to fit the page width as this is not a setting that is available in PHPWord (see the PHPWord documentation in PHPWord_Docs_0.6.2.docx - in the phpword directory) - you can only set the widths of individual cells. However, this could be added as a feature to future versions of the HTML to docx converter, probably something like this:

* Add a setting to define a width (in TWIPs or px) for a fully sized table (or we can get this information from the pageSizeW property);

* Pick up percentage widths set by a CSS class or inline style;

* Use the above two to compute the width that each cell needs to be (also taking into account border widths) in order to scale the width of the table correctly.

Feel free to suggest some code (as a patch if possible) and/or add this to the issue tracker.


Aug 9, 2012 at 9:27 AM

Thanks for reply. When i tried the width setting i failed.

I analized the xml code generated by script and saved by word. And the made this modification:


protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) {
		$_rows = $table->getRows();
		$_cRows = count($_rows);
		if($_cRows > 0) {
				$tblStyle = $table->getStyle();
				if($tblStyle instanceof PHPWord_Style_Table) {
					$this->_writeTableStyle($objWriter, $tblStyle);
				} else {
					if(!empty($tblStyle)) {
								$objWriter->writeAttribute('w:val', $tblStyle);

// width part start //
								$objWriter->writeAttribute('w:w', '11350');
								$objWriter->writeAttribute('w:type', 'dxa');
// width part end //

So i added tblW tag with a fixed (dxa) width. 11350 is about 100% width, while i'm using my personal margins.