Example #1
0
 protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table)
 {
     $_rows = $table->getRows();
     $_cRows = count($_rows);
     if ($_cRows > 0) {
         $objWriter->startElement('w:tbl');
         $tblStyle = $table->getStyle();
         if ($tblStyle instanceof PHPWord_Style_Table) {
             $this->_writeTableStyle($objWriter, $tblStyle);
         } else {
             if (!empty($tblStyle)) {
                 $objWriter->startElement('w:tblPr');
                 $objWriter->startElement('w:tblStyle');
                 $objWriter->writeAttribute('w:val', $tblStyle);
                 $objWriter->endElement();
                 $objWriter->endElement();
             }
         }
         // Insert a tblGrid element so that Open/Libre office can size the
         // table width properly. Word can work out how to size the table
         // appropriately without this although it will add this element in if
         // it is not present following a save. Also, the scaling of widths in
         // this element can be switched on or off - this has no effect in Word
         // though - an oversized table will still be reduced to fit into the
         // available width.
         if (!is_null($table->getSectionSettings())) {
             $reduceTableWidthsToFit = $table->getSectionSettings()->getReduceTableWidthsToFit();
             $section_width = $table->getSectionSettings()->insideMarginsPageW();
             $objWriter->startElement('w:tblGrid');
             $cell_widths = array();
             for ($i = 0; $i < $_cRows; $i++) {
                 $row = $_rows[$i];
                 $table_width = 0;
                 foreach ($row as $cell) {
                     $table_width += $cell->getWidth();
                 }
                 $horizontal_offset = 0;
                 foreach ($row as $cell) {
                     $width = $cell->getWidth();
                     if ($table_width > $section_width && $reduceTableWidthsToFit) {
                         // Scale the table to fit in the section width if
                         // it is larger than the section width:
                         $width = $width * ($section_width / $table_width);
                     }
                     $cell_widths[$horizontal_offset + $width] = $width;
                     $horizontal_offset = $horizontal_offset + $width;
                 }
             }
             ksort($cell_widths);
             $current_horizontal_offset = 0;
             foreach ($cell_widths as $horizontal_offset => $cell_width) {
                 $objWriter->startElement('w:gridCol');
                 $column_width = $horizontal_offset - $current_horizontal_offset;
                 $current_horizontal_offset = $horizontal_offset;
                 $objWriter->writeAttribute('w:w', $column_width);
                 $objWriter->writeAttribute('w:type', 'dxa');
                 $objWriter->endElement();
             }
             $objWriter->endElement();
         }
         $_heights = $table->getRowHeights();
         for ($i = 0; $i < $_cRows; $i++) {
             $row = $_rows[$i];
             $height = $_heights[$i];
             $objWriter->startElement('w:tr');
             if (!is_null($height)) {
                 $objWriter->startElement('w:trPr');
                 $objWriter->startElement('w:trHeight');
                 $objWriter->writeAttribute('w:val', $height);
                 $objWriter->endElement();
                 $objWriter->endElement();
             }
             foreach ($row as $cell) {
                 $objWriter->startElement('w:tc');
                 $cellStyle = $cell->getStyle();
                 $width = $cell->getWidth();
                 $objWriter->startElement('w:tcPr');
                 $objWriter->startElement('w:tcW');
                 $objWriter->writeAttribute('w:w', $width);
                 $objWriter->writeAttribute('w:type', 'dxa');
                 $objWriter->endElement();
                 if ($cellStyle instanceof PHPWord_Style_Cell) {
                     $this->_writeCellStyle($objWriter, $cellStyle);
                 }
                 $objWriter->endElement();
                 $_elements = $cell->getElements();
                 if (count($_elements) > 0) {
                     foreach ($_elements as $element) {
                         if ($element instanceof PHPWord_Section_Text) {
                             $this->_writeText($objWriter, $element);
                         } elseif ($element instanceof PHPWord_Section_TextRun) {
                             $this->_writeTextRun($objWriter, $element);
                         } elseif ($element instanceof PHPWord_Section_Link) {
                             $this->_writeLink($objWriter, $element);
                         } elseif ($element instanceof PHPWord_Section_TextBreak) {
                             $this->_writeTextBreak($objWriter);
                         } elseif ($element instanceof PHPWord_Section_ListItem) {
                             $this->_writeListItem($objWriter, $element);
                         } elseif ($element instanceof PHPWord_Section_Image || $element instanceof PHPWord_Section_MemoryImage) {
                             $this->_writeImage($objWriter, $element);
                         } elseif ($element instanceof PHPWord_Section_Object) {
                             $this->_writeObject($objWriter, $element);
                         } elseif ($element instanceof PHPWord_Section_Footer_PreserveText) {
                             $this->_writePreserveText($objWriter, $element);
                         }
                     }
                 } else {
                     $this->_writeTextBreak($objWriter);
                 }
                 $objWriter->endElement();
             }
             $objWriter->endElement();
         }
         $objWriter->endElement();
     }
 }