@@ -321,6 +321,7 @@ public function setComplexBlock($search, Element\AbstractElement $complexType):
321
321
/**
322
322
* @param string $search
323
323
* @param string $htmlContent
324
+ * @param bool $fullHtml
324
325
*/
325
326
public function setHtmlBlock ($ search , $ htmlContent , $ fullHtml = false ): void
326
327
{
@@ -356,12 +357,19 @@ public function setHtmlBlock($search, $htmlContent, $fullHtml = false): void
356
357
// Load the XML string into a SimpleXMLElement
357
358
$ xml = simplexml_load_string ($ documentXml );
358
359
// Extract content between <w:body> tags
360
+ if ($ xml === false ){
361
+ return ;
362
+ }
359
363
$ bodyContent = $ xml ->xpath ('//w:body/* ' );
360
364
// Output the extracted content
361
365
$ documentBodyStr = '' ;
362
- foreach ($ bodyContent as $ element ) {
363
- $ documentBodyStr .= $ element ->asXML ();
366
+ if ($ bodyContent ) {
367
+ foreach ($ bodyContent as $ element ) {
368
+ $ documentBodyStr .= $ element ->asXML ();
369
+ }
364
370
}
371
+
372
+
365
373
//replace html content r:id vaule avoid rid conflict
366
374
$ rIdsElement = $ xml ->xpath ('//*[@r:id] ' );
367
375
$ rIdValuesMap = [];
@@ -382,19 +390,27 @@ public function setHtmlBlock($search, $htmlContent, $fullHtml = false): void
382
390
$ this ->replaceXmlBlock ($ search , $ documentBodyStr , 'w:p ' );
383
391
384
392
$ xml = simplexml_load_string ($ relsDocumentXml );
393
+ if ($ xml === false ){
394
+ return ;
395
+ }
385
396
// Register the namespace
386
397
$ xml ->registerXPathNamespace ('ns ' , 'http://schemas.openxmlformats.org/package/2006/relationships ' );
387
398
// Use XPath to find all Relationship nodes
388
399
$ RelationshipXmls = $ xml ->xpath ('//ns:Relationship ' );
389
400
$ RelationshipStr = '' ;
390
- foreach ($ RelationshipXmls as $ relationshipXml ) {
391
- $ rid = (string ) $ relationshipXml ->attributes ();
392
- if (isset ($ rIdValuesMap [$ rid ])) {
393
- $ tmpStr = $ relationshipXml ->asXML ();
394
- $ tmpStr = str_replace ($ rid , $ rIdValuesMap [$ rid ], $ tmpStr );
395
- $ RelationshipStr .= $ tmpStr ;
401
+ if ($ RelationshipXmls ){
402
+ foreach ($ RelationshipXmls as $ relationshipXml ) {
403
+ $ rid = (string ) $ relationshipXml ->attributes ();
404
+ if (isset ($ rIdValuesMap [$ rid ])) {
405
+ $ tmpStr = $ relationshipXml ->asXML ();
406
+ if ($ tmpStr !=false ){
407
+ $ tmpStr = str_replace ($ rid , $ rIdValuesMap [$ rid ], $ tmpStr );
408
+ $ RelationshipStr .= $ tmpStr ;
409
+ }
410
+ }
396
411
}
397
412
}
413
+
398
414
//add relation to document.xml.rels
399
415
if ($ RelationshipStr ) {
400
416
$ relsFileName = $ this ->getRelationsName ($ this ->getMainPartName ());
0 commit comments