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