Skip to content

Commit c8a1e2e

Browse files
committed
Final fix that satisfies both MS Word and OpenOffice
1 parent d87fa2a commit c8a1e2e

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

Classes/PHPWord/Section.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,6 @@ public function createFooter()
422422
*/
423423
public function getFooter()
424424
{
425-
if (!($this->_footer instanceOf PHPWord_Section_Footer)) {
426-
$this->_footer = new PHPWord_Section_Footer($this->_sectionCount);
427-
}
428425
return $this->_footer;
429426
}
430427

Classes/PHPWord/Writer/Word2007.php

Lines changed: 18 additions & 7 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,12 +140,14 @@ public function save($pFilename = null)
139140
}
140141

141142
$_footer = $section->getFooter();
142-
$_cFtrs++;
143-
$_footer->setRelationId(++$rID);
144-
$_footerCount = $_footer->getFooterCount();
145-
$_footerFile = 'footer' . $_footerCount . '.xml';
146-
$sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID);
147-
$objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer));
143+
$footers[++$_cFtrs] = $_footer;
144+
if (!is_null($_footer)) {
145+
$_footer->setRelationId(++$rID);
146+
$_footerCount = $_footer->getFooterCount();
147+
$_footerFile = 'footer' . $_footerCount . '.xml';
148+
$sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID);
149+
$objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer));
150+
}
148151
}
149152

150153
if (PHPWord_Footnote::countFootnoteElements() > 0) {
@@ -159,7 +162,15 @@ public function save($pFilename = null)
159162

160163
// build docx file
161164
// Write dynamic files
162-
$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+
);
163174
$objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->_document));
164175
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->_document));
165176
$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)