DOCX Writer: Create `word/settings.xml` and `word/webSettings.xml` dynamically

This commit is contained in:
Ivan Lanin 2014-04-16 22:13:00 +07:00
parent b419d64277
commit b670a1e70f
15 changed files with 199 additions and 78 deletions

View File

@ -35,6 +35,7 @@ This release marked heavy refactorings on internal code structure with the creat
- HTML Writer: Basic HTML writer: text, textrun, link, title, textbreak, table, image (as Base64), footnote, endnote - @ivanlanin GH-203 GH-67 GH-147
- PDF Writer: Basic PDF writer using DomPDF: All HTML element except image - @ivanlanin GH-68
- DOCX Writer: Change `docProps/app.xml` `Application` to `PHPWord` - @ivanlanin
- DOCX Writer: Create `word/settings.xml` and `word/webSettings.xml` dynamically - @ivanlanin
### Bugfixes

View File

@ -21,6 +21,8 @@ use PhpOffice\PhpWord\Writer\Word2007\Header;
use PhpOffice\PhpWord\Writer\Word2007\Notes;
use PhpOffice\PhpWord\Writer\Word2007\Numbering;
use PhpOffice\PhpWord\Writer\Word2007\Rels;
use PhpOffice\PhpWord\Writer\Word2007\Settings;
use PhpOffice\PhpWord\Writer\Word2007\WebSettings;
use PhpOffice\PhpWord\Writer\Word2007\Styles;
/**
@ -59,6 +61,8 @@ class Word2007 extends AbstractWriter implements WriterInterface
$this->writerParts['document'] = new Document();
$this->writerParts['styles'] = new Styles();
$this->writerParts['numbering'] = new Numbering();
$this->writerParts['settings'] = new Settings();
$this->writerParts['websettings'] = new WebSettings();
$this->writerParts['header'] = new Header();
$this->writerParts['footer'] = new Footer();
$this->writerParts['footnotes'] = new Notes();
@ -118,11 +122,11 @@ class Word2007 extends AbstractWriter implements WriterInterface
$objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->phpWord));
$objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord));
$objZip->addFromString('word/numbering.xml', $this->getWriterPart('numbering')->writeNumbering());
$objZip->addFromString('word/settings.xml', $this->getWriterPart('settings')->writeSettings());
$objZip->addFromString('word/webSettings.xml', $this->getWriterPart('websettings')->writeWebSettings());
// Write static files
$objZip->addFile(__DIR__ . '/../_staticDocParts/settings.xml', 'word/settings.xml');
$objZip->addFile(__DIR__ . '/../_staticDocParts/theme1.xml', 'word/theme/theme1.xml');
$objZip->addFile(__DIR__ . '/../_staticDocParts/webSettings.xml', 'word/webSettings.xml');
$objZip->addFile(__DIR__ . '/../_staticDocParts/fontTable.xml', 'word/fontTable.xml');
// Close file

View File

@ -46,12 +46,15 @@ class ContentTypes extends AbstractWriterPart
}
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('Types');
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
$this->writeContentType($xmlWriter, $defaults, true);
$this->writeContentType($xmlWriter, $overrides, false);
$xmlWriter->endElement();
$xmlWriter->endElement(); // Types
return $xmlWriter->getData();
}

View File

@ -34,16 +34,10 @@ class Document extends Base
if (is_null($phpWord)) {
throw new Exception("No PhpWord assigned.");
}
// Create XML writer
$xmlWriter = $this->getXmlWriter();
// XML header
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
// w:document
$xmlWriter->startElement('w:document');
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
@ -74,8 +68,9 @@ class Document extends Base
}
}
$xmlWriter->endElement(); // End w:body
$xmlWriter->endElement(); // End w:document
$xmlWriter->endElement(); // w:body
$xmlWriter->endElement(); // w:document
// Return
return $xmlWriter->getData();

View File

@ -23,12 +23,9 @@ class Footer extends Base
*/
public function writeFooter(FooterElement $footer)
{
// Create XML writer
$xmlWriter = $this->getXmlWriter();
// XML header
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('w:ftr');
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
@ -42,9 +39,8 @@ class Footer extends Base
$this->writeContainerElements($xmlWriter, $footer);
$xmlWriter->endElement();
$xmlWriter->endElement(); // w:ftr
// Return
return $xmlWriter->getData();
}
}

View File

@ -23,12 +23,9 @@ class Header extends Base
*/
public function writeHeader(HeaderElement $header)
{
// Create XML writer
$xmlWriter = $this->getXmlWriter();
// XML header
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('w:hdr');
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
@ -42,9 +39,8 @@ class Header extends Base
$this->writeContainerElements($xmlWriter, $header);
$xmlWriter->endElement();
$xmlWriter->endElement(); // w:hdr
// Return
return $xmlWriter->getData();
}
}

View File

@ -31,7 +31,6 @@ class Notes extends Base
$elementNode = $isFootnote ? 'w:footnote' : 'w:endnote';
$xmlWriter = $this->getXmlWriter();
// XML header
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement($rootNode);
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
@ -73,7 +72,7 @@ class Notes extends Base
}
}
$xmlWriter->endElement();
$xmlWriter->endElement(); // $rootNode
return $xmlWriter->getData();
}

View File

@ -159,7 +159,7 @@ class Numbering extends Base
}
}
$xmlWriter->endElement();
$xmlWriter->endElement(); // w:numbering
return $xmlWriter->getData();
}

View File

@ -107,7 +107,8 @@ class Rels extends AbstractWriterPart
$this->writeRel($xmlWriter, $id++, "officeDocument/2006/relationships/{$type}", $target, $targetMode);
}
}
$xmlWriter->endElement();
$xmlWriter->endElement(); // Relationships
}
/**

View File

@ -0,0 +1,133 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\Shared\XMLWriter;
/**
* Word2007 settings part writer
*/
class Settings extends AbstractWriterPart
{
/**
* Write word/settings.xml
*/
public function writeSettings()
{
$settings = array(
'w:zoom' => array('@attributes' => array('w:percent' => '100')),
'w:embedSystemFonts' => '',
'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')),
'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')),
'w:doNotHyphenateCaps' => '',
'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')),
'w:doNotValidateAgainstSchema' => '',
'w:doNotDemarcateInvalidXml' => '',
'w:compat' => array(
'w:useNormalStyleForList' => '',
'w:doNotUseIndentAsNumberingTabStop' => '',
'w:useAltKinsokuLineBreakRules' => '',
'w:allowSpaceOfSameStyleInTable' => '',
'w:doNotSuppressIndentation' => '',
'w:doNotAutofitConstrainedTables' => '',
'w:autofitToFirstFixedWidthCell' => '',
'w:underlineTabInNumList' => '',
'w:displayHangulFixedWidth' => '',
'w:splitPgBreakAndParaMark' => '',
'w:doNotVertAlignCellWithSp' => '',
'w:doNotBreakConstrainedForcedTable' => '',
'w:doNotVertAlignInTxbx' => '',
'w:useAnsiKerningPairs' => '',
'w:cachedColBalance' => '',
),
'm:mathPr' => array(
'm:mathFont' => array('@attributes' => array('m:val' => 'Cambria Math')),
'm:brkBin' => array('@attributes' => array('m:val' => 'before')),
'm:brkBinSub' => array('@attributes' => array('m:val' => '--')),
'm:smallFrac' => array('@attributes' => array('m:val' => 'off')),
'm:dispDef' => '',
'm:lMargin' => array('@attributes' => array('m:val' => '0')),
'm:rMargin' => array('@attributes' => array('m:val' => '0')),
'm:defJc' => array('@attributes' => array('m:val' => 'centerGroup')),
'm:wrapIndent' => array('@attributes' => array('m:val' => '1440')),
'm:intLim' => array('@attributes' => array('m:val' => 'subSup')),
'm:naryLim' => array('@attributes' => array('m:val' => 'undOvr')),
),
'w:uiCompat97To2003' => '',
'w:themeFontLang' => array('@attributes' => array('w:val' => 'de-DE')),
'w:clrSchemeMapping' => array(
'@attributes' => array(
'w:bg1' => 'light1',
'w:t1' => 'dark1',
'w:bg2' => 'light2',
'w:t2' => 'dark2',
'w:accent1' => 'accent1',
'w:accent2' => 'accent2',
'w:accent3' => 'accent3',
'w:accent4' => 'accent4',
'w:accent5' => 'accent5',
'w:accent6' => 'accent6',
'w:hyperlink' => 'hyperlink',
'w:followedHyperlink' => 'followedHyperlink',
),
),
'w:doNotIncludeSubdocsInStats' => '',
'w:doNotAutoCompressPictures' => '',
'w:decimalSymbol' => array('@attributes' => array('w:val' => ',')),
'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
);
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('w:settings');
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
$xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
$xmlWriter->writeAttribute('xmlns:sl', 'http://schemas.openxmlformats.org/schemaLibrary/2006/main');
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
$xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
$xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
foreach ($settings as $settingKey => $settingValue) {
$this->writeSetting($xmlWriter, $settingKey, $settingValue);
}
$xmlWriter->endElement(); // w:settings
return $xmlWriter->getData();
}
/**
* Write indivual setting, recursive to any child settings
*
* @param XMLWriter $xmlWriter XML Writer
* @param string $settingKey
* @param array $settingValue
*/
protected function writeSetting($xmlWriter, $settingKey, $settingValue)
{
if ($settingValue == '') {
$xmlWriter->writeElement($settingKey);
} else {
$xmlWriter->startElement($settingKey);
foreach ($settingValue as $childKey => $childValue) {
if ($childKey == '@attributes') {
foreach ($childValue as $key => $val) {
$xmlWriter->writeAttribute($key, $val);
}
} else {
$this->writeSetting($xmlWriter, $childKey, $childValue);
}
}
$xmlWriter->endElement();
}
}
}

View File

@ -33,11 +33,8 @@ class Styles extends Base
if (is_null($phpWord)) {
throw new Exception("No PhpWord assigned.");
}
// Create XML writer
$xmlWriter = $this->getXmlWriter();
// XML header
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('w:styles');
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');

View File

@ -0,0 +1,43 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\Shared\XMLWriter;
/**
* Word2007 web settings part writer
*/
class WebSettings extends Settings
{
/**
* Write word/webSettings.xml
*/
public function writeWebSettings()
{
$settings = array(
'w:optimizeForBrowser' => '',
);
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('w:webSettings');
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
foreach ($settings as $settingKey => $settingValue) {
$this->writeSetting($xmlWriter, $settingKey, $settingValue);
}
$xmlWriter->endElement(); // w:settings
return $xmlWriter->getData();
}
}

View File

@ -1,48 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<w:settings xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main">
<w:zoom w:percent="100" />
<w:embedSystemFonts />
<w:defaultTabStop w:val="708" />
<w:hyphenationZone w:val="425" />
<w:doNotHyphenateCaps />
<w:characterSpacingControl w:val="doNotCompress" />
<w:doNotValidateAgainstSchema />
<w:doNotDemarcateInvalidXml />
<w:compat>
<w:useNormalStyleForList />
<w:doNotUseIndentAsNumberingTabStop />
<w:useAltKinsokuLineBreakRules />
<w:allowSpaceOfSameStyleInTable />
<w:doNotSuppressIndentation />
<w:doNotAutofitConstrainedTables />
<w:autofitToFirstFixedWidthCell />
<w:underlineTabInNumList />
<w:displayHangulFixedWidth />
<w:splitPgBreakAndParaMark />
<w:doNotVertAlignCellWithSp />
<w:doNotBreakConstrainedForcedTable />
<w:doNotVertAlignInTxbx />
<w:useAnsiKerningPairs />
<w:cachedColBalance />
</w:compat>
<m:mathPr>
<m:mathFont m:val="Cambria Math" />
<m:brkBin m:val="before" />
<m:brkBinSub m:val="--" />
<m:smallFrac m:val="off" />
<m:dispDef />
<m:lMargin m:val="0" />
<m:rMargin m:val="0" />
<m:defJc m:val="centerGroup" />
<m:wrapIndent m:val="1440" />
<m:intLim m:val="subSup" />
<m:naryLim m:val="undOvr" />
</m:mathPr>
<w:uiCompat97To2003 />
<w:themeFontLang w:val="de-DE" />
<w:clrSchemeMapping w:bg1="light1" w:t1="dark1" w:bg2="light2" w:t2="dark2" w:accent1="accent1" w:accent2="accent2" w:accent3="accent3" w:accent4="accent4" w:accent5="accent5" w:accent6="accent6" w:hyperlink="hyperlink" w:followedHyperlink="followedHyperlink" />
<w:doNotIncludeSubdocsInStats />
<w:doNotAutoCompressPictures />
<w:decimalSymbol w:val="," />
<w:listSeparator w:val=";" />
</w:settings>

View File

@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:webSettings xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:optimizeForBrowser/></w:webSettings>

View File

@ -40,6 +40,9 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
'DocProps' => 'DocProps',
'Document' => 'Document',
'Styles' => 'Styles',
'Numbering' => 'Numbering',
'Settings' => 'Settings',
'WebSettings' => 'WebSettings',
'Header' => 'Header',
'Footer' => 'Footer',
'Footnotes' => 'Notes',