Phpword: Replace or add an table on a template

Created on 10 Jun 2014  路  6Comments  路  Source: PHPOffice/PHPWord

how can i add/ replace template from array or from sql select to word template value? is possible to add this feature ?


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Change Request Consulting Request Template Processor

Most helpful comment

Hey Guys,
I would really appreciate this feature too. In the meanwhile, I use a little (and of course really dirty) workaround. This would be to add a new public function to the Writer-Part Document Class as follows:

  public function getTableAsText($element) {
    $xmlWriter = $this->getXmlWriter();
    $writer = new \PhpOffice\PhpWord\Writer\Word2007\Element\Table($xmlWriter, $element);
    $writer->write();
    return $xmlWriter->getData();
  }

Using this, you can create a phpWord-Object as usual, create a new table $table and insert it like that:

// create new writer
$objWriter = new PhpOffice\PhpWord\Writer\Word2007($phpWord);

// use new function to get $tables XML
$tableStr = $objWriter->getWriterPart('Document')->getTableAsText($table);

// setValue in loaded Template
$templateProcessor->setValue('table', $tableStr);

As mentioned, not a good but a working way. Maybe it can help some people.
Improvements and suggestions are always appreciated :)

All 6 comments

I put this as an enhancement request. Thanks.

Hey Guys,
I would really appreciate this feature too. In the meanwhile, I use a little (and of course really dirty) workaround. This would be to add a new public function to the Writer-Part Document Class as follows:

  public function getTableAsText($element) {
    $xmlWriter = $this->getXmlWriter();
    $writer = new \PhpOffice\PhpWord\Writer\Word2007\Element\Table($xmlWriter, $element);
    $writer->write();
    return $xmlWriter->getData();
  }

Using this, you can create a phpWord-Object as usual, create a new table $table and insert it like that:

// create new writer
$objWriter = new PhpOffice\PhpWord\Writer\Word2007($phpWord);

// use new function to get $tables XML
$tableStr = $objWriter->getWriterPart('Document')->getTableAsText($table);

// setValue in loaded Template
$templateProcessor->setValue('table', $tableStr);

As mentioned, not a good but a working way. Maybe it can help some people.
Improvements and suggestions are always appreciated :)

Thank you @alexsparky, this works very well. I guess the progression would be to make a generic getAsText function that could then be used for more advanced template replacement.

Hey guys,

you could also use your own class to write the "getTableAsText"-function (no patch is needed):

    /**
     * Write table object to xml, ready for Word
     *
     * @param \PhpOffice\PhpWord\Element\Table $table
     * @return xml
     */
    private function getTableAsText($table)
    {
        // Get \PhpOffice\PhpWord\Writer\Word2007
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($this->phpWordObject);
        // Get \PhpOffice\PhpWord\Writer\Word2007\Part\Document
        $partDocument = $objWriter->getWriterPart('document');
        $parentWriter = $partDocument->getParentWriter();
        $useDiskCaching = false;
        if (!is_null($parentWriter)) {
            if ($parentWriter->isUseDiskCaching()) {
                $useDiskCaching = true;
            }
        }
        // Get XMLWriter
        if ($useDiskCaching) {
            $xmlWriter = new \PhpOffice\Common\XMLWriter(\PhpOffice\Common\XMLWriter::STORAGE_DISK, $parentWriter->getDiskCachingDirectory(), \PhpOffice\PhpWord\Settings::hasCompatibility());
        }
        else {
            $xmlWriter = new \PhpOffice\Common\XMLWriter(\PhpOffice\Common\XMLWriter::STORAGE_MEMORY, './', \PhpOffice\PhpWord\Settings::hasCompatibility());
        }
        // Get Table Element
        $writer = new \PhpOffice\PhpWord\Writer\Word2007\Element\Table($xmlWriter, $table);
        // Write to xml
        $writer->write();

        return $xmlWriter->getData();
    }

just wanted to let you know, if you insert a table like this, you will find it wrapped inside blocks which is not quite correct. in word, the file looks correct. but in the moment you want to convert it to pdf (for example using libreoffice) you will notify the table is stripped out because of the bad format.

you will have to REPLACE the whole block instead of just replacing the content...

you can do this by using xpath or pre_replace for example. good luck!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cyrillkalita picture cyrillkalita  路  6Comments

taophp picture taophp  路  3Comments

jaberu picture jaberu  路  3Comments

JFSebastianVillalba picture JFSebastianVillalba  路  3Comments

phuwin picture phuwin  路  3Comments