Skip to content

Commit a7bfc8d

Browse files
authored
Merge pull request #1485 from Timanx/develop
Fix parsing of Office 365 documents
2 parents 586f45e + c12f98f commit a7bfc8d

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ v0.16.0 (xx xxx 2018)
1111
- Fix regex in `cloneBlock` function @nicoder #1269
1212
- HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436
1313
- RTF writer: Round getPageSizeW and getPageSizeH to avoid decimals @Patrick64 #1493
14+
- Fix parsing of Office 365 documents @Timanx #1485
1415

1516
v0.15.0 (14 Jul 2018)
1617
----------------------

src/PhpWord/TemplateProcessor.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,19 @@ protected function getHeaderName($index)
503503
}
504504

505505
/**
506+
* Usually, the name of main part document will be 'document.xml'. However, some .docx files (possibly those from Office 365, experienced also on documents from Word Online created from blank templates) have file 'document22.xml' in their zip archive instead of 'document.xml'. This method searches content types file to correctly determine the file name.
507+
*
506508
* @return string
507509
*/
508510
protected function getMainPartName()
509511
{
510-
return 'word/document.xml';
512+
$contentTypes = $this->zipClass->getFromName('[Content_Types].xml');
513+
514+
$pattern = '~PartName="\/(word\/document.*?\.xml)" ContentType="application\/vnd\.openxmlformats-officedocument\.wordprocessingml\.document\.main\+xml"~';
515+
516+
preg_match($pattern, $contentTypes, $matches);
517+
518+
return array_key_exists(1, $matches) ? $matches[1] : 'word/document.xml';
511519
}
512520

513521
/**

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,13 @@ public function cloneBlockCanCloneABlockTwice()
276276
);
277277
}
278278
}
279+
280+
public function testMainPartNameDetection()
281+
{
282+
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx');
283+
284+
$variables = array('test');
285+
286+
$this->assertEquals($variables, $templateProcessor->getVariables());
287+
}
279288
}
Binary file not shown.

0 commit comments

Comments
 (0)