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.
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
you will have to REPLACE the whole block instead of just replacing the
you can do this by using xpath or pre_replace for example. good luck!
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:
Using this, you can create a phpWord-Object as usual, create a new table
$tableand insert it like that:As mentioned, not a good but a working way. Maybe it can help some people.
Improvements and suggestions are always appreciated :)