4

I have template.docx template with mark ${table} inside. I need to create table using phpWord and insert it in my template.docx instead ${table} mark inside. Here is my code example

//Create simple table
$document_with_table = new PhpWord();
$section = $document_with_table->addSection();
$table = $section->addTable();
for ($r = 1; $r <= 8; $r++) {
    $table->addRow();
    for ($c = 1; $c <= 5; $c++) {
        $table->addCell(1750)->addText("Row {$r}, Cell {$c}");
    }
}

//Open template with ${table}
$template_document = new \PhpOffice\PhpWord\TemplateProcessor('template.docx');
// some code to replace ${table} with table from $document_with_table
// ???


//save template with table
$template_document->saveAs('template_with_table.docx');

First, I create table in separate variable $document_with_table using new PhpWord instance. Next I load my template.docx in $template_document variable. And now I need to insert table from $document_with_table to $template_document instead ${table} mark inside. How can I do that?

PhpWord version - latest stable (0.16.0)

Chris
  • 4,672
  • 13
  • 52
  • 93
Teretto
  • 635
  • 2
  • 10
  • 22

3 Answers3

9

PhpWord has another solution, which better for me.

    use PhpOffice\PhpWord\Element\Table;
    use PhpOffice\PhpWord\TemplateProcessor;

    $table = new Table(array('unit' => TblWidth::TWIP));
    foreach ($details as $detail) {
        $table->addRow();
        $table->addCell(700)->addText($detail->column);
        $table->addCell(500)->addText(1);
    }
    $phpWord = new TemplateProcessor('template.docx');
    $phpWord->setComplexBlock('{table}', $table);
    $phpWord->saveAs('template_with_table.docx');
8

You can get xml code of your table and insert it insite template

//Create table
$document_with_table = new PhpWord();
$section = $document_with_table->addSection();
$table = $section->addTable();
for ($r = 1; $r <= 8; $r++) {
    $table->addRow();
    for ($c = 1; $c <= 5; $c++) {
        $table->addCell(1750)->addText("Row {$r}, Cell {$c}");
    }
}

// Create writer to convert document to xml
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($document_with_table, 'Word2007');

// Get all document xml code
$fullxml = $objWriter->getWriterPart('Document')->write();

// Get only table xml code
$tablexml = preg_replace('/^[\s\S]*(<w:tbl\b.*<\/w:tbl>).*/', '$1', $fullxml);

//Open template with ${table}
$template_document = new \PhpOffice\PhpWord\TemplateProcessor('template.docx');

// Replace mark by xml code of table
$template_document->setValue('table', $tablexml);

//save template with table
$template_document->saveAs('template_with_table.docx');
autumnrustle
  • 595
  • 1
  • 10
  • 21
  • 2
    This one helped me a lot. One thing i want to add is, that to get it working correctly i had to add '' in front and '' after the $tablexml else the content was present in the document.xml but was not visible. – Sebastian Fischer Nov 15 '19 at 11:49
  • @autumnrustle Hey there, this code works pretty good, but I am having an issue where my table loses its styling and formatting using this step. Is there some way to prevent this? – Mihail Minkov Apr 14 '21 at 15:36
  • @MihailMinkov I have no any ideas how to fix it – autumnrustle Apr 14 '21 at 19:21
  • This helped . but how to add section text ? – KHALID Feb 18 '23 at 19:48
0

The problem is that you do not have access to create table functions.

I just used the following code. Obviously you need the parameter: ${serial_numbers_table} in your table. I just randomly extracted the properties when I saved a word document with similar table as .xml.

// First add table and its properties like grid column
$snums_tbl = 
      '<w:tbl><w:tblPr><w:tblStyle w:val="Grilledutableau"/>' 
    . '<w:tblW w:w="0" w:type="auto"/>'
    . '<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0"'
      . ' w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid>'
        . '<w:gridCol w:w="4675"/><w:gridCol w:w="4675"/></w:tblGrid>' ; 

// Header row
$snums_tbl .= 
  '<w:tr>'
    . '<w:tc><w:tcPr><w:tcW w:w="4675" w:type="dxa"/></w:tcPr>'
        . '<w:p><w:t>Equipment Serial Number</w:t></w:p></w:tc>'
    . '<w:tc><w:tcPr><w:tcW w:w="4675" w:type="dxa"/></w:tcPr>'
        . '<w:p><w:t>Applied Attesta Label Number</w:t></w:p></w:tc>'
  . '</w:tr>' ;

foreach ($serial_nums as $serial_num)
{
  $snums_tbl .= 
    '<w:tr>'
      . '<w:tc><w:tcPr><w:tcW w:w="4675" w:type="dxa"/></w:tcPr>'
          . '<w:p><w:t>' . $serial_num -> serialNumber . '</w:t></w:p></w:tc>'
      . '<w:tc><w:tcPr><w:tcW w:w="4675" w:type="dxa"/></w:tcPr>'
          . '<w:p><w:t>' . $serial_num -> LabelNumber . '</w:t></w:p></w:tc>'
    . '</w:tr>' ;
} 
$snums_tbl .= '</w:tbl>' ;

$tmpl -> setValue("serial_numbers_table",$snums_tbl) ;
James Risner
  • 5,451
  • 11
  • 25
  • 47
Reza
  • 1
  • 1