Skip to content

Commit de02e43

Browse files
committed
Merge pull request #125 from ivanlanin/msword-footnote-fix
MS Word footnote fix
2 parents 645d237 + c8a1e2e commit de02e43

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

Classes/PHPWord/Writer/Word2007.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public function save($pFilename = null)
128128
$rID = PHPWord_Media::countSectionMediaElements() + 6;
129129
$_sections = $this->_document->getSections();
130130

131+
$footers = array();
131132
foreach ($_sections as $section) {
132133
$_headers = $section->getHeaders();
133134
foreach ($_headers as $index => &$_header) {
@@ -139,8 +140,8 @@ public function save($pFilename = null)
139140
}
140141

141142
$_footer = $section->getFooter();
143+
$footers[++$_cFtrs] = $_footer;
142144
if (!is_null($_footer)) {
143-
$_cFtrs++;
144145
$_footer->setRelationId(++$rID);
145146
$_footerCount = $_footer->getFooterCount();
146147
$_footerFile = 'footer' . $_footerCount . '.xml';
@@ -161,7 +162,15 @@ public function save($pFilename = null)
161162

162163
// build docx file
163164
// Write dynamic files
164-
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->_imageTypes, $this->_objectTypes, $_cHdrs, $_cFtrs));
165+
$objZip->addFromString(
166+
'[Content_Types].xml',
167+
$this->getWriterPart('contenttypes')->writeContentTypes(
168+
$this->_imageTypes,
169+
$this->_objectTypes,
170+
$_cHdrs,
171+
$footers
172+
)
173+
);
165174
$objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->_document));
166175
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->_document));
167176
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->_document));

Classes/PHPWord/Writer/Word2007/ContentTypes.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
class PHPWord_Writer_Word2007_ContentTypes extends PHPWord_Writer_Word2007_WriterPart
3232
{
3333

34-
public function writeContentTypes($_imageTypes, $_objectTypes, $_cHdrs, $_cFtrs)
34+
public function writeContentTypes($_imageTypes, $_objectTypes, $_cHdrs, $footers)
3535
{
3636
// Create XML writer
3737
$objWriter = null;
@@ -153,12 +153,14 @@ public function writeContentTypes($_imageTypes, $_objectTypes, $_cHdrs, $_cFtrs)
153153
);
154154
}
155155

156-
for ($i = 1; $i <= $_cFtrs; $i++) {
157-
$this->_writeOverrideContentType(
158-
$objWriter,
159-
'/word/footer' . $i . '.xml',
160-
'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml'
161-
);
156+
for ($i = 1; $i <= count($footers); $i++) {
157+
if (!is_null($footers[$i])) {
158+
$this->_writeOverrideContentType(
159+
$objWriter,
160+
'/word/footer' . $i . '.xml',
161+
'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml'
162+
);
163+
}
162164
}
163165

164166

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Changes in branch for release 0.7.1 :
5252
- Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF
5353
- Feature: (ivanlanin) - Reader: Initial effort for Word2007
5454
- Feature: (ivanlanin) - MemoryImage: Allow remote image when allow_url_open = on
55+
- Bugfix: (ivanlanin) - Footnote: Corrupt DOCX reported by MS Word when sections > 1 and not every sections have footnote
5556
- QA: (Progi1984) - UnitTests
5657

5758
Changes in branch for release 0.7.0 :

0 commit comments

Comments
 (0)