Skip to content

Commit 930d8de

Browse files
committed
ODT Writer: Enable title element and custom document properties
1 parent 0360f8e commit 930d8de

File tree

7 files changed

+57
-30
lines changed

7 files changed

+57
-30
lines changed

CHANGELOG.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
1515
- HTML: Ability to add elements to PHPWord object via html - @basjan GH-231
1616
- ListItemRun: New element that can add a list item with inline formatting like a textrun - @basjan GH-235
1717
- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149
18-
- RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158
18+
- RTF Writer: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158
1919
- Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237
20-
- RTF: Ability to add links and page breaks in RTF - @ivanlanin GH-196
20+
- RTF Writer: Ability to add links and page breaks in RTF - @ivanlanin GH-196
2121
- ListItemRun: Remove fontStyle parameter because ListItemRun is inherited from TextRun and TextRun doesn't have fontStyle - @ivanlanin
2222
- Config: Ability to use a config file to store various common settings - @ivanlanin GH-200
23-
- ODT: Enable inline font style in TextRun - @ivanlanin
24-
- ODT: Enable underline, strike/doublestrike, smallcaps/allcaps, superscript/subscript font style - @ivanlanin
25-
- ODT: Enable section and column - @ivanlanin
26-
- PDF: Add TCPDF and mPDF as optional PDF renderer library - @ivanlanin
23+
- ODT Writer: Enable inline font style in TextRun - @ivanlanin
24+
- ODT Writer: Enable underline, strike/doublestrike, smallcaps/allcaps, superscript/subscript font style - @ivanlanin
25+
- ODT Writer: Enable section and column - @ivanlanin
26+
- PDF Writer: Add TCPDF and mPDF as optional PDF renderer library - @ivanlanin
27+
- ODT Writer: Enable title element and custom document properties - @ivanlanin
2728

2829
### Bugfixes
2930

docs/intro.rst

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,15 @@ Writers
6161
+---------------------------+----------------------+--------+-------+-------+--------+-------+
6262
| Features | | DOCX | ODT | RTF | HTML | PDF |
6363
+===========================+======================+========+=======+=======+========+=======+
64-
| **Document Properties** | Standard || | | | |
64+
| **Document Properties** | Standard || | | | |
6565
+---------------------------+----------------------+--------+-------+-------+--------+-------+
66-
| | Extended || | | | |
67-
+---------------------------+----------------------+--------+-------+-------+--------+-------+
68-
| | UserDefined || | | | |
66+
| | Custom ||| | | |
6967
+---------------------------+----------------------+--------+-------+-------+--------+-------+
7068
| **Element Type** | Text ||||||
7169
+---------------------------+----------------------+--------+-------+-------+--------+-------+
7270
| | Text Run ||||||
7371
+---------------------------+----------------------+--------+-------+-------+--------+-------+
74-
| | Title || | |||
72+
| | Title || | |||
7573
+---------------------------+----------------------+--------+-------+-------+--------+-------+
7674
| | Link ||||||
7775
+---------------------------+----------------------+--------+-------+-------+--------+-------+
@@ -124,9 +122,7 @@ Readers
124122
+===========================+======================+========+=======+=======+
125123
| **Document Properties** | Standard || | |
126124
+---------------------------+----------------------+--------+-------+-------+
127-
| | Extended || | |
128-
+---------------------------+----------------------+--------+-------+-------+
129-
| | UserDefined || | |
125+
| | Custom || | |
130126
+---------------------------+----------------------+--------+-------+-------+
131127
| **Element Type** | Text ||| |
132128
+---------------------------+----------------------+--------+-------+-------+

docs/src/documentation.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ Below are the supported features for each file formats.
7878

7979
| Features | | DOCX | ODT | RTF | HTML | PDF |
8080
|-------------------------|--------------------|------|-----|-----|------|-----|
81-
| **Document Properties** | Standard || | | | |
82-
| | Extended || | | | |
83-
| | UserDefined || | | | |
81+
| **Document Properties** | Standard ||| | | |
82+
| | Custom ||| | | |
8483
| **Element Type** | Text ||||||
8584
| | Text Run ||||||
86-
| | Title || | |||
85+
| | Title || | |||
8786
| | Link ||||||
8887
| | Preserve Text || | | | |
8988
| | Text Break ||||||
@@ -111,8 +110,7 @@ Below are the supported features for each file formats.
111110
| Features | | DOCX | ODT | RTF |
112111
|-------------------------|--------------------|------|-----|-----|
113112
| **Document Properties** | Standard || | |
114-
| | Extended || | |
115-
| | UserDefined || | |
113+
| | Custom || | |
116114
| **Element Type** | Text ||| |
117115
| | Text Run || | |
118116
| | Title ||| |
-2.92 KB
Binary file not shown.
177 Bytes
Binary file not shown.

src/PhpWord/Writer/ODText/Element/Title.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public function write()
3535
return;
3636
}
3737

38-
$xmlWriter->startElement('text:p');
38+
$xmlWriter->startElement('text:h');
39+
$xmlWriter->writeAttribute('text:outline-level', $element->getDepth());
3940
$xmlWriter->writeRaw($element->getText());
40-
$xmlWriter->endElement(); // text:p
41+
$xmlWriter->endElement(); // text:h
4142
}
4243
}

src/PhpWord/Writer/ODText/Part/Meta.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\ODText\Part;
1919

20+
use PhpOffice\PhpWord\Shared\XMLWriter;
21+
2022
/**
2123
* ODText meta part writer: meta.xml
2224
*/
@@ -42,27 +44,56 @@ public function write()
4244
$xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
4345
$xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
4446
$xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
45-
46-
// office:meta
4747
$xmlWriter->startElement('office:meta');
48+
49+
// Core properties
4850
$xmlWriter->writeElement('dc:creator', $docProps->getLastModifiedBy());
4951
$xmlWriter->writeElement('dc:date', gmdate('Y-m-d\TH:i:s.000', $docProps->getModified()));
5052
$xmlWriter->writeElement('dc:description', $docProps->getDescription());
5153
$xmlWriter->writeElement('dc:subject', $docProps->getSubject());
5254
$xmlWriter->writeElement('dc:title', $docProps->getTitle());
55+
56+
// Extended properties
57+
$xmlWriter->writeElement('meta:generator', 'PHPWord');
5358
$xmlWriter->writeElement('meta:creation-date', gmdate('Y-m-d\TH:i:s.000', $docProps->getCreated()));
5459
$xmlWriter->writeElement('meta:initial-creator', $docProps->getCreator());
5560
$xmlWriter->writeElement('meta:keyword', $docProps->getKeywords());
5661

57-
// @todo : Where these properties are written ?
58-
// $docProps->getCategory()
59-
// $docProps->getCompany()
62+
// Category, company, and manager should be put in meta namespace
63+
$properties = array('Category', 'Company', 'Manager');
64+
foreach ($properties as $property) {
65+
$method = "get{$property}";
66+
if ($docProps->$method() !== null) {
67+
$this->writeCustomProperty($xmlWriter, $property, $docProps->$method());
68+
}
69+
}
6070

61-
$xmlWriter->endElement();
71+
// Other custom properties
72+
// @todo Check type. Currently all assumed as string
73+
foreach ($docProps->getCustomProperties() as $property) {
74+
$value = $docProps->getCustomPropertyValue($property);
75+
$this->writeCustomProperty($xmlWriter, $property, $value);
76+
}
6277

63-
$xmlWriter->endElement();
78+
$xmlWriter->endElement(); // office:meta
79+
$xmlWriter->endElement(); // office:document-meta
6480

65-
// Return
6681
return $xmlWriter->getData();
6782
}
83+
84+
/**
85+
* Write individual property
86+
*
87+
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
88+
* @param string $property
89+
* @param string $value
90+
* @param string $type
91+
*/
92+
private function writeCustomProperty(XMLWriter $xmlWriter, $property, $value, $type = null)
93+
{
94+
$xmlWriter->startElement('meta:user-defined');
95+
$xmlWriter->writeAttribute('meta:name', $property);
96+
$xmlWriter->writeRaw($value);
97+
$xmlWriter->endElement(); // meta:user-defined
98+
}
6899
}

0 commit comments

Comments
 (0)