|
24 | 24 | use PhpOffice\PhpWord\Exception\CopyFileException;
|
25 | 25 | use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
|
26 | 26 | use PhpOffice\PhpWord\Exception\Exception;
|
| 27 | +use PhpOffice\PhpWord\Shared\Html; |
27 | 28 | use PhpOffice\PhpWord\Shared\Text;
|
28 | 29 | use PhpOffice\PhpWord\Shared\XMLWriter;
|
29 | 30 | use PhpOffice\PhpWord\Shared\ZipArchive;
|
| 31 | +use PhpOffice\PhpWord\Writer\Word2007; |
30 | 32 | use Throwable;
|
31 | 33 | use XSLTProcessor;
|
32 | 34 |
|
@@ -315,6 +317,96 @@ public function setComplexBlock($search, Element\AbstractElement $complexType):
|
315 | 317 | $this->replaceXmlBlock($search, $xmlWriter->getData(), 'w:p');
|
316 | 318 | }
|
317 | 319 |
|
| 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 | + |
318 | 410 | /**
|
319 | 411 | * @param mixed $search
|
320 | 412 | * @param mixed $replace
|
|
0 commit comments