Skip to content

Commit 7e90a79

Browse files
committed
Add a method to TemplateProcessor for rendering HTML content.
1 parent 41cf4eb commit 7e90a79

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
use PhpOffice\PhpWord\Exception\CopyFileException;
2525
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
2626
use PhpOffice\PhpWord\Exception\Exception;
27+
use PhpOffice\PhpWord\Shared\Html;
2728
use PhpOffice\PhpWord\Shared\Text;
2829
use PhpOffice\PhpWord\Shared\XMLWriter;
2930
use PhpOffice\PhpWord\Shared\ZipArchive;
31+
use PhpOffice\PhpWord\Writer\Word2007;
3032
use Throwable;
3133
use XSLTProcessor;
3234

@@ -315,6 +317,96 @@ public function setComplexBlock($search, Element\AbstractElement $complexType):
315317
$this->replaceXmlBlock($search, $xmlWriter->getData(), 'w:p');
316318
}
317319

320+
/**
321+
* @param string $search
322+
* @param string $htmlContent
323+
*/
324+
public function setHtmlBlock($search,$htmlContent,$fullHtml=false): void
325+
{
326+
$phpWord = new PhpWord();
327+
$section = $phpWord->addSection();
328+
Html::addHtml($section,$htmlContent,$fullHtml);
329+
$zip = $this->zip();
330+
$obj = new Word2007($phpWord);
331+
$refClass = new \ReflectionClass(Word2007::class);
332+
$addFilesToPackage = $refClass->getMethod('addFilesToPackage');
333+
$addFilesToPackage->setAccessible(true);
334+
$sectionMedia = Media::getElements('section');
335+
//add image to zip
336+
if (!empty($sectionMedia)) {
337+
//insert image to zip
338+
$res = $addFilesToPackage->invoke($obj,$zip, $sectionMedia);
339+
$registerContentTypes = $refClass->getMethod('registerContentTypes');
340+
$registerContentTypes->setAccessible(true);
341+
$registerContentTypes->invoke($obj,$sectionMedia);
342+
343+
$relationships = $refClass->getProperty('relationships');
344+
$relationships->setAccessible(true);
345+
$tmpRelationships = [];
346+
foreach ($sectionMedia as $element) {
347+
$tmpRelationships[] = $element;
348+
}
349+
$relationships->setValue($obj,$tmpRelationships);
350+
}
351+
$documentWriterPart = $obj->getWriterPart("Document");
352+
$relsDocumentWriterPart = $obj->getWriterPart("RelsDocument");
353+
$documentXml = $documentWriterPart->write();
354+
$relsDocumentXml = $relsDocumentWriterPart->write();
355+
// Load the XML string into a SimpleXMLElement
356+
$xml = simplexml_load_string($documentXml);
357+
// Extract content between <w:body> tags
358+
$bodyContent = $xml->xpath('//w:body/*');
359+
// Output the extracted content
360+
$documentBodyStr = "";
361+
foreach ($bodyContent as $element) {
362+
$documentBodyStr .= $element->asXML();
363+
}
364+
//replace html content r:id vaule avoid rid conflict
365+
$rIdsElement = $xml->xpath('//*[@r:id]');
366+
$rIdValuesMap = [];
367+
if ($rIdsElement){
368+
foreach ($rIdsElement as $idEle){
369+
$rid = (string)$idEle->attributes('r', true)->id;
370+
$rIdValuesMap[$rid] = $rid;
371+
}
372+
}
373+
if (!empty($rIdValuesMap )){
374+
foreach ($rIdValuesMap as $rid => $value){
375+
$replactVulue = $rid."-1";
376+
$rIdValuesMap[$rid] = $replactVulue;
377+
$documentBodyStr = str_replace($rid,$replactVulue,$documentBodyStr);
378+
}
379+
}
380+
//replace document.xml
381+
$this->replaceXmlBlock($search, $documentBodyStr, 'w:p');
382+
383+
$xml = simplexml_load_string($relsDocumentXml);
384+
// Register the namespace
385+
$xml->registerXPathNamespace('ns', 'http://schemas.openxmlformats.org/package/2006/relationships');
386+
// Use XPath to find all Relationship nodes
387+
$RelationshipXmls = $xml->xpath('//ns:Relationship');
388+
$RelationshipStr = "";
389+
foreach ($RelationshipXmls as $relationshipXml){
390+
$rid = (string)$relationshipXml->attributes();
391+
if (isset($rIdValuesMap[$rid])){
392+
$tmpStr = $relationshipXml->asXML();
393+
$tmpStr = str_replace($rid,$rIdValuesMap[$rid],$tmpStr);
394+
$RelationshipStr .= $tmpStr;
395+
}
396+
}
397+
//add relation to document.xml.rels
398+
if ($RelationshipStr){
399+
$relsFileName = $this->getRelationsName($this->getMainPartName());
400+
$content = $this->tempDocumentRelations[$this->getMainPartName()];
401+
$endStr = "</Relationships>";
402+
$replaceValue = $RelationshipStr.$endStr;
403+
$content = str_replace($endStr,$replaceValue,$content);
404+
$this->tempDocumentRelations[$this->getMainPartName()] = $content ;
405+
}
406+
407+
}
408+
409+
318410
/**
319411
* @param mixed $search
320412
* @param mixed $replace

0 commit comments

Comments
 (0)