Skip to content

Commit b670a1e

Browse files
committed
DOCX Writer: Create word/settings.xml and word/webSettings.xml dynamically
1 parent b419d64 commit b670a1e

File tree

15 files changed

+199
-78
lines changed

15 files changed

+199
-78
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ This release marked heavy refactorings on internal code structure with the creat
3535
- HTML Writer: Basic HTML writer: text, textrun, link, title, textbreak, table, image (as Base64), footnote, endnote - @ivanlanin GH-203 GH-67 GH-147
3636
- PDF Writer: Basic PDF writer using DomPDF: All HTML element except image - @ivanlanin GH-68
3737
- DOCX Writer: Change `docProps/app.xml` `Application` to `PHPWord` - @ivanlanin
38+
- DOCX Writer: Create `word/settings.xml` and `word/webSettings.xml` dynamically - @ivanlanin
3839

3940
### Bugfixes
4041

src/PhpWord/Writer/Word2007.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use PhpOffice\PhpWord\Writer\Word2007\Notes;
2222
use PhpOffice\PhpWord\Writer\Word2007\Numbering;
2323
use PhpOffice\PhpWord\Writer\Word2007\Rels;
24+
use PhpOffice\PhpWord\Writer\Word2007\Settings;
25+
use PhpOffice\PhpWord\Writer\Word2007\WebSettings;
2426
use PhpOffice\PhpWord\Writer\Word2007\Styles;
2527

2628
/**
@@ -59,6 +61,8 @@ public function __construct(PhpWord $phpWord = null)
5961
$this->writerParts['document'] = new Document();
6062
$this->writerParts['styles'] = new Styles();
6163
$this->writerParts['numbering'] = new Numbering();
64+
$this->writerParts['settings'] = new Settings();
65+
$this->writerParts['websettings'] = new WebSettings();
6266
$this->writerParts['header'] = new Header();
6367
$this->writerParts['footer'] = new Footer();
6468
$this->writerParts['footnotes'] = new Notes();
@@ -118,11 +122,11 @@ public function save($filename = null)
118122
$objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->phpWord));
119123
$objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord));
120124
$objZip->addFromString('word/numbering.xml', $this->getWriterPart('numbering')->writeNumbering());
125+
$objZip->addFromString('word/settings.xml', $this->getWriterPart('settings')->writeSettings());
126+
$objZip->addFromString('word/webSettings.xml', $this->getWriterPart('websettings')->writeWebSettings());
121127

122128
// Write static files
123-
$objZip->addFile(__DIR__ . '/../_staticDocParts/settings.xml', 'word/settings.xml');
124129
$objZip->addFile(__DIR__ . '/../_staticDocParts/theme1.xml', 'word/theme/theme1.xml');
125-
$objZip->addFile(__DIR__ . '/../_staticDocParts/webSettings.xml', 'word/webSettings.xml');
126130
$objZip->addFile(__DIR__ . '/../_staticDocParts/fontTable.xml', 'word/fontTable.xml');
127131

128132
// Close file

src/PhpWord/Writer/Word2007/ContentTypes.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ public function writeContentTypes($contentTypes)
4646
}
4747

4848
$xmlWriter = $this->getXmlWriter();
49+
4950
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
5051
$xmlWriter->startElement('Types');
5152
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
53+
5254
$this->writeContentType($xmlWriter, $defaults, true);
5355
$this->writeContentType($xmlWriter, $overrides, false);
54-
$xmlWriter->endElement();
56+
57+
$xmlWriter->endElement(); // Types
5558

5659
return $xmlWriter->getData();
5760
}

src/PhpWord/Writer/Word2007/Document.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,10 @@ public function writeDocument(PhpWord $phpWord = null)
3434
if (is_null($phpWord)) {
3535
throw new Exception("No PhpWord assigned.");
3636
}
37-
38-
// Create XML writer
3937
$xmlWriter = $this->getXmlWriter();
4038

41-
// XML header
4239
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
43-
44-
// w:document
4540
$xmlWriter->startElement('w:document');
46-
4741
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
4842
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
4943
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
@@ -74,8 +68,9 @@ public function writeDocument(PhpWord $phpWord = null)
7468
}
7569
}
7670

77-
$xmlWriter->endElement(); // End w:body
78-
$xmlWriter->endElement(); // End w:document
71+
$xmlWriter->endElement(); // w:body
72+
73+
$xmlWriter->endElement(); // w:document
7974

8075
// Return
8176
return $xmlWriter->getData();

src/PhpWord/Writer/Word2007/Footer.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ class Footer extends Base
2323
*/
2424
public function writeFooter(FooterElement $footer)
2525
{
26-
// Create XML writer
2726
$xmlWriter = $this->getXmlWriter();
2827

29-
// XML header
3028
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
31-
3229
$xmlWriter->startElement('w:ftr');
3330
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
3431
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
@@ -42,9 +39,8 @@ public function writeFooter(FooterElement $footer)
4239

4340
$this->writeContainerElements($xmlWriter, $footer);
4441

45-
$xmlWriter->endElement();
42+
$xmlWriter->endElement(); // w:ftr
4643

47-
// Return
4844
return $xmlWriter->getData();
4945
}
5046
}

src/PhpWord/Writer/Word2007/Header.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ class Header extends Base
2323
*/
2424
public function writeHeader(HeaderElement $header)
2525
{
26-
// Create XML writer
2726
$xmlWriter = $this->getXmlWriter();
2827

29-
// XML header
3028
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
31-
3229
$xmlWriter->startElement('w:hdr');
3330
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
3431
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
@@ -42,9 +39,8 @@ public function writeHeader(HeaderElement $header)
4239

4340
$this->writeContainerElements($xmlWriter, $header);
4441

45-
$xmlWriter->endElement();
42+
$xmlWriter->endElement(); // w:hdr
4643

47-
// Return
4844
return $xmlWriter->getData();
4945
}
5046
}

src/PhpWord/Writer/Word2007/Notes.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function writeNotes($elements, $notesTypes = 'footnotes')
3131
$elementNode = $isFootnote ? 'w:footnote' : 'w:endnote';
3232
$xmlWriter = $this->getXmlWriter();
3333

34-
// XML header
3534
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
3635
$xmlWriter->startElement($rootNode);
3736
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
@@ -73,7 +72,7 @@ public function writeNotes($elements, $notesTypes = 'footnotes')
7372
}
7473
}
7574

76-
$xmlWriter->endElement();
75+
$xmlWriter->endElement(); // $rootNode
7776

7877
return $xmlWriter->getData();
7978
}

src/PhpWord/Writer/Word2007/Numbering.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function writeNumbering()
159159
}
160160
}
161161

162-
$xmlWriter->endElement();
162+
$xmlWriter->endElement(); // w:numbering
163163

164164
return $xmlWriter->getData();
165165
}

src/PhpWord/Writer/Word2007/Rels.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ private function writeRels(XMLWriter $xmlWriter, $xmlRels = null, $mediaRels = n
107107
$this->writeRel($xmlWriter, $id++, "officeDocument/2006/relationships/{$type}", $target, $targetMode);
108108
}
109109
}
110-
$xmlWriter->endElement();
110+
111+
$xmlWriter->endElement(); // Relationships
111112
}
112113

113114
/**
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Writer\Word2007;
11+
12+
use PhpOffice\PhpWord\Shared\XMLWriter;
13+
14+
/**
15+
* Word2007 settings part writer
16+
*/
17+
class Settings extends AbstractWriterPart
18+
{
19+
/**
20+
* Write word/settings.xml
21+
*/
22+
public function writeSettings()
23+
{
24+
$settings = array(
25+
'w:zoom' => array('@attributes' => array('w:percent' => '100')),
26+
'w:embedSystemFonts' => '',
27+
'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')),
28+
'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')),
29+
'w:doNotHyphenateCaps' => '',
30+
'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')),
31+
'w:doNotValidateAgainstSchema' => '',
32+
'w:doNotDemarcateInvalidXml' => '',
33+
'w:compat' => array(
34+
'w:useNormalStyleForList' => '',
35+
'w:doNotUseIndentAsNumberingTabStop' => '',
36+
'w:useAltKinsokuLineBreakRules' => '',
37+
'w:allowSpaceOfSameStyleInTable' => '',
38+
'w:doNotSuppressIndentation' => '',
39+
'w:doNotAutofitConstrainedTables' => '',
40+
'w:autofitToFirstFixedWidthCell' => '',
41+
'w:underlineTabInNumList' => '',
42+
'w:displayHangulFixedWidth' => '',
43+
'w:splitPgBreakAndParaMark' => '',
44+
'w:doNotVertAlignCellWithSp' => '',
45+
'w:doNotBreakConstrainedForcedTable' => '',
46+
'w:doNotVertAlignInTxbx' => '',
47+
'w:useAnsiKerningPairs' => '',
48+
'w:cachedColBalance' => '',
49+
),
50+
'm:mathPr' => array(
51+
'm:mathFont' => array('@attributes' => array('m:val' => 'Cambria Math')),
52+
'm:brkBin' => array('@attributes' => array('m:val' => 'before')),
53+
'm:brkBinSub' => array('@attributes' => array('m:val' => '--')),
54+
'm:smallFrac' => array('@attributes' => array('m:val' => 'off')),
55+
'm:dispDef' => '',
56+
'm:lMargin' => array('@attributes' => array('m:val' => '0')),
57+
'm:rMargin' => array('@attributes' => array('m:val' => '0')),
58+
'm:defJc' => array('@attributes' => array('m:val' => 'centerGroup')),
59+
'm:wrapIndent' => array('@attributes' => array('m:val' => '1440')),
60+
'm:intLim' => array('@attributes' => array('m:val' => 'subSup')),
61+
'm:naryLim' => array('@attributes' => array('m:val' => 'undOvr')),
62+
),
63+
'w:uiCompat97To2003' => '',
64+
'w:themeFontLang' => array('@attributes' => array('w:val' => 'de-DE')),
65+
'w:clrSchemeMapping' => array(
66+
'@attributes' => array(
67+
'w:bg1' => 'light1',
68+
'w:t1' => 'dark1',
69+
'w:bg2' => 'light2',
70+
'w:t2' => 'dark2',
71+
'w:accent1' => 'accent1',
72+
'w:accent2' => 'accent2',
73+
'w:accent3' => 'accent3',
74+
'w:accent4' => 'accent4',
75+
'w:accent5' => 'accent5',
76+
'w:accent6' => 'accent6',
77+
'w:hyperlink' => 'hyperlink',
78+
'w:followedHyperlink' => 'followedHyperlink',
79+
),
80+
),
81+
'w:doNotIncludeSubdocsInStats' => '',
82+
'w:doNotAutoCompressPictures' => '',
83+
'w:decimalSymbol' => array('@attributes' => array('w:val' => ',')),
84+
'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
85+
);
86+
87+
$xmlWriter = $this->getXmlWriter();
88+
89+
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
90+
$xmlWriter->startElement('w:settings');
91+
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
92+
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
93+
$xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
94+
$xmlWriter->writeAttribute('xmlns:sl', 'http://schemas.openxmlformats.org/schemaLibrary/2006/main');
95+
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
96+
$xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
97+
$xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
98+
99+
foreach ($settings as $settingKey => $settingValue) {
100+
$this->writeSetting($xmlWriter, $settingKey, $settingValue);
101+
}
102+
103+
$xmlWriter->endElement(); // w:settings
104+
105+
return $xmlWriter->getData();
106+
}
107+
108+
/**
109+
* Write indivual setting, recursive to any child settings
110+
*
111+
* @param XMLWriter $xmlWriter XML Writer
112+
* @param string $settingKey
113+
* @param array $settingValue
114+
*/
115+
protected function writeSetting($xmlWriter, $settingKey, $settingValue)
116+
{
117+
if ($settingValue == '') {
118+
$xmlWriter->writeElement($settingKey);
119+
} else {
120+
$xmlWriter->startElement($settingKey);
121+
foreach ($settingValue as $childKey => $childValue) {
122+
if ($childKey == '@attributes') {
123+
foreach ($childValue as $key => $val) {
124+
$xmlWriter->writeAttribute($key, $val);
125+
}
126+
} else {
127+
$this->writeSetting($xmlWriter, $childKey, $childValue);
128+
}
129+
}
130+
$xmlWriter->endElement();
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)